gpt4 book ai didi

R Shiny - DT::renderDataTable 列宽

转载 作者:行者123 更新时间:2023-12-03 17:42:27 32 4
gpt4 key购买 nike

我正在制作一个 Shiny 的应用程序,但遇到了一个小而烦人的问题。我产生的部分输出是使用 DT::renderDataTable 输出的.只有两列,第一列的宽度将取决于输入数据集,所以我不想绝对固定宽度。我见过this线程,并且其中一个答案中的代码在某种程度上有效。但是,如果我第二次按下 actionButton,表格会自动调整大小以填充输出窗口的整个宽度。

有没有办法防止表格在再次按下操作按钮后自行调整大小?我知道只使用 renderTabletableOutput会解决问题,但我喜欢 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) )

最佳答案

您可以添加 width ui 函数中的参数而不是 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) )

关于R Shiny - DT::renderDataTable 列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32955610/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com