0

Following this answer I'm trying to create an app that will output a plot based on the value I will pass to the app via URL

library(shiny)

shinyApp(
  ui = fluidPage(
    mainPanel(
      plotOutput("plot")
    )
  ),
  server = function(input, output, session) {
    observe({
      query <- parseQueryString(session$clientData$url_search)
      if (!is.null(query[['text']])) {
        n <- query[['text']]
      }
    })
    output$plot <- renderPlot({
      # Add a little noise to the cars data
      plot(cars[sample(nrow(cars), n), ])
    })
  }
)

Yet I don't know where/how I should store/pass the value of the variable n so to transfer it from observe() to renderPlot().

Community
  • 1
  • 1
CptNemo
  • 6,455
  • 16
  • 58
  • 107

1 Answers1

1

Try this. Note that n is defined as a per-session global variable, and notice the global assignment operator <<-

library(shiny)

shinyApp(
    ui = fluidPage(
        mainPanel(
            plotOutput("plot")
        )
    ),
    server = function(input, output, session) {
        n <- 5
        observe({
            query <- parseQueryString(session$clientData$url_search)
            if (!is.null(query[['text']])) {
                n <<- query[['text']]
            }
        })
        output$plot <- renderPlot({
            # Add a little noise to the cars data
            plot(cars[sample(nrow(cars), n), ])
        })
    }
)
Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41