【问题标题】:Passing Plots from inside a renderUI in a Shiny Module to main Server将绘图从 Shiny 模块中的 renderUI 内部传递到主服务器
【发布时间】:2021-06-09 13:43:06
【问题描述】:

我正在尝试了解如何将信息从模块传递到 Shiny App 的主服务器。这是对我的实际代码的过度简化,所以我知道它可以以不同的方式完成,但我需要主要使用 server.R 文件中的callModule 来完成此操作。

# Mod1.R File
modUI <- function(id) {

ns <- NS(id)

  tagList(
    fluidRow(
      column(
        width = 12,
        numericInput(ns("num"), "Choose a number to plot", value = 3),
        uiOutput(ns("bins"))
      )
    )
  )
}

modServer <- function(input, output, session) {
  
  ns <- session$ns
  
  output$bins <- renderUI(
    ns <- session$ns,
    selectInput(ns("plot_type"), "select plot", c("hist", "plot")),
    plotOutput(ns("plott"))
  )
  
  output$plott <- renderPlot(
    if (input$plot_type == "hist"){
      hist(input$num)
    } else (
      plot(input$num)
    )
  )

}

##############

# App.R File

library(shiny)
library(tidyverse)

# Modules
source("mod1.R")

    # Main App ----------------------------------------------------------------
    
    ui <- fluidPage(
      modUI("ssss")
    )  # Fluid Page
    
    
    server <- function(input, output, session) {
      callModule(modServer, "ssss")
    }
    
    
    shinyApp(ui, server)

我正在尝试将应该在 Mod1.R 文件中生成的绘图返回到服务器函数中的 App.R 文件,但我不太确定如何执行此操作。我知道我应该在 Mod1.R 文件中返回一个响应式输出,例如:return(reactive(output$plott)),但这没有任何作用。你能指导我正确的方向吗?谢谢。

【问题讨论】:

    标签: r shiny shinymodules renderui


    【解决方案1】:

    我不确定您所说的“将情节...返回给应用程序”是什么意思。如果您只想显示绘图,那么这似乎可以解决您的代码中的问题:

    # Mod1.R File
    modUI <- function(id) {
      
      ns <- NS(id)
      
      tagList(
        fluidRow(
          column(
            width = 12,
            numericInput(ns("num"), "Choose a number to plot", value = 3),
            uiOutput(ns("bins"))
          )
        )
      )
    }
    
    modServer <- function(input, output, session) {
      
      ns <- session$ns
      
      output$bins <- renderUI({
        tagList(
          selectInput(ns("plot_type"), "select plot", c("hist", "plot")),
          plotOutput(ns("plott"))
        )
      })
      
      output$plott <- renderPlot(
        if (input$plot_type == "hist"){
          hist(input$num)
        } else (
          plot(input$num)
        )
      )
      
    }
    
    ##############
    
    # App.R File
    
    library(shiny)
    library(tidyverse)
    
    # Modules
    
    # Main App ----------------------------------------------------------------
    
    ui <- fluidPage(
      modUI("ssss")
    )  # Fluid Page
    
    
    server <- function(input, output, session) {
      callModule(modServer, "ssss")
    }
    
    
    shinyApp(ui, server)
    

    如果您真正想要返回情节而不是简单地显示它,那么您需要创建一个包含情节的反应式在您的 output$plott 反应之外,然后从模块 UI 返回该反应(不是它的值)。比如:

    modServer <- function(input, output, session) {
      
      ns <- session$ns
      
      output$bins <- renderUI({
        tagList(
          selectInput(ns("plot_type"), "select plot", c("hist", "plot")),
          plotOutput(ns("plott"))
        )
      })
      
      myPlot <- reactive({
        if (input$plot_type == "hist"){
          hist(input$num)
        } else (
          plot(input$num)
        )
      })
      
      output$plott <- renderPlot({
        myPlot()
      })
      
      return(myPlot)
    }
    

    server <- function(input, output, session) {
      mainServerPlot <- callModule(modServer, "ssss")
    }
    

    然后您可以在主服务器中使用mainServerPlot() 引用模块返回的绘图对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-19
      • 2021-06-04
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      相关资源
      最近更新 更多