Controlling table width in Shiny dataTableOutput - r

I am unable to control the width of a datatable I have added to a shiny app using the function dataTableOutput(). I've tried to use the width parameter within the function but it changes nothing in the output and there is no error ~ it's not telling me that it's ignoring the width parameter.
library(shiny)
library(shinythemes)
ui <- fluidPage(theme = shinytheme("Spacelab"),
fluidRow(
column(6,dataTableOutput(outputId = "table")),
column(6,p(textOutput("para")))
)
)
server <- function(input, output){
df <- as.data.frame(matrix(0, ncol = 15, nrow = 20))
output$table <- renderDataTable({df})
output$para <- renderText({
text <- rep(x = "Hello World",1000)
})
}
shinyApp(ui = ui,server = server)

dataTableOutput does not have an argument width. You can use column within a fluidRow with argument width, supplying an integer between 1 and 12.
library(shinythemes)
ui <- fluidPage(theme = shinytheme("Spacelab"),
fluidRow(
column(
dataTableOutput(outputId = "table"), width = 6)
)
)
server <- function(input, output){
df <- as.data.frame(matrix(0, ncol = 20, nrow = 5))
output$table <- renderDataTable({df},
options = list(scrollX = TRUE))
}
shinyApp(ui = ui,server = server)
Options from the JavaScript library DataTable can be passed directly via renderDataTable argument options. For example, setting scrollX to be true allows tables to scroll.

If you use the "DT" R package, and the respective DT::dataTableOutput and DT::renderDataTable, you can use a "width" option with those calls, which apparently can be either a % (e.g. width = "100%") or pixels (width = 300) which should get you the control you want.
See:
https://rstudio.github.io/DT/shiny.html
Note from that page:
Important: Be sure to use the DT:: prefix when calling dataTableOutput
and renderDataTable so that the DT versions of these functions are
guaranteed to be called, instead of the deprecated Shiny versions. If
you make sure to library(DT) after library(shiny), normally the DT
versions should just override the shiny versions if you do not use the
DT:: prefix (when in doubt, use this prefix, until we completely
remove these functions from shiny)

Related

Save filtered DT::datatable into a new dataframe R shiny

