目标:
我想在 DT 表中选择一行,切换选项卡,并将 pickerInput 值更新为表行中的值。
问题:
我可以很好地切换标签(感谢另一篇关于使用父会话的建议);但是,我似乎无法弄清楚如何让 updatePickerInput 正常工作。我认为 session 和 inputId 参数存在问题。updatePickerInput 的会话不是父会话,而是更像来自 updateTabItem 调用的“子”会话,这是有道理的。
代表:
下面是一个单独的文件 app.r 脚本,它具有两个面板的模块作为全局函数。
会话信息():
R 版本 4.0.3 (2020-10-10)
平台:x86_64-apple-darwin17.0(64位)
运行于:macOS Big Sur 10.16
# Dependencies ----
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)
library(DT)
# Picker Module ----
# Picker UI
picker_ui <- function(id){
box(width = 12
, uiOutput(NS(id, "picker"))
)
}
# Picker Server
picker_server <- function(id){
moduleServer(id, function(input, output, session){
# render pickerInput
output$picker <- renderUI({
ls_choices <- c("One", "Two", "Three")
pickerInput(NS(id, "pickerInput")
, choices = ls_choices
, selected = NULL)
})
})
}
# Table Module ----
# Table UI
table_ui <- function(id){
fluidPage(
box(width = 12
, DTOutput(NS(id, "table"))
)
)
}
# Table Server
table_server <- function(id, parent){
moduleServer(id, function(input, output, session){
output$table <- renderDT({
data <- c("One", "Two", "Three")
df <- tibble("Labels" = data)
datatable(df, rownames = F, selection = "single", options = list(dom = 'tip'))
})
observeEvent(input$table_cell_clicked, {
req(input$table_cell_clicked$value)
updateTabItems(session = parent, inputId = "tabs", selected = "picker")
################
#### ISSUE #####
################
updatePickerInput(session = session, inputId = "pickerInput", selected = input$table_cell_clicked$value)
})
})
}
# Shiny App----
# UI
ui <- dashboardPage(
dashboardHeader(title = "Demo")
, dashboardSidebar(
sidebarMenu(
id = "tabs"
, menuItem("Table", tabName = "table")
, menuItem("Picker Input", tabName = "picker")
)
)
, dashboardBody(
tabItems(
tabItem(tabName = "table"
, table_ui("table")
)
, tabItem(tabName = "picker"
, picker_ui("picker")
)
)
)
)
# Server
server <- function(input, output, session) {
table_server("table", session)
picker_server("picker")
}
# Run App ----
shinyApp(ui, server)