在闪亮的应用程序中使用 checkboxGroupInput 在同一张图上绘制多图

Multiplot on the same graph with checkboxGroupInput in a Shiny App

我想在同一张图上绘制一个或多个图,具体取决于我 "checkboxGroupInput" 中选中的框。 在检查了一些关于 SO 的主题之后,我发现一个易于使用的解决方案是在第二个、第三个……图中添加 "add=TRUE"。

这是我的代码的简化副本: Server.R

function(input, output) {

  dataInput <- reactive({
    #I use getMyPlotValues with two parameters. 
    #idFcast is the one which doesn't work.

    m_PARAM$idSite <- input$country
    m_PARAM$idFcast <- input$model[i]

    getMyPlotValues(m_PARAM)
  })  

  output$plot1 <- renderPlot({

                      ## We plot all the models on the same graph.
                      if(length(input$model) > 0)
                      {
                        firstplot<- TRUE 
                        #To put add = TRUE for the second, third... plots

                        for(i in 1:length(input$model))
                        {
                          valueshere <- dataInput()
                          #We pick up the vectors (ts,obs) with the function
                          #And plot them. 
                          if(firstplot)
                          {
                            plot(valueshere$ts,valueshere$obs)
                          }
                          else
                          {
                            plot(valueshere$ts,valueshere$obs,add=TRUE)
                          }
                          firstplot <- FALSE
                        } 

                      }

                  })
  }

现在 UI.R 简化了 :

shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(

  fluidRow(
    column((4),
           selectInput("country", label = h3("Pays"), 
                       choices = list("First" = 2,"Second" = 1), selected = 1)
    )

  fluidRow( 

      #checkboxGroupInput
    column(6, 
           checkboxGroupInput("model", 

                              choices = list("First" = 1,"Second"=2,"Third"=3),
                              selected = 1)
    )


  )

),

mainPanel
(

plotOutput("plot1")
)
  )
)

)

我发现在这个主题 (How can I pass data between functions in a Shiny app) 上,我可以尝试通过在此处编写 valueshere <- dataInput()$i 来传递变量 "i"... 因为我觉得这是错误来自哪里。我尝试显示 input$model 并且它有效,input$model[i] 没有。

我的代码肯定有问题,因为我是 Shiny 的初学者,但我无法修复,即使所有关于 Shiny 的主题都在 SO 上。

感谢您的帮助,祝您有愉快的一天!

似乎 add 论点不起作用。我完成了解决方案,该解决方案包括为所有模型创建绘图,并且仅在选择值时显示它们。所以这里是:

我已尝试使您的示例可重现,因此您会观察到一些变化

UPDATE:假设你的 SQL 语句会给你一个带有模型名称的向量(models_list),你可以使用 lapply(比如here

library(shiny)
# Simulate data
set.seed(189)
df <- data.frame("obs" = 1:30, "ts" = (rnorm(30)+100)
                 , "country" = rep(c("First", "Second"), 15)
                 , "models" = rep(c("First", "Second", "Third"), 10),
                 stringsAsFactors = F)

# Suppose your SQL query returns this kind of output
models_list <- c("First", "Second", "Third")
models_num <- length(models_list)

# Run App
shiny::runApp(list(

  ui = fluidPage(

    sidebarLayout(
      sidebarPanel(
        selectInput("country", label = "Countries", 
                    choices = c("First", "Second"), selected =  "First"),
        checkboxGroupInput("model", label = "Models",
                           choices = c("First", "Second", "Third"),
                           selected = "First")
      ),

      mainPanel(

        lapply(1:models_num, function(i) {
          plotOutput(paste0('plot', i))
        })

      )
    )

  ),
  server = function(input, output) {

    dataInput <- reactive({
      df_out <- df[(df$country == input$country), ]
      return(df_out)
    })  

    lapply(1:models_num, function(i) {
      output[[paste0('plot', i)]] <- renderPlot({
        if(any(input$model %in% models_list[i])){
          valueshere <- dataInput()[df$models == models_list[i],]
          plot(valueshere$ts,valueshere$obs)
        }
      })
    })

  }
))