选择变量时,selectInput不起作用


大黑骡子王
2025-03-01 07:36:08 (17天前)
  1. 希望只是一个简单的问题,我已经在我的代码中添加了一个selectInput函数并将其链接到服务器,但是每当我更改应用程序中的“年份”时,散点图都不会改变...

3 条回复
  1. 0# 十二* | 2019-08-31 10-32



    编辑 - 在尝试之前

    observeEvent

    解:



    根据你想要绘制的确切内容,可能会出现这种情况

    facet_grid(. ~year)

    并不是

    facet_grid(. ~input$year)




    如果

    facet_grid(. ~input$year)

    不是你想要的,那么……
    你可以尝试一下

    observeEvent

    从闪亮的包装:




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

    2. </code>


    基本上每当对象

    input$year

    更改,您渲染一个新的情节。



    您的示例将如下所示:




    1. library(shiny)
      library(ggplot2)
      pigs <- read.csv(“pigs_data.csv”)

    2. Define UI for application

      ui <- fluidPage(

    3. Application title

      titlePanel(“Pig Breeding”),
      sidebarLayout(
      sidebarPanel(

    4.   #Allows user to choose a year which changes the distribution of plot points
    5.   selectInput(inputId = "year",
    6.               label = "Choose a year:",
    7.               choices = c(2016, 2017, 2018),
    8.               selectize = FALSE
    9.   )
    10. ),
    11. # Show a plot of the generated distribution
    12. mainPanel(
    13.   plotOutput("scatterplot")
    14. )
    15. )
      )

    16. 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)
      })
      })
      }

    17. Run the application

      shinyApp(ui = ui, server = server)

    18. </code>


    我希望这适合你:)


  2. 1# 小它.Little it | 2019-08-31 10-32



    我认为你需要更新你的表


    </强>
    如果它包含变量




    </强>
    像这样:




    1. 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)
      })
      }

    2. </code>


    希望这有帮助。


登录 后才能参与评论