在下面的代碼中,我試圖為在“匯總資料表列:”底部的部分。此外,columnDefs = list(list(width = '100px', targets = "_all"))一旦用戶單擊“按期間 2”單選按鈕,我使用以下陳述句實作的任何格式都會被清除。
關于如何調整渲染表中列寬的任何建議?
當我對此進行研究時,我對此缺乏清晰度/一致性感到驚訝。至少,在我的情況下,能夠調整“Period...”最左側列的寬度,并且能夠統一調整“Period”右側的所有其他列的寬度會很有用..”,如果無法獨立調整每列的寬度。
代碼:
library(dplyr)
library(DT)
library(shiny)
library(shinyWidgets)
library(tidyverse)
ui <-
fluidPage(
fluidRow(
column(width = 8,
h3("Data table:"),
tableOutput("data"),
h3("Sum the data table columns:"),
radioButtons(
inputId = "grouping",
label = NULL,
choiceNames = c("By period 1", "By period 2"),
choiceValues = c("Period_1", "Period_2"),
selected = "Period_1",
inline = TRUE
),
DT::dataTableOutput("sums")
)
)
)
server <- function(input, output, session) {
data <- reactive({
data.frame(
Period_1 = c("2020-01", "2020-02", "2020-03", "2020-01", "2020-02", "2020-03"),
Period_2 = c(1, 2, 3, 3, 1, 2),
ColA = c(1000.01, 20, 30, 40, 50, 60),
ColB = c(15.06, 25, 35, 45, 55, 65)
)
})
colNames <- reactive({c(stringr::str_replace(input$grouping, fixed("_"), " "), "Col A", "Col B") })
summed_data <- reactive({
data() %>%
group_by(!!sym(input$grouping)) %>%
select("ColA","ColB") %>%
summarise(across(everything(), sum))
})
output$data <- renderTable(data())
output$sums <- renderDT({
summed_data() %>%
datatable(
rownames = FALSE,
colnames=colNames(),
options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '100px', targets = "_all"))
)
)
})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
您需要先設定autoWidth為FALSE,然后您可以使用該targets選項為不同的列指定不同的寬度。第一列被索引為0。
library(dplyr)
library(DT)
library(shiny)
library(shinyWidgets)
library(tidyverse)
ui <-
fluidPage(
fluidRow(
column(width = 8,
h3("Data table:"),
tableOutput("data"),
h3("Sum the data table columns:"),
radioButtons(
inputId = "grouping",
label = NULL,
choiceNames = c("By period 1", "By period 2"),
choiceValues = c("Period_1", "Period_2"),
selected = "Period_1",
inline = TRUE
),
DT::dataTableOutput("sums")
)
)
)
server <- function(input, output, session) {
data <- reactive({
data.frame(
Period_1 = c("2020-01", "2020-02", "2020-03", "2020-01", "2020-02", "2020-03"),
Period_2 = c(1, 2, 3, 3, 1, 2),
ColA = c(1000.01, 20, 30, 40, 50, 60),
ColB = c(15.06, 25, 35, 45, 55, 65)
)
})
colNames <- reactive({c(stringr::str_replace(input$grouping, fixed("_"), " "), "Col A", "Col B") })
summed_data <- reactive({
data() %>%
group_by(!!sym(input$grouping)) %>%
select("ColA","ColB") %>%
summarise(across(everything(), sum))
})
output$data <- renderTable(data())
output$sums <- renderDT({
summed_data() %>%
datatable(
rownames = FALSE,
colnames=colNames(),
options = list(
autoWidth = FALSE,
columnDefs = list(
list(width = '35px', targets = c(0)),
list(width = '150px', targets = c(1,2)))
)
)
})
}
shinyApp(ui, server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393426.html
