简体   繁体   中英

Use ConditionalPanel with DT:datatable rows selected in shiny modules

I have a problem with convert my shiny app to use Shiny Modules. In my app I have a ConditionalPanel where conditional is a js-string to DT:datatable_rows_selected. I don't understand how I need to rewrite this condition to work with ShinyModule conception.

Example: This is work correct (when select rows in table - ConditionalPanel is open):

    library(shiny)
    library(DT)
    shinyApp(
      ui = fluidPage(
        DT::dataTableOutput("testTable"),
        conditionalPanel(condition = "typeof input.testTable_rows_selected  !== 'undefined' && input.testTable_rows_selected.length > 0",
                         verbatimTextOutput("two")                     
                         )
      ),
      server = function(input,output) {
        output$testTable <- DT::renderDataTable(mtcars, selection=list(mode="multiple",target="row"))
        output$two <- renderPrint(input$testTable_rows_selected) 
      }
    )

But this is not work:

library(shiny)
library(DT)
testUI <- function(id) {
  ns <- NS(id)
  tagList(
            DT::dataTableOutput(ns("testTable")),
            conditionalPanel(condition = "typeof input.testTable_rows_selected  !== 'undefined' && input.testTable_rows_selected.length > 0",
                             verbatimTextOutput(ns("two"))
                            )
          )
}

test <- function(input,output,session) {
  ns <- session$ns
  output$testTable <- DT::renderDataTable(mtcars, selection=list(mode="multiple",target="row"))
  output$two <- renderPrint(input$testTable_rows_selected) 
  # return(reactive(input$testTable_rows_selected))
}

shinyApp(
  ui = testUI("one"),
  server = function(input,output) {
    out <- callModule(test,"one")
  }
)

Very strange, but not needed to construct new name of table like previous answer. The correct - set "ns" parameter to conditional panel, and don't touch js-string for condition. This example work correct:

library(shiny)
library(DT)

testUI <- function(id) {
  ns <- NS(id)

  tagList(
      fluidPage(
        DT::dataTableOutput(ns("one")),
        conditionalPanel( 
                          condition = "typeof input.one_rows_selected  !== 'undefined' && input.one_rows_selected.length > 0", 
                          ns=ns, 
                          verbatimTextOutput(ns("two"))
                         )
    )
  )
}

test <- function(input,output,session) 
{
  output$one <- DT::renderDataTable(mtcars, selection=list(mode="multiple",target="row"))
  output$two <- renderPrint(input$one_rows_selected) 
}

shinyApp(
  ui = testUI("p"),
  server = function(input,output,session) {  out <- callModule(test,"p")}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM