选择变量时,selectInput不起作用

问题描述 投票:1回答:2

希望只是一个简单的问题,我已将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
2个回答
1
投票

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

根据你想要绘制的内容,这可能是因为qazxsw poi而不是qazxsw poi。

如果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)
    })
})

我希望这适合你:)


1
投票

我认为如果它包含变量year,你需要更新你的表猪:

input$year

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.