观察在另一个模块中发生的动作

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

oberveEvent on an action located in another module

问题

在一个闪亮的应用程序中,我有两个模块,在第一个模块(mod_1_ui)中我有筛选器和一个按钮。在模块2中,我希望在按钮被点击时显示一个绘图。

我的问题是,observeEvent位于模块2中,而按钮位于模块1中。如何观察两个不同模块中的按钮点击事件?

以下是一个可重现的示例:

mod_1_ui = function(id) {
  ns = NS(id)
  
  fluidRow(
    column(
      width = 3,
      radioButtons(
        ns("dist"),
        "Distribution type:",
        c(
          "Normal" = "norm",
          "Uniform" = "unif",
          "Log-normal" = "lnorm",
          "Exponential" = "exp"
        )
      ),
      actionButton(
        inputId = ns("btn_go"),
        label = "Go !",
        icon = icon("play")
      )
    ),
  
    column(width = 9,
           mod_2_ui(ns("display_plot"))
         )
  )
}

mod_1_server = function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    mod_2_server("display_plot")
  })
}

mod_2_ui = function(id) {
  ns = NS(id)

  plotOutput(outputId = ns("plot"))
}

mod_2_server = function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    
    observeEvent(input$btn_go,{
      output$plot <- renderPlot({
        plot(mtcars$mpg)
      })
    })
    
  })
}

ui <- fluidPage(
  mod_1_ui("mod")
)

server <- function(input, output, session) {
  r_global <- reactiveValues()

  mod_1_server("mod", r_global = r_global)
}

if (interactive())
  shinyApp(ui, server)

观察在另一个模块中发生的动作

英文:

In a shiny app, I have 2 modules, in the first one (mod_1_ui) I have filters and an actionButton.
On the module 2, I want to display a plot WHEN button is clicked.

My problem is that, the observeEvent is in module 2 and the button in module 1.
How can I observeEvent an action in 2 differents modules ?
观察在另一个模块中发生的动作

Below is a reproducible example :

mod_1_ui = function(id) {
  ns = NS(id)
  
  fluidRow(
    column(
    width = 3,
    radioButtons(
      ns(&quot;dist&quot;),
      &quot;Distribution type:&quot;,
      c(
        &quot;Normal&quot; = &quot;norm&quot;,
        &quot;Uniform&quot; = &quot;unif&quot;,
        &quot;Log-normal&quot; = &quot;lnorm&quot;,
        &quot;Exponential&quot; = &quot;exp&quot;
      )
    ),
    actionButton(
      inputId = ns(&quot;btn_go&quot;),
      label = &quot;Go !&quot;,
      icon = icon(&quot;play&quot;)
    )
  ),
  
  column(width = 9,
           mod_2_ui(ns(&quot;display_plot&quot;))
         )
  )
}


mod_1_server = function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    mod_2_server(&quot;display_plot&quot;)
    
  })
}




mod_2_ui = function(id) {
  ns = NS(id)

  plotOutput(outputId = ns(&quot;plot&quot;))
}


mod_2_server = function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    
    observeEvent(input$btn_go,{
      output$plot &lt;- renderPlot({
        plot(mtcars$mpg)
      })
    })
    
  })
}




ui &lt;- fluidPage(
  mod_1_ui(&quot;mod&quot;)
)

server &lt;- function(input, output, session) {
  r_global &lt;- reactiveValues()

  mod_1_server(&quot;mod&quot;, r_global = r_global)
}

if (interactive())
  shinyApp(ui, server)


答案1

得分: 1

以下是代码的翻译部分:

一种选项是使用reactivereactiveVal将按钮的状态作为参数传递给模块2的服务器:

library(shiny)

mod_1_ui <- function(id) {
  ns <- NS(id)

  fluidRow(
    column(
      width = 3,
      radioButtons(
        ns("dist"),
        "Distribution type:",
        c(
          "Normal" = "norm",
          "Uniform" = "unif",
          "Log-normal" = "lnorm",
          "Exponential" = "exp"
        )
      ),
      actionButton(
        inputId = ns("btn_go"),
        label = "Go !",
        icon = icon("play")
      )
    ),
    column(
      width = 9,
      mod_2_ui(ns("display_plot"))
    )
  )
}

mod_1_server <- function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    show <- reactive({
      input$btn_go
    })
    
    mod_2_server("display_plot", show = show)
  })
}

mod_2_ui <- function(id) {
  ns <- NS(id)

  plotOutput(outputId = ns("plot"))
}

mod_2_server = function(id, r_global, show) {
  moduleServer(id, function(input, output, session) {
    
    observeEvent(show(),{
      output$plot <- renderPlot({
        plot(runif(10, runif(10)))
      })
    })
    
  })
}

ui <- fluidPage(
  mod_1_ui("mod")
)

server <- function(input, output, session) {
  r_global <- reactiveValues()

  mod_1_server("mod", r_global = r_global)
}

if (interactive()) {
  shinyApp(ui, server)
}
英文:

One option would be to use a reactive or a reactiveVal to pass the state of your button as an argument to the module 2 server:

library(shiny)

mod_1_ui &lt;- function(id) {
  ns &lt;- NS(id)

  fluidRow(
    column(
      width = 3,
      radioButtons(
        ns(&quot;dist&quot;),
        &quot;Distribution type:&quot;,
        c(
          &quot;Normal&quot; = &quot;norm&quot;,
          &quot;Uniform&quot; = &quot;unif&quot;,
          &quot;Log-normal&quot; = &quot;lnorm&quot;,
          &quot;Exponential&quot; = &quot;exp&quot;
        )
      ),
      actionButton(
        inputId = ns(&quot;btn_go&quot;),
        label = &quot;Go !&quot;,
        icon = icon(&quot;play&quot;)
      )
    ),
    column(
      width = 9,
      mod_2_ui(ns(&quot;display_plot&quot;))
    )
  )
}

mod_1_server &lt;- function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    show &lt;- reactive({
      input$btn_go
    })
    
    mod_2_server(&quot;display_plot&quot;, show = show)
  })
}

mod_2_ui &lt;- function(id) {
  ns &lt;- NS(id)

  plotOutput(outputId = ns(&quot;plot&quot;))
}

mod_2_server = function(id, r_global, show) {
  moduleServer(id, function(input, output, session) {
    
    observeEvent(show(),{
      output$plot &lt;- renderPlot({
        plot(runif(10, runif(10)))
      })
    })
    
  })
}

ui &lt;- fluidPage(
  mod_1_ui(&quot;mod&quot;)
)

server &lt;- function(input, output, session) {
  r_global &lt;- reactiveValues()

  mod_1_server(&quot;mod&quot;, r_global = r_global)
}

if (interactive()) {
  shinyApp(ui, server)
}

观察在另一个模块中发生的动作

huangapple
  • 本文由 发表于 2023年3月4日 03:38:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631246.html
匿名

发表评论

匿名网友

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

确定