Let's say that I have a shiny app displaying a data table like the following:
library(shiny)
library(tidyverse)
library(datasets)
library(DT)
data<- as.data.frame(USArrests)
#data<- cbind(state = rownames(data), data)
ui <- fluidPage(
dataTableOutput("preview")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$preview<- renderDataTable(
datatable(data, options = list(searching = T, pageLength = 10, lengthMenu = c(5,10,15, 20), scrollY = "600px", scrollX = T ))
)
}
# Run the application
shinyApp(ui = ui, server = server)
Let's say I then type in "Iowa" into the search box. I would like to save that filtered datatable into a seperate dataframe within the app. I would like it to be dynamic as well so if I typed "Kentucky", it would save Kentucky's filtered data into the dataframe instead. Is there a way to do this?
NOTE: this is a DT datatable
Maybe this type of solution. It is possible to add further conditions like checking the first letter in upper case, but the main idea is to check each column and search for the pattern entered inside the datatable searchbox. This may or may not result in more than one dataset to print (depending if the string is partially matched in multiple columns (this is also solvable with rbind function.
code:
library(shiny)
library(tidyverse)
library(datasets)
library(DT)
data <- as.data.frame(USArrests)
data <- cbind(state = rownames(data), data)
ui <- fluidPage(
dataTableOutput("preview"),
tableOutput('filtered_df')
)
# Define server logic required to draw a histogram
server <- function(input, output) {
df <- reactiveValues()
output$preview<- renderDataTable(
datatable(data, options = list(searching = T, pageLength = 10, lengthMenu = c(5,10,15, 20), scrollY = "600px", scrollX = T ))
)
observeEvent(input$preview_search, {
searched_string <- map(data, ~str_subset(.x, input$preview_search)) %>% discard(~length(.x) == 0)
df$filtered <- syms(names(data)) %>%
map(~ filter(data, !!.x %in% searched_string)) %>%
discard(~ nrow(.x) == 0)
})
output$filtered_df <- renderTable({df$filtered})
}
# Run the application
shinyApp(ui = ui, server = server)

Shiny R: RowReorder, reordering all values not only first one

I have simple Shiny app with DT table
library(shiny)
library(DT)
iris2 = head(iris, 30)
server <- function(input, output) {
output$tb <-DT::renderDataTable(server=FALSE,{
datatable(
iris2,
colnames = c(colnames(iris2)), extensions = 'RowReorder',
options = list(rowReorder = TRUE))
})
}
ui <- fluidPage(dataTableOutput('tb', width = '200px', height = '200px'))
shinyApp(ui, server)
However, when I try to adjust the table row only the first column changes the position. It is probably related to the configuration of the ReorderRow, as described here. Unfortunately, I don't know how to implement JavaScript into the Shiny app, especially datatable options.
One has to add the row names and sort the table on them, as mentioned in the github issue. The working solutions requires only adding order = list(list(0, 'asc')) in the DT options:
library(shiny)
library(DT)
iris2 = head(iris, 30)
server <- function(input, output) {
output$tb <-DT::renderDataTable(server=FALSE,{
datatable(
iris2,
colnames = c(colnames(iris2)), extensions = 'RowReorder',
options = list(order = list(list(0, 'asc')), rowReorder = TRUE))
})
}
ui <- fluidPage(dataTableOutput('tb', width = '200px', height = '200px'))
shinyApp(ui, server)

R Shiny - Server rendered checkbox won't check

I'm trying to use awesomeCheckbox from the shinyWidgets package and am running into an issue where I can't check/uncheck the box that a server rendered.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
awesomeCheckbox(inputId = "checkboxA",
label = "A checkbox",
value = TRUE),
uiOutput("checkboxB"),
uiOutput("FS1")
)
server <- function(input, output) {
output$checkboxB <- renderUI({
awesomeCheckbox(inputId = "checkboxB",
label = "B checkbox",
value = TRUE)
})
output[[paste0("FS", 1)]] <- renderUI({
awesomeCheckbox(inputId = paste0("FS", 1),label = "FS", value = FALSE)
})
}
shinyApp(ui= ui, server=server)
I need this piece of code as part of a larger Shiny App where checkboxes are generated dynamically in the server (hence the weird paste0 naming).
I've checked my version of R and have tried using both Chrome and Safari but can't seem to get the FS checkbox to check/uncheck. I also can't seem to find anything out of the ordinary when I use "Inspect element" in my browser.
You have outputids checkboxB and FS1 respectively already rendered once, yet you create other components with the same names, hence they wont work, change the names as you cant have duplicate divs like so:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
awesomeCheckbox(inputId = "checkboxA",label = "A checkbox",value = TRUE),
uiOutput("checkboxB"),
uiOutput("FS1")
)
server <- function(input, output) {
output$checkboxB <- renderUI({
awesomeCheckbox(inputId = "checkboxBx",label = "B checkbox", value = TRUE)
})
output[[paste0("FS", 1)]] <- renderUI({
awesomeCheckbox(inputId = paste0("FSx", 1),label = "FS", value = FALSE)
})
}
shinyApp(ui= ui, server=server)

How to render a dataTable with definite row height?

