Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting reactive values in ReactiveValues

Tags:

r

reactive

shiny

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?

like image 415
K.Hua Avatar asked Oct 24 '25 11:10

K.Hua


1 Answers

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

How about:

rv <- reactiveValues(i = 0)

observeEvent(input$valstart,{
    rv$i <- input$valstart
  })
like image 199
Adam Waring Avatar answered Oct 27 '25 02:10

Adam Waring