【问题标题】:selectInput not working when variables are selected选择变量时selectInput不起作用
【发布时间】:2019-08-03 04:51:00
【问题描述】:

希望只是一个简单的问题,我已在代码中添加了 selectInput 函数并将其链接到服务器,但是每当我在应用程序中更改“年份”时,散点图不会根据年。

我错过了一些代码吗?

library(shiny)
library(ggplot2)
pigs <- read.csv("pigs_data.csv")
# Define UI for application 
ui <- fluidPage(
# Application title
titlePanel("Pig Breeding"),
sidebarLayout( 
sidebarPanel(
  #Allows user to choose a year which changes the distribution of plot points
selectInput(inputId = "year",
            label = "Choose a year:",
            choices = c(2016, 2017, 2018),
            selectize = FALSE
            )
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("scatterplot")
)
)
)
# Define server logic 
server <- function(input, output) {
output$scatterplot <- renderPlot({
                    input$year
                    ggplot(pigs, 
                    aes(x = sow_count, y = species, col = species)) + 
                    geom_point() +
                    facet_grid(. ~year)
})
}
# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    编辑 - 在尝试 observeEvent 解决方案之前:

    根据您想要绘制的具体内容,可能是facet_grid(. ~year) 而不是facet_grid(. ~input$year)

    如果facet_grid(. ~input$year) 不是您想要的,那么... 你可以试试闪亮包中的observeEvent

    observeEvent(input$year, {
        output$scatterplot <- renderPlot({
          input$year
          ggplot(pigs, 
                 aes(x = sow_count, y = species, col = species)) + 
            geom_point() +
            facet_grid(. ~year)
        })
    })
    

    基本上,只要对象input$year 发生变化,您就会渲染一个新图。

    您的示例将如下所示:

    library(shiny)
    library(ggplot2)
    pigs <- read.csv("pigs_data.csv")
    # Define UI for application 
    ui <- fluidPage(
      # Application title
      titlePanel("Pig Breeding"),
      sidebarLayout( 
        sidebarPanel(
          #Allows user to choose a year which changes the distribution of plot points
          selectInput(inputId = "year",
                      label = "Choose a year:",
                      choices = c(2016, 2017, 2018),
                      selectize = FALSE
          )
        ),
        # Show a plot of the generated distribution
        mainPanel(
          plotOutput("scatterplot")
        )
      )
    )
    # Define server logic 
    server <- function(input, output) {
      observeEvent(input$year, {
        output$scatterplot <- renderPlot({
          input$year
          ggplot(pigs, 
                 aes(x = sow_count, y = species, col = species)) + 
            geom_point() +
            facet_grid(. ~year)
        })
      })
    }
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    我希望这对你有用:)

    【讨论】:

      【解决方案2】:

      我认为你需要更新你的表 pigs 如果它包含变量 year 像这样:

      server <- function(input, output) {
      output$scatterplot <- renderPlot({
                          input$year
                          ggplot(pigs %>% filter(year %in% input$year), 
                          aes(x = sow_count, y = species, col = species)) + 
                          geom_point() +
                          facet_grid(. ~year)
      })
      }
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-04-22
        • 2014-12-15
        • 2020-04-19
        • 1970-01-01
        • 2013-01-29
        • 2021-07-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多