[英]R shiny - DT::renderDataTable column width
我正在制作一個閃亮的應用程序,並遇到了一個小而刺激性的問題。 我生成的部分輸出是使用DT::renderDataTable
輸出的。 只有兩列,第一列的寬度將取決於輸入數據集,所以我不想絕對地將寬度固定為任何東西。 我已經看過這個帖子了,其中一個答案中的代碼在某種程度上起了作用。 但是,如果我第二次按下actionButton,表會自行調整大小以填充輸出窗口的整個寬度。
有沒有辦法防止表再次按下操作按鈕后自行調整大小? 我知道只使用renderTable
和tableOutput
會解決問題,但我喜歡dataTableOutput
,並且更喜歡使用該函數。
對於MWE:
library("shiny")
library("DT")
mwe_ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
titlePanel("Example"),
sliderInput(inputId = "df",
label = "Degrees of Freedom:",
min=1 , max=50 , value=1 , step=1
),
actionButton(inputId = "compute1",
label = "Sample"
)
),
mainPanel(
dataTableOutput( outputId = "summary" )
)
)))
mwe_server <- function(input, output) {
temp01 <- reactive({
compute1 <- input$compute1
if( compute1 > 0 ){
isolate({
aa <- round( runif(6, 4,20 ) )
bb <- character()
for( ii in 1:6 ){
bb[ii] <- paste0(sample(letters, size=aa[ii]), collapse="")
}
xx <- matrix( round(rt(6, df=input$df), 4), nrow=6, ncol=1 )
return( data.frame(xx) )
})
}
})
##############
output$summary <- DT::renderDataTable({
temp02 <- temp01()
}, rownames=FALSE,
options = list(autoWidth = TRUE,
columnDefs = list(list(width = "125px", targets = "_all"))
)
)
}
runApp( list(ui=mwe_ui, server=mwe_server) )
你可以在ui函數中添加width
參數而不是在服務器的columnDefs
中添加嗎?
library("shiny")
library("DT")
mwe_ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
titlePanel("Example"),
sliderInput(inputId = "df",
label = "Degrees of Freedom:",
min=1 , max=50 , value=1 , step=1
),
actionButton(inputId = "compute1",
label = "Sample"
)
),
mainPanel(
dataTableOutput( outputId = "summary" , width="125px")
)
)))
mwe_server <- function(input, output) {
temp01 <- reactive({
compute1 <- input$compute1
if( compute1 > 0 ){
isolate({
aa <- round( runif(6, 4,20 ) )
bb <- character()
for( ii in 1:6 ){
bb[ii] <- paste0(sample(letters, size=aa[ii]), collapse="")
}
xx <- matrix( round(rt(6, df=input$df), 4), nrow=6, ncol=1 )
return( data.frame(xx) )
})
}
})
##############
output$summary <- DT::renderDataTable({
temp02 <- temp01()
}, rownames=FALSE,
options = list(autoWidth = TRUE)
)
}
runApp( list(ui=mwe_ui, server=mwe_server) )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.