Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable row selection inside shiny module

Tags:

r

dt

shiny

I am trying to take this basic working shiny app and modularize it. This works:

shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("testTable"),
    verbatimTextOutput("two")
  ),
  server = function(input,output) {
    output$testTable <- DT::renderDataTable({
      mtcars
    }, selection=list(mode="multiple",target="row"))

    output$two <- renderPrint(input$testTable_rows_selected) 
  }
)

I want to make this a module that will work for any data.frame

# UI element of module for displaying datatable
testUI <- function(id) {
  ns <- NS(id)
  DT::dataTableOutput(ns("testTable"))

}
# server element of module that takes a dataframe
# creates all the server elements and returns
# the rows selected
test <- function(input,output,session,data) {
  ns <- session$ns
  output$testTable <- DT::renderDataTable({
    data()
  }, selection=list(mode="multiple",target="row"))

  return(input[[ns("testTable_rows_selected")]])

}

shinyApp(
  ui = fluidPage(
    testUI("one"),
    verbatimTextOutput("two")
  ),
  server = function(input,output) {
    out <- callModule(test,"one",reactive(mtcars))
    output$two <- renderPrint(out()) 
  }
)

This gives me errors saying I am trying to use reactivity outside of a reactive environment. If I eliminate the return statement, it runs. Is there anyway to return the rows selected from a datatable in a shiny module? Any help would be appreciated.

like image 894
Carl Avatar asked Jun 16 '16 14:06

Carl


1 Answers

Needed

return(reactive(input$testTable_rows_selected))
like image 113
Carl Avatar answered Nov 08 '22 07:11

Carl