Populate a reactiveValues entry on load

I have a csv file already on the server that contains some information, and my application has to do the following:

  • when the user connects, a reactiveValues key is populated with the content of the csv file.
  • from that moment on, the user modifies the reactiveValues key contents and triggers ordinary events, but in no circumstance I want to accidentally retrieve again from the csv.

It is not clear how this is meant to be done in shiny. If I had a button "load data", it would be trivial to connect an observe to it and perform the operation, reading from the csv and pushing the information in the reactiveValues, but I am not aware of any "on first connection" options in the session object.

Of course this assignment needs to be done in a reactive context, otherwise shiny will complain.

What is the proper way of addressing this use case in Shiny?

Hi sbo,
If you have a line in your server code that reads the csv and puts it into a reactive,
you can then make another reactive that is dependent on it, and allow the user to edit/interfere with.

You can read in the csv file in your global.r file and assign it to a data frame. Then in your server, when you initialize the reactiveValue, set it to your csv data frame, like:

#global.r
dat <- read.csv("yourfile.csv")

#server.r
reac_value <- reactiveValues(data = dat)

I must have had something wrong in the code, because I did exactly what you recommend and it did not work the first time, but now it does. However, I suspect that the only thing I can do is to set the values at creation. What about later, while still not being in a non-reactive environment. For example, if I were to do

reac_value$data <- dat2

later in the server code, would that be legal?
In other words, which usages of a reactiveValues is not legal outside of a reactive environment? getting?

Right if at any point you want to redefine that value after initializing, you must do so in a reactive context:

observe({
   dat2 <- dat
   reac_value$data <- dat2
})

The help page for reactiveValues should hopefully shine more light on this for you. I think of reactiveValues as a special list whose elemetns can be accessed within your app.
https://shiny.rstudio.com/reference/shiny/latest/reactiveValues.html

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.