简体   繁体   English

闪亮的R-下拉列表在使用observeEvent和updateVarSelection时始终重置为列表的第一位

[英]Shiny R - Drop down list constantly resets to first in list when using observeEvent and updateVarSelection

When using observe event and update var selection it resets every time to top in drop down list 使用观察事件并更新var选择时,每次都会重置到下拉列表的顶部

I have a very large data set, when one unit is selected it filters the dataset so the next drop down list only contains parameters related to that unit to plot. 我有一个非常大的数据集,当选择一个单位时,它将过滤数据集,因此下一个下拉列表仅包含与要绘制的该单位有关的参数。 This works except that the drop down list refreshes on all inputs not just the changing of the unit, I want it to refresh only when the unit is changed. 除了下拉列表不仅在单位更改上刷新所有输入之外,它还可以工作,我希望它仅在单位更改时刷新。 ie the drop down list becomes unit a temperature, unit a pressure, unit a level etc when unit a is selected. 即,当选择单位a时,下拉列表将变为单位温度,单位压力,单位水平等。 unit b temperature, unit b pressure etc. b单位温度,b单位压力等

However, if I chose unit b the drop down list becomes unit b temperature, unit b temperature etc with the temperature being the first in the list and automatically shown on the chart, if I click on the drop down list and chose pressure, the chart will briefly show the pressure but then apparently the observe event is triggered, the list resets itself and defaults back to temperature 但是,如果我选择单位b,则下拉列表将变为单位b温度,单位b温度等,其中温度为列表中的第一位,并自动显示在图表上;如果我单击下拉列表并选择压力,则图表会短暂显示压力,但随后显然会触发观察事件,列表会重置自身并默认返回到温度

some portions of code summerized for clarity and not here word for word 为了清楚起见,对代码的某些部分进行了总结,此处不再逐字逐句地介绍

# Define UI for application that plots User Input and filtered data 
 ui <-   navbarPage( "Viewer",
    tabPanel("Chart" , plotOutput(outputId = "newPlot"), 

    box(selectInput("Unit", "Chose Unit",c(a,b,c),selected = NULL)),
                    box(varSelectInput("Parameter", "Select the Parameter 
    to Plot:",TracePlot, selected = NULL )),

   ))




  # Define server logic 
  server <- function(input, output,session) { output$newPlot <- 
   renderPlot({


TracePlot<-filters on unit selected (input$Unit) 

observeEvent(input$Unit, updateVarSelectInput(session,"Parameter", 
"Select the Parameter to Plot:", TracePlot,selected = NULL) )




  ggplot(Trace, aes_string(y=input$Parameter, x=input$Batch_Or_Step)) + 
  geom_point() 
 })

 }


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

Your code hase several issues: 您的代码有几个问题:

  1. Reproducibility: Try to run your code snippet in a refreshed R session before posting 重现性:发布前,请尝试在刷新的R会话中运行您的代码段
    • your alternatives in the unit selector are missing 您在单位选择器中的替代品丢失了
    • you've used a comma at the end of your ui 您在用户界面的末尾使用了逗号
    • You did not define variable Trace 您没有定义变量Trace
    • Any example data are missing to help you directly with your code. 缺少任何示例数据可直接帮助您使用代码。 That's necessary as varSelectInput requires data 这是必需的,因为varSelectInput需要数据
  2. You cannot use TracePlot in ui, so use renderUI 不能使用TracePlot在UI,所以用renderUI
  3. Reactivity: You do not need to use observeEvent inside renderPlot as it's reactive already. 反应性:您无需在renderPlot使用observeEvent ,因为它已经是反应性的。 Instead, use input$Unit directly or to update data based on input$Unit , call a reactive variable created before. 而是直接使用input$Unit或基于input$Unit更新数据,调用之前创建的反应变量。
  4. updateVarSelectInput is redundant if you use the reactive value in varSelectInput("Parameter", ...) already. 如果已经在varSelectInput("Parameter", ...)使用反应性值,则updateVarSelectInput是冗余的。 If you want to update selected , your probably better with observe() outside of renderPlot 如果要更新selected ,最好在renderPlot之外使用observe()

Try this one as a start, I explained what I've changed 首先尝试一下,我解释了我已更改的内容

library(shiny)
library(ggplot2)
# Define UI for application that plots User Input and filtered data 
ui <-   navbarPage(
  "Viewer",
  tabPanel(
    "Chart" , 
    plotOutput(outputId = "newPlot"), 
    box(selectInput("Unit", "Chose Unit", c("a","b","c"), selected = NULL)),
    # uiOutput to render selectInput/varSelectInput in server session using
    # reactive Values
    box(uiOutput("selectParameter"))
  )
)




# Define server logic 
server <- function(input, output,session) { 
  # create reactive
  TracePlot <- reactiveValues()
  # update reactive with your filter
  observe({
    # Do your filtering here and save to TracePlot$filter 
    TracePlot$filter <- input$Unit
  })

  # From ui to server with renderUI - change to varSelectInput again if data 
  # provided
  output$selectParameter <- renderUI({
    req(TracePlot$filter)
    selectInput(
      "Parameter", 
      "Select the Parameter to Plot:",
      input$Unit, 
      selected = NULL 
    )
  })

  # cannot plot without data and Trace
  output$newPlot <- renderPlot({
    req(TracePlot$filter)

    #ggplot(Trace, aes_string(y=input$Parameter, x=input$Batch_Or_Step)) + 
      #geom_point() 
  })

}


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM