4

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 one modification considering the first one, so It seems like I should save RA_s every time I modify it.

How can I handle with this problem ?

Code

shinyServer(function(input, output) {

  RA_s <- reactive({
    read.csv("C:/alay/Desktop/RA.csv")
  })

  Per <- reactive({
    if(input$go == 0) return(RA_s())
    else {
       c = RA_s()
       c[1] = rep(0,nrow(RA_s()))
    }
   c
  })

      })


sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Download", tabName = "d")
)
body<- dashboardBody(
  tabItems(
    tabItem(tabName = "d",
         actionButton("go",'GO!')     )
  )
dashboardPage(
dashboardHeader(title = "Valo"),
sidebar,
body
)
a.lay
  • 161
  • 2
  • 8

2 Answers2

4

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!

Graham
  • 7,431
  • 18
  • 59
  • 84
Florian
  • 24,425
  • 4
  • 49
  • 80
  • I tried this code but it didn't work. I have the same error that I had when I tried to modify reactive object : `All arguments passed to reactiveValues() must be named` – a.lay Jul 25 '17 at 07:02
  • I would be more than happy to help, but you should provide some sample data, so that I can reproduce your error. In particular, make sure you provide some data, see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Florian Jul 25 '17 at 14:55
  • It works when I use the import function directly in the code, but when I go through a `fileInput` i got this error `in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context (You tried to do something that can only be done from inside a reactive expression or observer.).` so i don't think there is a problem with data. The error lies in this line `new_value <- current_value[-(1)]` . (the same code worked without using `fileinput`) – a.lay Jul 26 '17 at 07:54
2

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)
a.lay
  • 161
  • 2
  • 8