I'm making an app in shiny, and have run into a small but irritating problem. Part of the output that I produce is outputted using DT::renderDataTable
. There are only two columns, and the width of the first column will depend on the input dataset, so I don't want to absolutely fix the width to anything. I've seen this thread, and the code in one of the answers works to some extent. However, if I push the actionButton a second time, the table resizes itself to fill the entire width of the output window.
Is there a way to prevent the table from resizing itself after pressing the action button again? I know that using just renderTable
and tableOutput
will solve the problem, but I like the dataTableOutput
, and would prefer to use that function.
For a 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) )