Home > Back-end >  track changes when user input varies in r shiny
track changes when user input varies in r shiny

Time:10-11

I wonder how we can track changes when the user modifies the input in R Shiny. For example, I want to count the number of times the user changes the x input in the following code, but it seems not to be working.

    library(shiny)

ui <- fluidPage(
  selectInput(inputId = "x", label = "X", choices = names(mtcars), selected = names(mtcars)[1]),
  br(),
  br(),
  verbatimTextOutput("out")
)

server <- function(input, output, session) {
  
  r <- reactiveVal(0)
  y <- eventReactive(input$x,{
    r()   1 
  })
  
  output$out <- renderPrint({
    y()
  })
}

shinyApp(ui, server)

CodePudding user response:

Setting the value for a reactiveVal is done by assigning it like this:

r() = 0 r(1) = 1, etc.

So adjust your code like this:

library(shiny)

ui <- fluidPage(
  selectInput(inputId = "x", label = "X", choices = names(mtcars), selected = names(mtcars)[1]),
  br(),
  br(),
  verbatimTextOutput("out")
)

server <- function(input, output, session) {
  
  r <- reactiveVal(0)
  y <- eventReactive(input$x,{
   r(r()   1)
   return(r())        
  })
  
  output$out <- renderPrint({
    y()
  })
}

shinyApp(ui, server)
  • Related