Self-referencing reactive variables in Shiny (R)

Here is a quick solution to the problem but does not exactly address the question.

library(shiny)

omega_nr <- 0

# Define the UI
myui <- basicPage(  
  textOutput('var')
)

# Define the server code
myserver <- function(input, output, session) {

  omega <- reactive({
    invalidateLater(1000, session)
    return(omega_nr)
  })

  # update non reactive value
  observe({
    omega()
    omega_nr<<-omega_nr+1
  })

  output$var <- renderText(omega())
}


# Return a Shiny app object
shinyApp(ui = myui, server = myserver)

This is working I just tested it.

I'll try other options and let you know. Meanwhile I'm also interested in exact solution. But somehow I feel that self referencing reactive should not be implemented. But I may be wrong..


You can just build a function that does that:

library(shiny)

myui <- basicPage(  textOutput('var'))
myserver <- shinyServer(function(input, output, session){

  omega <<- 0
  update_data <- function(){omega <<- omega + 1}

  output$var <- renderText({
    invalidateLater(1000, session)
    update_data()})
})

shinyApp(ui = myui, server = myserver)