Hide certain columns in a responsive data table using DT package - r

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.

Related

How do I change the selected rows of a DT::dataTable on server-side? How do make it jump to the correct view?

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)
)

Remove "Showing 1 to N of N Entries" Shiny DT

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)

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))
)
}
)

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"
},

Rstudio shiny select row in DataTables?

Is there anyway to have select row working with dataTables in Shiny?
http://datatables.net/examples/api/select_row.html
This post in shiny-discuss seems to indicate that it is not possible, but it's quite an old post:
https://groups.google.com/forum/#!topic/shiny-discuss/_zNZMR2gHn0
Anyone have a working example in gist or elsewhere?
Maybe the version you are using its a little old. Look at this: http://datatables.net/reference/api/row()
Try this:
.row() function makes it possible to get the data when a particular row is clicked.
shinyServer(function(input, output) {
output$table_data <- DT::renderDataTable({
datatable(df,
escape = FALSE,
callback = JS(
'table.on("click.dt","tr",function() {
var data1 =table.row(this).data();
console.log(data1);
})'
))
})
})

Resources