有关嵌套Shiny模块响应动作按钮点击的问题。

huangapple go评论58阅读模式
英文:

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 &lt;- function(id){
  ns &lt;- NS(id)
  actionButton(ns(&#39;go&#39;), &#39;go&#39;)
}
server_2 &lt;- function(id) {
  moduleServer(id, function(input, output, session){
    observeEvent(input$go, print(&#39;working!&#39;))
  })
}

ui_1 &lt;- function(id) {
  ns &lt;- NS(id)
  fluidRow(
    ui_2(ns(&#39;two&#39;))
  )
    
  
}
server_1 &lt;- function(id) {
  moduleServer(id, function(input, output, session) {
    ns &lt;- NS(id)
    server_2(ns(&#39;two&#39;))
  }) 
}


ui &lt;- fluidPage(
  ui_1(&#39;one&#39;)
)

server &lt;- function(input, output, session) {
  server_1(&#39;one&#39;)
}

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(&quot;two&quot;) instead of server_2(ns(&#39;two&#39;)).

library(shiny)

ui_2 &lt;- function(id) {
  ns &lt;- NS(id)
  actionButton(ns(&quot;go&quot;), &quot;go&quot;)
}

server_2 &lt;- function(id) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$go, print(&quot;working!&quot;))
  })
}

ui_1 &lt;- function(id) {
  ns &lt;- NS(id)
  fluidRow(
    ui_2(ns(&quot;two&quot;))
  )
}
server_1 &lt;- function(id) {
  moduleServer(id, function(input, output, session) {
    server_2(&quot;two&quot;)
  })
}


ui &lt;- fluidPage(
  ui_1(&quot;one&quot;)
)

server &lt;- function(input, output, session) {
  server_1(&quot;one&quot;)
}

shinyApp(ui, server)

有关嵌套Shiny模块响应动作按钮点击的问题。

huangapple
  • 本文由 发表于 2023年6月1日 07:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定