![](/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.