Fix width of a DT in shiny - r

I'm trying to fix the width of very wide table in shiny but haven't mange to do it. I try the answer here Shrink DT::dataTableOutput Size but it didn't work, I also tried the answer from here https://github.com/rstudio/DT/issues/29 with percentage and pixels for all the columns and it didn't work neither. This is an example of the table and problem I have:
shinyApp(
ui = fluidPage(
DT::dataTableOutput("table")
),
server <- function(input, output) {
x <- cbind(iris,iris,iris,iris)
output$table <- DT::renderDataTable(x)
}
)

I think this does what you want:
library(shiny)
shinyApp(
ui = fluidPage(
DT::dataTableOutput("table",width='500px')
),
server <- function(input, output) {
x <- cbind(iris,iris,iris,iris)
output$table <- DT::renderDataTable(x,options=list(scrollX=T))
}
)

Related

dataTableOutput fit column size

Building a Shiny Dashboard where I need to render a matrix.
For that I plan to use a DT::renderDataTable.
Everything is inside a FluidRow with a column size of 3 (out of 12).
The DataTable spread out of the defined column.
How can I force the table to fit the column and don't go over it ?
Perhaps you can use splitLayout to obtain your desired output. Try this
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Test App"
),
dashboardSidebar(),
dashboardBody(
splitLayout(cellWidths = c("25%", "25%", "50%"),
DTOutput("t1"), DTOutput("t2"), DTOutput("t3")
)
)
)
server <- function(input, output) {
output$t1 <- renderDT({mtcars})
output$t2 <- renderDT({mtcars[9:15,]})
output$t3 <- renderDT({mtcars[21:32,]})
}
shinyApp(ui, server)

how can I dynamically update datatable in shiny app? [r]

I would like to clear a datatable with an actionButton.
The chunk does work, but the table is still there. How can I update the content of the table when the actionButton is pressed?
Please find the toy sampel as following.
library(shiny)
library(shinydashboard)
library(DT)
DT1 <- iris
shinyApp(
ui <-
dashboardPage(
dashboardHeader(title = ""),
dashboardSidebar(),
dashboardBody(
DTOutput("DT"),
actionButton("clear", "clear")
)
),
server <-
function(input, output, session) {
output$DT <- renderDT(datatable(DT1))
observeEvent(input$clear, {
DT1 <- data.frame()
})
}
)
This is what you need -
server <- function(input, output, session) {
dt_data <- reactiveValues(dt_data = iris)
observeEvent(input$clear, {
dt_data$dt_data <- data.frame() # use iris[0, ] if you want empty df
})
output$DT <- renderDT(datatable(dt_data$dt_data))
}

Adjust the position of actionButton widget in R shiny DT table

The given R shiny script creates a simple data table as in the snapshot with an actionButton in the center. I want to place the button a little below it's current position such that it is in perfect horizontal inline position to the search bar. Thanks and please help.
library(DT)
library(shiny)
library(shinyBS)
library(rpivotTable)
library(bupaR)
ui <- basicPage(
h2("The mtcars data"),
column(5,offset = 5,actionButton("CR1_S1", "Button")),
dataTableOutput("mytable1")
)
server <- function(input, output) {
output$mytable1 <- DT::renderDataTable({
DT::datatable(iris)
})
}
shinyApp(ui, server)
You can put the button inside the datatable in this way:
ui <- basicPage(
h2("The mtcars data"),
actionButton("CR1_S1", "Button"),
DTOutput("mytable1")
)
server <- function(input, output) {
output$mytable1 <- renderDT({
datatable(iris, callback = JS("$('div.button').append($('#CR1_S1'));"),
options = list(
dom = '<"row"<"col-sm-4"l><"col-sm-4 text-center"<"button">><"col-sm-4"f>>tip'
))
})
}
shinyApp(ui, server)

How to specify the width of a sidebarPanel in pixels?

How to specify the width of a sidebarPanelin pixels? The width argument is not accurate enough in my case.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(width = 5), # This is not accurate enough!
mainPanel()))
server <- function(input, output, session) {}
shinyApp(ui, server)
You can also specify the width as a function of the percentage, 100% = full width instead of hard-coding it to px
library(shiny)
ui <- fluidPage(
sidebarLayout(
div(style="width: 70%;",sidebarPanel(width = 5)), # This is not accurate enough!
mainPanel()))
server <- function(input, output, session) {}
shinyApp(ui, server)
With the help of R shiny - background of sidebar panel I found the following solution:
library(shiny)
ui <- fluidPage(
tags$head(tags$style(HTML('#sidebar {width: 100px;}'))),
sidebarLayout(
sidebarPanel(id = "sidebar"),
mainPanel()))
server <- function(input, output, session) {}
shinyApp(ui, server)
This one does not affect other wellPanels.

Using renderDataTable within renderUi in Shiny

I'm experimenting a Shiny App to show dynamic contexts, but I cannot get renderDataTable working into a renderUi component.
Below two simple replicable tests: the first one is not working, the second one without renderUi works fine, of course.
What is the conceptually difference between this two, and why the first one cannot work in Shiny?
This one not works: note that the uiOutput myTable, contains two reactive component, a selectInput and a renderDataTable, but only the selectInput is rendered.
library(shiny)
runApp(list(
ui = fluidPage(
fluidRow(h2("where is the table?")),
uiOutput('myTable')
),
server = function(input, output) {
output$myTable <- renderUI({
fluidPage(
fluidRow(selectInput("test", "test", c(1,2,3))),
fluidRow(renderDataTable(iris))
)
})
}
))
This is fine, both selectInput and renderDataTable are rendered:
library(shiny)
runApp(list(
ui = fluidPage(
fluidRow(h2("where is the table?")),
fluidRow(selectInput("test", "test", c(1,2,3))),
fluidRow(dataTableOutput('myTable'))
),
server = function(input, output) {
output$myTable = renderDataTable(iris)
}
))
How to get the first scenario working?
Thanks.
EDITING after Yihui comment (thanks Yihui):
In renderUi has to be used some ui function, and not some render function:
changed the sample code in the correct way, the result does not change: still no data is shown.
library(shiny)
runApp(list(
ui = basicPage(
uiOutput('myTable')
),
server = function(input, output) {
output$myTable <- renderUI({dataTableOutput(iris)
})
}
))
EDIT n.2
Just solved, got it working so:
library(shiny)
runApp(list(
ui = fluidPage(
mainPanel(
uiOutput('myTable')
)
),
server = function(input, output) {
output$myTable <- renderUI({
output$aa <- renderDataTable(iris)
dataTableOutput("aa")
})
}
))
I have to save the renderTableOutput in a output variable first, and then feeding it to dataTableOutput.
Thanks for pointing me to: here
It would be clearer if you split the part of datatable generation and ui generation :
library(shiny)
runApp(list(
ui = fluidPage(
mainPanel(
uiOutput('myTable')
)
),
server = function(input, output) {
output$aa <- renderDataTable({iris})
output$myTable <- renderUI({
dataTableOutput("aa")
})
}
))

Resources