rubenarslan / formr.org

Chain simple surveys into longer runs to build complex studies. Use R to generate pretty feedback and complex designs.
https://formr.org
Other
131 stars 36 forks source link

Persist R objects across pages #498

Open kwcooper opened 1 year ago

kwcooper commented 1 year ago

Hi! I ran into the same problem of needing to accesses previously initialized R objects as the user here posted in the groups page. Given that I needed to store an entire dataframe, however, the suggested answer (storing the result of the calculation) didn't seem as apt to my situation.

What somewhat works is to store the dataframe as a JSON file, via jsonlite::toJSON(dataframe) in a calculate label, and then continually read in the dataframe for each page, via jsonlite::fromJSON(json_df), then resave it for the next page. At best though, this is a bit hacky, and at worst, it doesn't meet all edge cases.

My guess is that the difficulties are due to each page in the survey creating a new R session? (although this is just my assumption, I couldn't find any description of how the backend R works in the documentation) Yet, if formr is able to store the result of the calculations to persist via survey_name$variable_name, I'm curious if my assumption is wrong, and maybe there is a better way? Thank you!