Rstudio shiny select row in DataTables? - r

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

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)

Hide certain columns in a responsive data table using DT package

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.

Get Selected Row From DataTable in Shiny App

I want to modify this application:
https://demo.shinyapps.io/029-row-selection/
so that only one row can be selected at a time, and so that I can acquire the item in the first column of the selected row to plot data with. Does anyone know how to do this?
UPDATE: you can now access the selected rows using input$tableId_rows_selected in server.R. See here for more details.
To select a unique row, you can change the callback function of your example to this:
callback = "function(table) {
table.on('click.dt', 'tr', function() {
table.$('tr.selected').removeClass('selected');
$(this).toggleClass('selected');
Shiny.onInputChange('rows',
table.rows('.selected').data()[0][0]);
});
}"
When you click on a row,it basically removes any selected rows (they have the .selected class) and selects the row you clicked on.
I also changed the code in the Shiny.onInputChange function so that it returns the number in the first column.
The R method which renders a DataTable has a parameter which defines the selection mode. For example:
output$table1 <-
DT::renderDataTable(dataSet,
selection = 'single')
Possible values are ('multiple' is the default):
none
single
multiple
For further reference you can see: http://rstudio.github.io/DT/shiny.html
EDIT 04/14/2016
In the setup I use working with single selection mode has issues.
Here is what version I use:
> DT:::DataTablesVersion
[1] "1.10.7"
> packageVersion("DT")
[1] ‘0.1’
The problem I faced is that visually you have a single row selection but when you do:
observeEvent(input$table1_rows_selected, {
str(input$table1_rows_selected)
})
You will get a list with all rows which were selected but were not explicitly deselected. In other words selecting a new row does not automatically deselect the previous row in the internal Datatables logic. This might be also due to the DT wrapper, not sure.
This is why currently as a workaround we use JS for this:
$(document).on('click', '#table1 table tr', function() {
var selectedRowIds = $('#table1 .dataTables_scrollBody table.dataTable').DataTable().rows('.selected')[0];
var selectedId = "";
if (selectedRowIds.length === 1) {
selectedId = $(this).children('td:eq(0)').text();
} else {
$('#table1 tbody tr').removeClass('selected');
}
Shiny.onInputChange("table1_selected_id", selectedId);
});
Once you have this in place you will be able to do:
observeEvent(input$table1_selected_id, {
str(input$table1_selected_id)
})
This now at least sends correct data to your server.R code. Unfortunately you will still have an issue with the table because internally it keeps track on which rows were selected and if you switch pages it will restore a wrong selection. But at least this is purely a visual defect and your code will have the chance to function properly. So this solution actually needs more work.
You can use this:
output$data_table <- renderDataTable(data(),options = list(pageLength = 9))
Then to get the selected row:
selected <- input$data_table_rows_selected
Then to get cells from that row, you can use (assuming that time is the name of the col you are trying to get in this case):
time_x = data()[selected, "time"]
Selected is the index for the selected rows, so you need to use that index along with the col name.
The below code display a dataframe in DT table format. Users will be able to select single row. The selected row is retrieved and displayed. you can write your plot function in the plot block in the server.
I hope this helps !!
# Server.R
shinyServer(function(input, output,session) {
output$sampletable <- DT::renderDataTable({
sampletable
}, server = TRUE,selection = 'single')
output$selectedrow <- DT::renderDataTable({
selectedrowindex <<- input$sampletable_rows_selected[length(input$sampletable_rows_selected)]
selectedrowindex <<- as.numeric(selectedrowindex)
selectedrow <- (sampletable[selectedrowindex,])
selectedrow
})
output$plots <- renderPlot({
variable <- sampletable[selectedrowindex,1]
#write your plot function
})
})
#ui.R
shinyUI(navbarPage( "Single Row Selection",
tabPanel("Row selection example",
sidebarLayout(sidebarPanel("Parameters"),
mainPanel(
DT::dataTableOutput("selectedrow"),
DT::dataTableOutput("sampletable")
))
)
))
# global.R
library(DT)
library(shiny)
selectedrowindex = 0

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