Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

track changes when user input varies in r shiny

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading