Using a reactive value as DataTables option

171 views
Skip to first unread message

Muggy

unread,
Oct 1, 2015, 5:37:35 PM10/1/15
to Shiny - Web Framework for R
Hi, I'm running shiny_0.12.2, R 3.2.2, and I have a datatable rendered as follows:

output$ixpivot <- renderDataTable(rv$data() ,options= rv$options)

I'd like an actionButton with id = "mapcolors" to trigger a change in the data being used for the DataTables, as well as options.

piv_data2 <- ix_mapped()[[1]]
hidecols <- ix_mapped()[[2]]

options.pivot <- reactive(list(dom = 'lfrtp', scrollX = "100%", "border-collapse" = "collapse",
                               pageLength = 15, lengthMenu = append(c(1:25), 50)
                               ,createdRow = I(row_names_format)
                               ,language = list(lengthMenu = "Display: _MENU_")))
options.mapped <- reactive(list(dom = 'lfrtp', scrollX = "100%"
                          ,"border-collapse" = "collapse"
                          ,pageLength = 10, lengthMenu = append(c(1:25), 50)
                          ,createdRow = I(color_mapping), columnDefs = hidecols
                          ,language = list(lengthMenu = "Display: _MENU_")))

rv
<- reactiveValues(data = ix_pivot, options = options.pivot)

observeEvent(input$mapcolors, { rv$data <- piv_data2 })
observeEvent(input$mapcolors, { rv$options <- options.mapped })

Here, ix_mapped()[[2]] refers to a function that hides half the columns in the dataset, stored as ix_mapped()[[1]]. 

When I run the app, the table renders. I click the actionButton, and the new dataset piv_data2 is appears as the table - so the button works. However, the options are not updated with options.mapped, despite the fact that I've run the same options separately using options = options.mapped with no problems. Please let me know if you need any more information, any guidance would be appreciated! 

-Muggy

Yihui Xie

unread,
Oct 1, 2015, 7:39:29 PM10/1/15
to Muggy, Shiny - Web Framework for R
I think you need to wrap up the options argument inside datatable(), e.g. change

output$ixpivot <- renderDataTable(rv$data() ,options= rv$options)

to

output$ixpivot <- renderDataTable(datatable(rv$data() ,options= rv$options))

Regards,
Yihui

Muggy

unread,
Oct 1, 2015, 11:32:58 PM10/1/15
to Shiny - Web Framework for R, jhw...@gmail.com
Hi Yihui,

Thanks a million! I was using shiny's native datatables, not DT. After altering the code to play nice with DT, I implemented your change and it worked like a charm!

Jeff
Reply all
Reply to author
Forward
0 new messages