How reactive val works in Shiny

Here I have created a reactive value object y. So whenever a value of y changes as per RHS, it should get updated. But in this sample, the output I get is 61 instead of 90 . Can anyone explain why this is happening

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    # titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(),

        # Show a plot of the generated distribution
        mainPanel(
           textOutput("text")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    
    output$text <- renderText({
      y <- reactiveVal(0)
      #x <- reactiveVal(0)
      x = 61
      y = x
      x = 90
      y
    })
    
  
}

# Run the application 
shinyApp(ui = ui, server = server)

In the server, try this:

y <- reactiveVal(a = 0)
output$text <- renderText({
      x = 61
      y$a = x
      x = 90
      y$a
    })

y=x assigns the current value of x to y. It does not set up a permanent relation (y always equaling x).

I tried this, But getting error

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    # titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(),

        # Show a plot of the generated distribution
        mainPanel(
          actionButton("but", "Submit"),
           textOutput("text"),
          textOutput("text1")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  
  event_reactive <- FALSE
  if (event_reactive){
    asd <- eventReactive(input$but,{
      #browser()
      y <- reactiveVal(a = 0)
      #x <- reactiveVal(0)
      x <- 61
      y$a <- x
      x <- 90
      y$a
    })
    
    output$text <- renderText({
      asd()
    })

  } else {
    observeEvent(input$but,{
      y <- reactiveVal(a = 0)
      #x <- reactiveVal(0)
      x <- 61
      y$a <- x
      x <- 90
      y$a
      
      output$text <- renderText({
        y$a
      })
    })
  }


    

    
  
}

# Run the application 
shinyApp(ui = ui, server = server)

Error below

Warning: Error in reactiveVal: unused argument (a = 0)
  1: runApp

You shouldn't put a reactiveVal inside an else clause. It should be defined at the top before you begin the if construct. The following code works on my end.

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  # titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(),
    
    # Show a plot of the generated distribution
    mainPanel(
      actionButton("but", "Submit"),
      textOutput("text"),
      textOutput("text1")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  
  y <- reactiveVal(0)
  
  event_reactive <- FALSE
  if (event_reactive){
    asd <- eventReactive(input$but,{
      #browser()
      # y <- reactiveVal(a = 0)
      #x <- reactiveVal(0)
      x <- 61
    y <- x
      x <- 90
      y
    })
    
    output$text <- renderText({
      asd()
    })
    
  } else {
    observeEvent(input$but,{
      
      #x <- reactiveVal(0)
      x <- 61
      y <- x
      x <- 90
      y
      
      output$text <- renderText({
        y
      })
    })
  }
}
  
  shinyApp(ui = ui, server = server)

Thanks. I tried with this,

The output I get is 61 instead of 90.
The actual output should be 90 right?

Not sure if we are on the same page. The output I am expecting is 90 since x has a new value (that is 90)

No, you should get 61. y is the reactiveVal. Changing x has no bearing on the value of y which is what you are outputting.

I see, I misunderstood reactiveVal then. So what is the purpose of reactiveVal ?. I use this definition reactiveVal

a reactiveVal, is a container you can put things into, and read things out from, it is reactive in the same way reactive()'s are because if you have a reactive() or render* that depends on it, when its value changes those that rely on it will change. You can see that here that the y text printer changes when we use y() to give a new value to reactiveVal y.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("x_slider", "X", 
                  min = 1, max = 5, value = 3),
      actionButton("yset_btn", "set Y to X")
    ),
    mainPanel(
      fluidPage(
        fluidRow(
          column(3, verbatimTextOutput("xtext")),
          column(3, verbatimTextOutput("ytext"))
        )
      )
    )
  )
)

server <- function(input, output) {
  y <- reactiveVal(0)
  
  observeEvent(input$yset_btn,{
    y(input$x_slider)  # <<<< Giving y() new contents here ! 
  })
  
  output$xtext <- renderPrint({
    req(input$x_slider)
  })
  
  output$ytext <- renderPrint({
    req(y())  #  <<<<< when y() changes the print will happen
  })
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.