How can I ensure the observation is called before renderPlot in my shiny app

I have a brilliant app with two selectInputs (L1 and L2) with an observer that updates L2 based on L1 selection using updateSelectInput. I also have renderPlot output which depends on both options. The problem I'm running into is that whenever I change L1, renderPlot is called twice, once with the old L2 value and once with the new value (set in updateSelectInput). My code is below:

ui.R
shinyUI(
    fluidPage(
        titlePanel("Nested Selects Problem"),
        sidebarLayout(
            sidebarPanel(
                selectInput(
                    "L1",
                    label = "L1",
                    choices = c("red", "blue")
                    ),
                selectInput(
                    "L2",
                    label = "L2",
                    choices = ""
                    )
                ),
            mainPanel(
                plotOutput("plot")
                )
            )
        )
    )

server.R
shinyServer(
    function(input,output,session) {
        observe({
            if (input$L1 == "red") {
                choices <- c(1000000,2000000,3000000)
            }
            else {
                choices <- c(10,20,30)
            }
            updateSelectInput(session,"L2",choices=choices,selected=choices[1])
        })

        output$plot <- renderPlot({
            if (input$L2 != "") {
                plot(runif(as.numeric(input$L2)),col=input$L1)
            }
        })
    })

      

How can I avoid the first call to renderPlot? It seems to me that if I could arrange for observation () to be called before the first renderPlot, I would get the desired effect.

Thank you for your help.

+3


source to share


2 answers


You can use isolate

to call input$L1

inside renderPlot

. Thus, changes should propagate from your call updateSelectInput

only when you change input$L1

:



library(shiny)
runApp(list(
  ui = fluidPage(
    titlePanel("Nested Selects Problem"),
    sidebarLayout(
      sidebarPanel(
        selectInput("L1",label = "L1",choices = c("red", "blue")),
        selectInput("L2",label = "L2",choices = "")
      ),
      mainPanel(
        plotOutput("plot")
      )
    )
  )


  , server = function(input,output,session) {
    observe({
      if (input$L1 == "red") {
        choices <- c(100,200,300)
      }
      else {
        choices <- c(10,20,30)
      }
      updateSelectInput(session,"L2",choices=choices,selected=choices[1])
    })

    output$plot <- renderPlot({
      if (input$L2 != "") {
        plot(runif(as.numeric(input$L2)),col=isolate(input$L1))
      }
    })
  })
)

      

+1


source


Well, how about:



shinyServer(
  function(input,output,session) {
    L1_selected <- reactiveValues(triggered=-1)

    observe({
      if (input$L1 == "red") {
        choices <- c(10, 100,200,300)
      }
      else {
        choices <- c(10,20,30)
      }

      old_L2 <- isolate(input$L2)
      updateSelectInput(session,"L2",choices=choices,selected=choices[1])
      isolate(L1_selected$triggered <- L1_selected$triggered + as.numeric(old_L2 != choices[1]))
    })

    output$plot <- renderPlot({
      if (input$L2 != "") {
        if (isolate(L1_selected$triggered)) {
          isolate(L1_selected$triggered <- L1_selected$triggered - 1)
          return()
        } else {
          plot(runif(as.numeric(input$L2)),col=input$L1)
        }
      }
    })
  })

      

+1


source







All Articles