In my shiny app, I have a render that I want to only execute after a radio button changes values, but only the first time this happens. Is there a way to make it reactive to the first change in value, but not subsequent ones?
            Asked
            
        
        
            Active
            
        
            Viewed 840 times
        
    1
            
            
         
    
    
        Kewl
        
- 3,327
- 5
- 26
- 45
- 
                    I would suggest adding a minimal example to show what you want to happen: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Iain Apr 30 '17 at 16:15
1 Answers
3
            Below you will find that observeEvent has arguments such as ignoreInit and once, I would advise that you go and have a look at the function definitions on the official website Event handler. I have also added the shinyjs library with its disable function which I think is handy here.
rm(list=ls())
library(shiny)
library(shinyjs)
ui <- fluidPage(
  useShinyjs(),
  radioButtons("nums", "Will Execute only Once",c("1000" = 1000,"10000" = 10000), selected = 0),
  plotOutput("Plot")
)
server <- function(input, output) {
  v <- reactiveValues()
  observeEvent(input$nums, {
    v$data <- rnorm(input$nums) 
  },ignoreInit = TRUE, once = TRUE)
  output$Plot <- renderPlot({
    if(is.null(v$data)){
      return()
    }
    disable('nums')
    hist(v$data)
    box()
  })
}
shinyApp(ui, server)
 
    
    
        Pork Chop
        
- 28,528
- 5
- 63
- 77
