DataTable行选择闪亮模块内部

时间:2016-06-16 14:48:58

标签: r shiny dt

我正在尝试使用这个基本的工作闪亮应用程序并模块化它。这有效:

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

我想让这个模块适用于任何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()) 
  }
)

这让我错误地说我试图在反应环境之外使用反应性。如果我删除了return语句,它就会运行。无论如何返回从闪亮模块中的数据表中选择的行?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

所需

return(reactive(input$testTable_rows_selected))
相关问题