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

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

Trouble with nested shiny module responding to action button click

问题

我在使用嵌套的Shiny模块与动作按钮时遇到问题。

我希望在点击时嵌套的模块会在控制台上打印。我创建了一个示例来说明这个问题。

  1. library(shiny)
  2. ui_2 <- function(id){
  3. ns <- NS(id)
  4. actionButton(ns('go'), 'go')
  5. }
  6. server_2 <- function(id) {
  7. moduleServer(id, function(input, output, session){
  8. observeEvent(input$go, print('working!'))
  9. })
  10. }
  11. ui_1 <- function(id) {
  12. ns <- NS(id)
  13. fluidRow(
  14. ui_2(ns('two'))
  15. )
  16. }
  17. server_1 <- function(id) {
  18. moduleServer(id, function(input, output, session) {
  19. ns <- NS(id)
  20. server_2(ns('two'))
  21. })
  22. }
  23. ui <- fluidPage(
  24. ui_1('one')
  25. )
  26. server <- function(input, output, session) {
  27. server_1('one')
  28. }
  29. 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.

  1. library(shiny)
  2. ui_2 &lt;- function(id){
  3. ns &lt;- NS(id)
  4. actionButton(ns(&#39;go&#39;), &#39;go&#39;)
  5. }
  6. server_2 &lt;- function(id) {
  7. moduleServer(id, function(input, output, session){
  8. observeEvent(input$go, print(&#39;working!&#39;))
  9. })
  10. }
  11. ui_1 &lt;- function(id) {
  12. ns &lt;- NS(id)
  13. fluidRow(
  14. ui_2(ns(&#39;two&#39;))
  15. )
  16. }
  17. server_1 &lt;- function(id) {
  18. moduleServer(id, function(input, output, session) {
  19. ns &lt;- NS(id)
  20. server_2(ns(&#39;two&#39;))
  21. })
  22. }
  23. ui &lt;- fluidPage(
  24. ui_1(&#39;one&#39;)
  25. )
  26. server &lt;- function(input, output, session) {
  27. server_1(&#39;one&#39;)
  28. }
  29. 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;)).

  1. library(shiny)
  2. ui_2 &lt;- function(id) {
  3. ns &lt;- NS(id)
  4. actionButton(ns(&quot;go&quot;), &quot;go&quot;)
  5. }
  6. server_2 &lt;- function(id) {
  7. moduleServer(id, function(input, output, session) {
  8. observeEvent(input$go, print(&quot;working!&quot;))
  9. })
  10. }
  11. ui_1 &lt;- function(id) {
  12. ns &lt;- NS(id)
  13. fluidRow(
  14. ui_2(ns(&quot;two&quot;))
  15. )
  16. }
  17. server_1 &lt;- function(id) {
  18. moduleServer(id, function(input, output, session) {
  19. server_2(&quot;two&quot;)
  20. })
  21. }
  22. ui &lt;- fluidPage(
  23. ui_1(&quot;one&quot;)
  24. )
  25. server &lt;- function(input, output, session) {
  26. server_1(&quot;one&quot;)
  27. }
  28. 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:

确定