reactiveVal: Create a (single) reactive value

View source: R/reactives.R

reactiveValR Documentation

Create a (single) reactive value


The reactiveVal function is used to construct a "reactive value" object. This is an object used for reading and writing a value, like a variable, but with special capabilities for reactive programming. When you read the value out of a reactiveVal object, the calling reactive expression takes a dependency, and when you change the value, it notifies any reactives that previously depended on that value.


reactiveVal(value = NULL, label = NULL)



An optional initial value.


An optional label, for debugging purposes (see reactlog()). If missing, a label will be automatically created.


reactiveVal is very similar to reactiveValues(), except that the former is for a single reactive value (like a variable), whereas the latter lets you conveniently use multiple reactive values by name (like a named list of variables). For a one-off reactive value, it's more natural to use reactiveVal. See the Examples section for an illustration.


A function. Call the function with no arguments to (reactively) read the value; call the function with a single argument to set the value.


## Not run: 

# Create the object by calling reactiveVal
r <- reactiveVal()

# Set the value by calling with an argument

# Read the value by calling without arguments

## End(Not run)

## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(
  actionButton("minus", "-1"),
  actionButton("plus", "+1"),

# The comments below show the equivalent logic using reactiveValues()
server <- function(input, output, session) {
  value <- reactiveVal(0)       # rv <- reactiveValues(value = 0)

  observeEvent(input$minus, {
    newValue <- value() - 1     # newValue <- rv$value - 1
    value(newValue)             # rv$value <- newValue

  observeEvent(input$plus, {
    newValue <- value() + 1     # newValue <- rv$value + 1
    value(newValue)             # rv$value <- newValue

  output$value <- renderText({
    value()                     # rv$value

shinyApp(ui, server)


shiny documentation built on May 29, 2024, 8:47 a.m.