0

When using observe event and update var selection it resets every time to top in drop down list

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. unit b temperature, unit b pressure etc.

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

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)
kjg2125
  • 3
  • 2
  • See this thread for a great explanation with examples on creating reactive UI elements: https://stackoverflow.com/questions/21465411/r-shiny-passing-reactive-to-selectinput-choices/21467399#21467399. – teofil Aug 18 '19 at 04:39

1 Answers1

1

Your code hase several issues:

  1. Reproducibility: Try to run your code snippet in a refreshed R session before posting
  • 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
  • Any example data are missing to help you directly with your code. That's necessary as varSelectInputrequires data
  1. You cannot use TracePlot in ui, so use renderUI
  2. Reactivity: You do not need to use observeEvent inside renderPlot as it's reactive already. Instead, use input$Unit directly or to update data based on input$Unit, call a reactive variable created before.
  3. updateVarSelectInput is redundant if you use the reactive value in varSelectInput("Parameter", ...) already. If you want to update selected, you are probably better off with observe()outside of renderPlot

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)
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Thomas
  • 1,252
  • 6
  • 24
  • Thank you so much! most of what you pointed out is just because I copied and pasted a lot of code and tried to rename (to remove IDing information) and simplify it. After posting it I ended up using ` if ( input$Unit != str_sub(input$Parameter,3,8)) { updateVarSelectInput(session, "Parameter", "Select the Parameter to Plot:", PITracePlot)}` and got the desired result. The issue was that the drop down list kept refreshing even if the unit was not refreshed, it was refreshing on every other input as well. I read your comment carefully and will go back and try to implement it. – kjg2125 Aug 19 '19 at 14:37