Fit wide table in PDF when downloaded using button from renderDT/datatable - r

I have a shiny app that exports a data table that can be downloaded using PDF, CSV, and Excel "buttons" from DT::datatable(). However, the exported data table is very wide, and so the exported PDF is cut off on the right side. Is there a way to make the pdf export in landscape (or otherwise ensure it fits within the page)?
A minimal reproducible example using the iris dataset is here:
library(shiny)
library(DT)
ui <- fluidPage(DTOutput('tbl'))
server <- function(input, output) {
output$tbl = renderDT(
cbind(iris, iris), # example wide table
extensions = "Buttons",
options = list(
lengthChange = FALSE,
dom = "tB",
buttons = c("pdf", "excel", "csv", "copy")
)
)
}
shinyApp(ui, server)
In options, I have tried orientation = "landscape" and toggling between TRUE and FALSE for fixedColumns, autoWidth and scrollX. Based on this DT help page I tried changing the button to pdfHtml5 (instead of pdf) and extensions = c("Buttons", "pdfHtml5") (along with orientation = "landscape"). Unfortunately none of this worked.
I would prefer to keep within the options/functionality in the renderDT() function as opposed to a fresh approach, if possible.

Related

Combining DT::datatable, DT::dataTableProxy and "SearchPanes" Extension in R Shiny

It appears that DT::dataTableProxy is not possible with SearchPanes extension because:
SearchPanes requires Select extension.
Select extension requires DT::renderDT(server = FALSE) option.
DT::dataTableProxy does not work on the client side and throws DT error.
library(shiny)
library(shinydashboard)
library(tidyverse)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
shiny::selectInput("rows", label = "Rows", choices = 1:nrow(mtcars)),
shiny::actionButton("new", label = "New Data")
),
dashboardBody(DT::dataTableOutput("cars"))
)
server <- function(input, output) {
rows <- reactive({ input$rows })
output$cars <- DT::renderDataTable(server = FALSE, {
expr = DT::datatable(
data = mtcars |> head(rows())
#,
#extensions = c("SearchPanes", "Select", "Buttons"),
#options = list(
# dom = "Btip",
# buttons = list("searchPanes")
#)
)
})
dtProxy <- DT::dataTableProxy("cars")
observeEvent(input$new, label = "Observe button proxy update", {
doubledata <- bind_rows(mtcars, mtcars)
DT::replaceData(proxy = dtProxy,
data = doubledata,
resetPaging = FALSE)
})
}
shinyApp(ui, server)
Try this code using server = FALSE, click New Data, you will receive DT Warning:
DataTables warning: table id=DataTables_Table_0 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
Remove server = FALSE and proxy runs.
Remove the commented section, and search panes appear, but with no actual filters represented, and message stating that if we really want to use select extension then set select = 'none'.
Here are some reference materials:
RStudio DT Extensions
Matt Herman Tutorial
What I ended up doing was use only the DT::datatableProxy feature, then use a custom button for the search panes. Custom button was found here How to add custom button in R Shiny datatable. This required making a new reactive which was invalidated by the first, and checking if the inputs had any new values. Then the proxy received the filtered data.
Maybe someday they will add support for search panes.
Add server-side support for SearchPanes extension #877

How to add checkBox for variables group with tabPanel in R shiny

I need help in adding variables checkbox in r shiny within the tabPanel. I have already developed an r shiny app, in which on one of the page the data is displayed on the basis of some filterers and keywords search box.
So, I want to include one checkbox group of variables for the uploaded file.
I saw some of the solution but, non of them is based on tabPanel. Is it possible to have variable checkBox with tabPanel and how to place this under UI and SERVER. Also, as rest of the development is done so, would like to have solution with tabPanel if possible. Thanks
Tried adding the checkBoxInput but with not working with tabPanel and disturbing the current tabPanel
Below is my UI and SERVER
Here, the complete code is not displayed but, with following ui and server, wanted to add checkBoxInputgroup of variable and it should take the variable values from dfcase().
ui <- tabPanel('Evidence Finder in Background Text',
selectizeInput("Words1",
label="Important Words - Dictionary",
choices= TRUE,
multiple = TRUE,
options = list(maxOptions = 20000)),
DT::dataTableOutput("table2")
)
output$table2 <- DT::renderDataTable(server = FALSE,{
dfcase_bckg = as.data.frame(dfcase())
DT::datatable(dfcase_bckg,
rownames = FALSE,
escape = TRUE,
class = 'cell-border stripe',
selection = "single",
extensions = 'Buttons',
)
}
)
This is the view of the app and to left side i need the list of variables with checkbox

show a bird's-eye view of datatable in shiny-based webpage

The final aim is to show all the columns and rows in a page without scrolling, even making the table smaller (like zoom in). Because I'd like to get a bird's-eye view of this table, without caring about numbers in table. Thank you.
one step can be show colnames vertically in shiny-based DT::datatable as they take lots of space, though I still do not know how to implement it.
Code:
library(shiny)
library(ggplot2)
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl')),
server = function(input, output) {
output$tbl = DT::renderDataTable(
DT::datatable({cbind(mpg, mpg, mpg)},
options = list(paging = FALSE))
)
}
)

Data Table Output not scrollable in Y direction

I have data I wish to show in a flexdashboard in R. I build the datatable with DT::renderDataTable({DT::datatable(data(), options=list(scrollX=TRUE))})
This works just fine when showing something like 10 entries, but when I select the option to show 25 entries, I cannot scroll down to the bottom of the page and click on the second page button, next button, etc. I cannot scroll vertically like I could previously. I have tried the sScrollY = "300px" options, but this doesn't let the data table expand to fill the full page on my flexdashboard. The problem is rows of observations being cut off and inaccessible when I try to scroll in the y-direction.
I am wondering what I need to do to make datatables expand and fill as expected, as shown in https://shiny.rstudio.com/gallery/datatables-options.html
From the example, you can see how it is still possible to scroll up and down when you change the number of rows shown. I cannot do this in the new version of datatable. As of right now, I am limiting the number of rows displayed to 10...however, this is not a long term solution.
Any ideas are greatly appreciated. Thank you. Best, NF
I haven't been able to find a solution I am satisfied with yet, but for the interim, I am using the sScrollY = '75vh' arguement and building the datatable like this:
DT::renderDataTable({
DT::datatable(plot_data(), options = list(scrollX = TRUE, sScrollY = '75vh', scrollCollapse = TRUE), extensions = list("Scroller"))
})
At least this way the pagination is visible. If anyone has additional ideas, I'd love to hear them. Cheers for now. --Nate
I had the same problem, I could'nt make datatables expand. The problem was that all the datatables have the option autoWidth = FALSE by default, so you need to change that to autoWidth = TRUE.
Try something like this:
DT::renderDataTable({DT::datatable(data(), options=list(autoWidth = TRUE,scrollX=TRUE))})
After that you should fine with the Width manipulation.
Here is an example.
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(title="Data Table"),
dashboardSidebar(
menuItem(text="Menu",icon=icon("bars"),
menuSubItem(text="Show datatable",tabName="ShowData", icon=icon("search")))
),
dashboardBody(
tabItems(
tabItem(tabName="ShowData",
box(DT::dataTableOutput("Data"),width = 12)))))
server <- shinyServer(function(input, output) {
output$Data<-DT::renderDataTable({DT::datatable(data(),options = list(autoWidth = TRUE,scrollX = TRUE))})
})
shinyApp(ui = ui, server = server)
Thanks for reporting this. As I've answered at rstudio/DT#818, the issue can be resolved by adding an option fillContainer = TRUE to DT::datatable().
I mean changing the chunck like below will be enough.
### renderDataTable (reactive)
```{r}
DT::renderDataTable(datatable(mydataset(), rownames = TRUE,
options = list(bPaginate = FALSE, searching = FALSE, info = FALSE),
fillContainer = TRUE))
```
The reason that using static data(DT::datatable()) works is fillContainer will be enabled by FlexDashBoard. However, under shiny mode, this feature fails to perform.
You may use: option = list(scrollY = 300, scrollCollapse = TRUE). I tried this in R Notebook and it works for me.

DataTable TableTools Extension Implementation in Shiny

I've got a dataTabe for which I'm trying to implement tableTools in order to export the records in csv format. However, when the filtered data is more than 1 page worth of records, as in the example provided here, the export button doesn't pick up the records on the 2nd page and onwards and it only exports the 1st page.
From my research, it appears that oSelectorOps:{ page: 'all' } option should do the trick. However I couldn't get it to work. If you run the code below and hit the export button, it will result in a csv file with only 100 rows (i.e. the first page) and not the entire table. Please advise if my syntax is incorrect or if there's a better alternative to attain this.
Please note that I don't want to use the downloadHandler because I would like to be able to export the data when filtered using the DataTable filter fields, at the bottom of the table.
Please click here and here to help with similar questions.
Here's my reproducible example:
#Load required packages
require(shiny)
#Create a dataframe
df <- data.frame(random=1:160)
server <- function(input,output,session){
#Display df using DataTable and apply desired options
output$display <- renderDataTable({df},
option=list(pageLength=100,
"dom" = 'T<"clear">lfrtip',
"tableTools" = list(
"sSwfPath" = "//cdn.datatables.net/tabletools/2.2.3/swf/copy_csv_xls_pdf.swf",
"aButtons" = list(list("sExtends" = "csv","oSelectorOpts"=list("page"="all"),"sButtonText" = "Export","aButtons" ="csv")))
)
)
}
ui <- shinyUI(fluidPage(
#Add a title
h1('Testing TableTools'),
#Add required JS libraries
tagList(
singleton(tags$head(tags$script(src='//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='//cdn.datatables.net/tabletools/2.2.3/js/dataTables.tableTools.min.js',type='text/javascript'))),
singleton(tags$head(tags$link(href='//cdn.datatables.net/tabletools/2.2.3/css/dataTables.tableTools.css',rel='stylesheet',type='text/css')))
),
mainPanel(
#Display results
dataTableOutput('display')
)
))
shinyApp(ui = ui, server = server)
Try this out:
{
sExtends: "csv",
"oSelectorOpts": {
page: 'all',
filter:'applied'
},
"mColumns": "visible"
},

Resources