【问题标题】:Select plot from a dropdown menu in RShiny从 R Shiny 的下拉菜单中选择绘图
【发布时间】:2021-10-02 06:13:56
【问题描述】:

我正在创建一个shiny 应用程序,用户可以在其中选择plot 进行查看。我使用this question 作为一种方法,但我收到了一个错误。我该如何解决这个问题?

用户界面

library(shiny)
library(shinydashboard)
library(shinythemes) 
library(tidyverse)

ui =   navbarPage("Project ", theme = shinytheme("cyborg")
                  uiOutput("all"),
       tabPanel("Plot",
                      icon = icon("chart-area"),
                      sidebarLayout(sidebarPanel(
                                    selectInput("Plot", "Please select a plot to view:",
                                                 choices = c("Plot-1", "Plot-2")),
                                    submitButton("Submit")),
                      plotOutput(outputId = "Plots",
                                   width = "1024px",
                               height = "768px")
              )))

服务器

server = function(input, output, session) {

data = eventReactive(input$Plot,{
            switch(input$Plot,
                   "Plot-1" = Plot1,
                   "Plot-1" = Plot2)
        })

output$Plots = renderPlot({

             data("midwest", package = "ggplot2") # Sample data

              Plot1 = ggplot(midwest, aes(x=area, y=poptotal)) +
                      geom_point()
              Plot2 =  ggplot(midwest, aes(x=area, y=poptotal)) + geom_point() + 
                       geom_smooth(method="lm")
 
        })
        

}

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

错误

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是一种方法-

    library(shiny)
    library(shinydashboard)
    library(shinythemes)
    
    ui =   navbarPage("Project ", theme = shinytheme("cyborg"),
                      uiOutput("all"),
                      tabPanel("Plot",
                               icon = icon("chart-area"),
                               sidebarLayout(sidebarPanel(
                                 selectInput("Plot", "Please select a plot to view:",
                                             choices = c("Plot-1", "Plot-2")),
                                 actionButton("submit", "Submit")),
                                 plotOutput(outputId = "Plots",
                                            width = "1024px",
                                            height = "768px")
                               )))
    
    server = function(input, output, session) {
      
      observeEvent(input$submit,{
    
          Plot1 = ggplot(midwest, aes(x=area, y=poptotal)) +
            geom_point()
          Plot2 =  ggplot(midwest, aes(x=area, y=poptotal)) + geom_point() + 
            geom_smooth(method="lm")
    
      output$Plots = renderPlot({
        switch(isolate(input$Plot),
               "Plot-1" = Plot1,
               "Plot-2" = Plot2)
      })
      
      })
      
    
      
      
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 在switch语句中调用ggplot不是更高效吗?这样,当只需要返回其中一个时,两个图都被构建。
    • 是的,我认为你是对的,这样会更有效率。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多