英文:
Trouble with nested shiny module responding to action button click
问题
我在使用嵌套的Shiny模块与动作按钮时遇到问题。
我希望在点击时嵌套的模块会在控制台上打印。我创建了一个示例来说明这个问题。
library(shiny)
ui_2 <- function(id){
ns <- NS(id)
actionButton(ns('go'), 'go')
}
server_2 <- function(id) {
moduleServer(id, function(input, output, session){
observeEvent(input$go, print('working!'))
})
}
ui_1 <- function(id) {
ns <- NS(id)
fluidRow(
ui_2(ns('two'))
)
}
server_1 <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- NS(id)
server_2(ns('two'))
})
}
ui <- fluidPage(
ui_1('one')
)
server <- function(input, output, session) {
server_1('one')
}
shinyApp(ui, server)
非常感谢任何帮助!
英文:
I am having problems getting nested shiny modules with action buttons to respond.
I want the nested module to print to the console when clicked. I created a toy example to illustrate the problem.
library(shiny)
ui_2 <- function(id){
ns <- NS(id)
actionButton(ns('go'), 'go')
}
server_2 <- function(id) {
moduleServer(id, function(input, output, session){
observeEvent(input$go, print('working!'))
})
}
ui_1 <- function(id) {
ns <- NS(id)
fluidRow(
ui_2(ns('two'))
)
}
server_1 <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- NS(id)
server_2(ns('two'))
})
}
ui <- fluidPage(
ui_1('one')
)
server <- function(input, output, session) {
server_1('one')
}
shinyApp(ui, server)
Any help is greatly appreciated!
答案1
得分: 0
The issue is that you wrapped the ID inside ns()
in the first module server, i.e. do server_2("two")
instead of server_2(ns('two'))
.
英文:
The issue is that you wrapped the ID inside ns()
in the first module server, i.e. do server_2("two")
instead of server_2(ns('two'))
.
library(shiny)
ui_2 <- function(id) {
ns <- NS(id)
actionButton(ns("go"), "go")
}
server_2 <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$go, print("working!"))
})
}
ui_1 <- function(id) {
ns <- NS(id)
fluidRow(
ui_2(ns("two"))
)
}
server_1 <- function(id) {
moduleServer(id, function(input, output, session) {
server_2("two")
})
}
ui <- fluidPage(
ui_1("one")
)
server <- function(input, output, session) {
server_1("one")
}
shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论