I've been struggling for a few hours with such a task:
in R Shiny I need to display a table which contains of a single column of integers with the definite (relatively large) spacing between rows.
There is the spacing argument in the renderTable() function, but even setting it to the biggest value 'l' is still not enough for my purpose.
I've tried to do that using xtable and taking into account the example from Adjust row height xtable R , but with no result (I don't know CSS).
The most natural way I've found in the web is to use DT package along with the Scroller extension, but the following code still gives no results
ui.R:
fluidPage(
sidebarLayout(
sidebarPanel(
dataTableOutput('dtable', width = '50%') # argument 'height' does not work here
),
mainPanel()
)
)
server.R:
library(shiny)
library(DT)
function(input, output) {
output$dtable <- DT::renderDataTable({
data.frame(SSD = c(2, 17, 19, 35))
},
extensions = 'Scroller',
options = list(
dom = 't',
ordering = FALSE,
scroller = list(rowHeight = 100)
)
)
}
The output of that gives only column name (what is wrong??), but without Scroller extensions it displays the expected table - of course with too small spacing...
You want to use the rowCallback option and attach a style to each row:
server.R
library(shiny)
library(DT)
function(input, output) {
output$dtable <- DT::renderDataTable({
data.frame(SSD = c(2, 17, 19, 35))
},
options = list(
dom = 't',
ordering = FALSE,
rowCallback = JS("function(r,d) {$(r).attr('height', '100px')}")
)
)
}
Note that this may result in increased render time as the number of rows raises

An error occurred rendering the PivotTable results

I try to create shiny app with
rpivotTable and nvd3 rcharts
all works , but when i try to to show any chart from pivot
i get error
An error occurred rendering the PivotTable results.
But if i use only rpivotTable charts works in pivot and i think that there is problem when using rpivotTable and nvd3 rcharts in one shiny app.
Example
UI
library(shiny)
library(rCharts)
library(rpivotTable)
shinyUI(fluidPage(
showOutput('plot1',lib = "nvd3"),
rpivotTableOutput('pivot1', width = "100%", height = "500px"))
)
Server
library(shiny)
library(rCharts)
library(rpivotTable)
df=data.frame(A=c(1:10),B=c(-10:-1),C=c("x",rep(c("x","y","z"),3)))
shinyServer(function(input, output, session) {
output$pivot1 <- renderRpivotTable({
rpivotTable(data =df ,
width="100%", height="500px")
})
output$plot1=renderChart2({
myform <- as.formula(paste('A','~','B'))
n2 <- nPlot(myform, group ="C", data = df, type = 'multiBarChart')
n2$chart(margin = list(left = 100))
n2$chart(reduceXTicks = F)
n2$set(width = 800, height = 500)
print(n2)
})
})
Give me
If i use only rpivotTable charts in pivot works
When i look at inspect i see
TypeError: a.axisTimeFormat.multi is not a function
at e.i.initParams (c3.min.js:1)
at e.i.init (c3.min.js:1)
at new d (c3.min.js:1)
at Object.k.generate (c3.min.js:1)
at Object.renderer (c3_renderers.coffee:129)
at t.fn.pivot (pivot.coffee:546)
at pivot.coffee:835
Is there way to fix it?
Package versions :
rpivotTable_0.1.5.7
rCharts_0.4.2
shiny_0.12.2.9005
Thanks!
As pointed out in the comments, this is due to the double loading of the n3 libraries. To avoid this issue (this is more of a hack than a fix), you could plot the rcharts frame in an iframe to avoid js and css issues.
To do this, you can use uiOutput/renderUI for the shiny part, and show to output the rCharts.
Here's an example:
library(shiny)
library(rCharts)
library(rpivotTable)
df=data.frame(A=c(1:10),B=c(-10:-1),C=c("x",rep(c("x","y","z"),3)))
ui <-shinyUI(fluidPage(
uiOutput('plot1'),
rpivotTableOutput('pivot1')
))
server <- shinyServer(function(input, output, session) {
output$pivot1 <- renderRpivotTable({
rpivotTable(data =df)
})
output$plot1=renderUI({
myform <- as.formula(paste('A','~','B'))
n2 <- nPlot(myform, group ="C", data = df, type = 'multiBarChart')
n2$chart(margin = list(left = 100))
n2$chart(reduceXTicks = F)
HTML(paste(capture.output(n2$show('iframesrc', cdn = TRUE)), collapse = '\n'))
})
})
shinyApp(ui,server)

Resources