首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >selectInput :基于先前的selectInput重置所选值

selectInput :基于先前的selectInput重置所选值
EN

Stack Overflow用户
提问于 2022-03-15 04:15:37
回答 1查看 740关注 0票数 0

在这个问题上有几个问题,包括here,但我仍然不确定我需要修改什么才能正确处理这个问题。

selectInput选项按预期工作,但当我更改第二个selectInput时,它暂时更改为所需的选择,然后自动返回到第一个筛选的选择。

例如,如果为Variable 1选择了“齿轮”,那么Variable 1 choices将正确显示"3、4、5“以确定可能的齿轮选择。如果我选择"5“的齿轮,它会出现,然后回到齿轮"3”作为一个选择。我不知道如何防止这种反应行为。

下面是一个使用mtcar内置数据集的简单的可重复示例:

代码语言:javascript
复制
library(tidyverse)
library(shiny)



# Variables interested in selecting
my_vars <- c("cyl", "gear", "carb")



# UI
ui <- fluidPage(
  
  # Title
  titlePanel("Reprex"),
  
  # Sidebar 
  sidebarLayout(
    sidebarPanel(
                 selectInput("sel_1",
                             "Variable 1",
                             choices  = my_vars,
                             selected = my_vars[[1]],
                             multiple = FALSE
                 ),
                 selectInput("sel_2",
                             "Variable 1 choices",
                             choices  = unique(mtcars[[ my_vars[[1]] ]]),
                             multiple = FALSE
                 )
    ), # sidebarPanel close
    
    # Plot
    mainPanel(
      plotOutput("plot_out")
    )  # mainPanel close
  )    # sidebarLayout close
)      # UI close




# Server
server <- function(input, output, session) {
  
  output$plot_out <- renderPlot({
    
    # Assign inputs
    sel_1 <- input$sel_1
    sel_2 <- input$sel_2
    
    
    # Make drop-down choice of sel_2 dependent upon user input of sel_1
    # *** Must put "shiny::observe" instead of "observe" since "observe" is masked by the Tidy infer package ***
    shiny::observe({
      updateSelectInput(session,
                        "sel_2",
                        choices = sort(unique(mtcars[[sel_1]]))
                        )
    })
    
    
    
    # Data to plot
    my_data <- mtcars %>% 
      filter(.data[[sel_1]] == sel_2)

    
    
    # Plot
    p <- ggplot(my_data, aes(x = factor(.data[[sel_1]]), y = hp)) + geom_point()
    p
    
  })
}




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

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-15 06:06:55

那是因为你的观察者在renderPlot里面。这里没什么大不了的。

代码语言:javascript
复制
server <- function(input, output, session) {

  # Make drop-down choice of sel_2 dependent upon user input of sel_1
  observeEvent(input$sel_1, {
    updateSelectInput(session,
                      "sel_2",
                      choices = sort(unique(mtcars[[input$sel_1]]))
    )
  })
  
  output$plot_out <- renderPlot({
    
    # Assign inputs
    sel_1 <- input$sel_1
    sel_2 <- input$sel_2
    
    # Data to plot
    my_data <- mtcars %>% 
      filter(.data[[sel_1]] == sel_2)
    
    # Plot
    ggplot(my_data, aes(x = factor(.data[[sel_1]]), y = hp)) + geom_point()

  })
}

这里不需要observeEvent而不是observe,因为input$sel_1是观察者中唯一的反应性值,但是我发现observeEvent更易读。

此外,避免加载tidyverse。装了一大堆你不需要的包裹。在这里,dplyrggplot2就足够了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71477000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档