【问题标题】:Create an arbitrary number of plots in shiny module在闪亮模块中创建任意数量的图
【发布时间】:2022-07-12 00:53:55
【问题描述】:

我目前正在为不同的模型开发一个闪亮的模块(使用已经创建的应用程序)。 困难之一是调整模块以处理要绘制的不同数量的变量。 应用程序中显示的变量名称是模块的参数。 我已经使用这个例子https://gist.github.com/wch/5436415 来编写我的代码。 这是迄今为止我所做的一个可复制的示例。

modeleServer <- function(id,variables){
  moduleServer(id,function(input, output,session){
    ns <- session$ns
    # Insert the right number of plot output objects into the web page
    output$plots <- renderUI({
      plot_output_list <- lapply(1:length(variables), function(i) {
        ns <- session$ns
        box(title=paste("graphe de ",variables[i],sep=" "),status="info",width=6,
            plotOutput(ns(paste("plot", variables[i], sep=""))))
      })
      
      # Convert the list to a tagList - this is necessary for the list of items
      # to display properly.
      do.call(tagList, plot_output_list)
    }) 
    
    observe({
      
      for (i in (1:length(variables))) {
        # Need local so that each item gets its own number. Without it, the value
        # of i in the renderPlot() will be the same across all instances, because
        # of when the expression is evaluated.
        local({
          my_i <- i
          plotname <- paste("plot", variables[my_i], sep="")
          
          output[[plotname]] <- renderPlot({
            ggplot(airquality)+
              geom_line(aes(x=Day,y=airquality[[paste0(variables[i])]]),col='blue',size=0.4)+
              theme_classic()+
              scale_x_continuous(expand = c(0, 0), limits = c(0,NA)) +
              scale_y_continuous(expand = c(0, 0), limits = c(0, NA))+
              theme(legend.position = "none") +
              ggtitle(paste0("graphe de ",variables[i]))
          })
        })
      }
    })
  })
}

modeleUI <-function(id){
  ns <-NS(id)
  tagList(

    uiOutput(ns("plots"))
  )
}

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("App using module"),
    modeleUI("test")
    
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  modeleServer(id="test",variables=c("Ozone","Wind"))
}

# Run the application 
shinyApp(ui = ui, server = server)

我的问题是显示的 ggplots 都是相同的(最后一个创建的),即使创建了不同的框并且我不知道出了什么问题。 感谢您的建议!

【问题讨论】:

  • 这几乎可以肯定是一个惰性求值问题。另外,我建议不要使用scale_,x|y&gt;_xxxx()limits 参数。它可能导致难以检测的意外错误。恕我直言,最好使用coord_cartesian。例如,请参阅here
  • 你可以试试 lapply 代替循环。
  • 但是由于您使用模块,您可以为每个模块绘制一个图。没有?

标签: r shiny module


【解决方案1】:

这适用于lapply

modeleServer <- function(id, variables) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    # Insert the right number of plot output objects into the web page
    output$plots <- renderUI({
      plot_output_list <- lapply(1:length(variables), function(i) {
        ns <- session$ns
        box(
          title = paste("graphe de ", variables[i], sep = " "), 
          status = "info", width = 6,
          plotOutput(ns(paste("plot", variables[i], sep = "")))
        )
      })
      
      # Convert the list to a tagList - this is necessary for the list of items
      # to display properly.
      do.call(tagList, plot_output_list)
    })
    
    lapply(1:length(variables), function(i){
      plotname <- paste("plot", variables[i], sep = "")
      
      output[[plotname]] <- renderPlot({
        ggplot(airquality) +
          geom_line(
            aes(x = Day, y = airquality[[paste0(variables[i])]]), 
            col = "blue", size = 0.4
          ) +
          theme_classic() +
          scale_x_continuous(expand = c(0, 0), limits = c(0, NA)) +
          scale_y_continuous(expand = c(0, 0), limits = c(0, NA)) +
          theme(legend.position = "none") +
          ggtitle(paste0("graphe de ", variables[i]))
      })
    })

  })
  
}

我没有尝试,但也许这也适用于循环和local。但在您的代码中,您使用i 而不是my_i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2019-10-22
    • 2021-07-17
    • 2018-05-11
    • 2023-04-09
    • 2021-11-09
    • 2016-02-19
    相关资源
    最近更新 更多