![](/img/trans.png)
[英]Shiny modules: how to access selected_rows with DT::datatables?
[英]Use ConditionalPanel with DT:datatable rows selected in shiny modules
我将我的闪亮应用程序转换为使用闪亮模块时遇到问题。 在我的应用程序中,我有一个ConditionalPanel,其中conditional是DT:datatable_rows_selected的js字符串。 我不明白我需要如何重写此条件才能使用ShinyModule概念。
示例:这是正确的工作(当选择表中的行时-ConditionalPanel已打开):
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)
}
)
但这是行不通的:
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")
}
)
非常奇怪,但是不需要像以前的答案一样构造表的新名称。 正确的-将“ ns”参数设置为条件面板,并且不要触摸js-string作为条件。 此示例工作正确:
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")}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.