Issue:
I'm looking to remove the showing 1 to n of n entries field in shiny DT. Please see picture below of what I would like to REMOVE.
Any insight is much appreciated.
You can use the dom option to determine which elements of the data table are shown. In the call to data table, you pass a named list of options to the options argument. dom accepts a character string where each element corresponds to one DOM element.
# only display the table, and nothing else
datatable(head(iris), options = list(dom = 't'))
# the filtering box and the table
datatable(head(iris), options = list(dom = 'ft'))
In your case, i is the table information summary: that's the one you want to leave out. You can also use this method to remove other elements like the search box or pagination controls.
See section 4.2 on this page for how to do this in R: https://rstudio.github.io/DT/options.html
This page in the Datatables manual discusses the DOM option: https://datatables.net/reference/option/dom
You can pass the info option directly using options
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput('myTable')
)
server <- function(input, output, session) {
output$myTable <- renderDataTable(mtcars,
options = list(pageLength = 15, info = FALSE)
)
}
shinyApp(ui, server)
Related
I want to output a dataTable and preselect a row. This row can have a higher number than 10, in which case I want it to be shown in the dataTable. I have read you could use a dataTableProxy but it does not jump to the correct row. Is there an easy way to do this?
Here a minimal example:
library(shiny)
ui <- fluidPage(
DT::dataTableOutput("dtout")
)
server <- function(input, output, session) {
output$dtout<- DT::renderDT(iris)
dtproxy<-DT::dataTableProxy(session = session,outputId = "dtout")
DT::selectRows(dtproxy,14)
}
shinyApp(ui, server)
This is the result:
This is what I want to be shown directly:
Is there an easy way to do so?
You can achieve that by using the Select extension. But you have to know in advance the number of the page to be displayed (maybe there's a way to get it automatically, I don't know).
library(DT)
callback <- "
table.row(':eq(13)', {page: 'all'}).select(); // select row 13+1
table.page(1).draw('page'); // jump to page 1+1
"
datatable(
iris,
extensions = "Select", selection = "none",
callback = JS(callback)
)
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))
)
}
)
I am trying to create a responsive data table for my shiny application using DT package. I want to hide certain columns in advance. For example:
library("shiny")
library("DT")
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl')),
server = function(input, output) {
output$tbl = DT::renderDataTable(
iris,extensions="Responsive"
)
}
)
This output gives me 5 columns. It only hides columns when I narrow the page. But, I want to hide last 3 columns in advance and I just want to see first two columns every time. Is there a way to do that?
Update:
Example output
You can hide columns in your table using DT options or extensions.
If you want them to be hidden in advance but have a button to make them visible again, the ColVis extension should work well for you: link
If you just want thme stay hidden, add the following option (can't remember where I've seen its documentation right now..)
options=list(columnDefs = list(list(visible=FALSE, targets=columns2hide)))
I have another way which I like for its readability. It does not solve the problem of column numbering though.
library("shiny")
library("DT")
library(magrittr)
columns2hide <- match('Sepal.Width', colnames(iris))
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl')),
server = function(input, output) {
output$tbl = DT::renderDataTable(
{
dataTableProxy(outputId = 'tbl') %>%
hideCols(hide = columns2hide)
iris
},
extensions="Responsive"
)
}
)
dataTableProxy creates a proxy object that you can operate on with a couple of functions (see ?dataTableProxy).
It can be handy for hiding/showing/selecting/add/... rows and columns of the table when clicking on a button, for example.
Because it has deferUntilFlush = TRUE by default it waits with its handling of the table until its next generation. In this case this simply happens on the following line.
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"
},
I am trying to change the options of a renderDataTable call in Shiny to depend on the value of an input variable, in this case a checkbox.
The first checkbox successfully alters the content of the table, adding one more column if checked.
The second checkbox does not alter the options of the table. See code below, I am loading an updated version of datatables and other extensions, but that does not seem to have any effect here.
The third checkbox also does not alter the formatting of the entries in the table, which is what I ultimately would like to do.
Any ideas?
# server.R
library("ggplot2")
shinyServer(function(input, output, session) {
bold = reactive ({
bold = ''
bold = paste0(bold,'function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {')
bold = ifelse(input$checkbox3,paste0(bold),paste0(bold,'if (parseFloat(aData[0]) >= 0.1) { $("td:eq(0)", nRow).css("font-weight", "bold"); }'))
bold = paste0(bold,'}')
return(bold)
})
output$mytable = renderDataTable({
diamonds[,1:ifelse(input$checkbox1,6,5)]
}, options = list(fnRowCallback = I(bold()),aaSorting=list(list(2, ifelse(input$checkbox2,"asc","desc"))))
)
}
)
# ui.R
shinyUI({
pageWithSidebar(
h1('Diamonds DataTable with TableTools'),
tagList(
singleton(tags$head(tags$script(src='jquery.dataTables.min.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='TableTools.min.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='dataTables.colReorder.min.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='colvis.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='ZeroClipboard.min.js',type='text/javascript'))),
singleton(tags$head(tags$link(href='TableTools.min.css',rel='stylesheet',type='text/css'))),
singleton(tags$head(tags$link(href='ColVis.css',rel='stylesheet',type='text/css')))
, tags$head(
tags$style(HTML("
.cvclear{
text-align:right}")
)
)
),
tabPanel("foo",
checkboxInput("checkbox1", "add one more column", FALSE),
checkboxInput("checkbox2", "sort [desc] or [asc]", FALSE),
checkboxInput("checkbox3", "no bold", FALSE),
dataTableOutput("mytable")
)
)
})
The option is declared as:
"aaSorting": [[2,'asc']]
which in R is
options = list(aaSorting = list(list(2, 'asc')))
so you need to swap your c for a list. However the logic is wrong here.
If sorting is enabled, then DataTables will perform a first pass sort on initialisation. You can define which column(s) the sort is performed upon, and the sorting direction, with this variable. The aaSorting array should contain an array for each column to be sorted initially containing the column's index and a direction string ('asc' or 'desc').
So this option will only work on the initialisation of the table. I dont think Shiny reinitialises the table once it has been formed so this wont work as a reactive only working the first time the table is initialised. Your more complex example may work however and it may just be the list(list(...)) that is the problem.