K.Hua
K.Hua

Reputation: 799

Inserting reactive values in ReactiveValues

I have the following Shiny App

server <- function(input, output, session) {
  rv <- reactiveValues(i = 0)

  output$myplot <- renderPlotly({
    dt = data.frame(x = 1:10, y = rep(rv$i,10))
    plot_ly(dt, x = ~x, y =~y, mode = "markers", type = 'scatter') %>%
      layout(yaxis = list(range = c(0,10)))
  })

  observeEvent(input$run,{
    rv$i <- input$valstart
  })

  observe({
    isolate({rv$i = rv$i + 1})
    if (rv$i < 10){invalidateLater(1000, session)}
  })
}

ui <- fluidPage(
  sliderInput('valstart','Start Value', min = 1, max =3, value= 3),
  actionButton("run", "START"),
  plotlyOutput("myplot")
)

shinyApp(ui = ui, server = server)

I would, however that the value of rv at the beginning when I launch the app to depend on a parameter (here my slider input). I would like something like that:

rv <- reactiveValues(i = input$valstart)

But this give me the following error :

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

It seems that I can't insert reactive values inside reactiveValues... any solution?

Upvotes: 4

Views: 1809

Answers (1)

Adam Waring
Adam Waring

Reputation: 1268

Does observeEvent not get triggered at the start of the app?

How about:

rv <- reactiveValues(i = 0)

observeEvent(input$valstart,{
    rv$i <- input$valstart
  })

Upvotes: 3

Related Questions