Shiny : How to modify reactive object

前端 未结 2 1702
小鲜肉
小鲜肉 2021-01-03 06:24

I have a reactive object that i want to modify when the user click on the GO button. I tried this code and Per gives a great result. Now I want to do more than

相关标签:
2条回答
  • 2021-01-03 07:03

    I used reactiveValues instead of reactiveVal and it works perfectly

    Thanks @Florian

    server <- shinyServer(function(input, output) {
    
       in_data <- reactive({
           inFile <- input$e
           read.csv(inFile$datapath)
        })
    
      RA_s <- reactiveValues(ra = NULL)
      RA_s$ra <- in_data() # intiialize the reactiveValue
    
    
      # an observer to update the reactiveValue RA_s
      observeEvent(input$go, {
        # read the current value
       current_value <- RA_s$ra
    
       # update the value
       new_value <- current_value[-(1)]
    
       # write the new value to the reactive value
       RA_s$ra <- new_value
    
    
      })
      output$x <- renderDataTable({
       RA_s$ra
      })
    })
    
    
    ui <- shinyUI(
      fluidPage(
        fileInput('e', 'E'),
        actionButton("go","GO!"),
        dataTableOutput('x')     
      )
    )
    shinyApp(ui,server)
    
    0 讨论(0)
  • 2021-01-03 07:07

    In order to store and update reactive objects, you can use reactiveVal or reactiveValues.

    I created a simple example on how this works with your goal in mind:

    server <- shinyServer(function(input, output) {
    
      RA_s <- reactiveVal()
      RA_s(1) # intiialize the reactiveVal, place your csv read here.
    
    
      # an observer to update the reactiveVal RA_s
      observeEvent(input$go, {
        # read the current value
       current_value <- RA_s()
    
       # update the value
       new_value <- current_value +1
    
       # write the new value to the reactive value
       RA_s(new_value)
    
       # print to console, just to check if it updated.
       print(paste0("Succesfully updated, new value: ",RA_s()))
      })
    
    })
    
    
    ui <- shinyUI(
      fluidPage(
        actionButton("go","GO!")     
      )
    )
    shinyApp(ui,server)
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题