Load a csv file in R and share in all sessions?

Viewed 126

I have a shiny application that uses a csv file to generate different figures. I upload my application in my personal linux server using shiny-server. I use this structure for my application

  • global.R
  • ui.R
  • server.R

Inside my global.R file I have this line, which help me to load and read my csv file

df <- read_csv("../Desktop/covid_2021-02-15.csv")

But my application is very slow, I read that objects in the global.R script, are read only one time and are share in all sessions.

Is there other way to load this data frame to have a more efficient application?

1 Answers

In addition to the comments from Gregor and HubertL:

To load large CSV files can slow down. I had the same issue and changed to r binary file (rds) with saveRDS() and readRDS(). As a first step you can try rds file and see if the issue is solved. To check whether there is a performance difference you can use system.time(). Returns the time taken to evaluate any R expression.

In your case:

df <- read_csv("../Desktop/covid_2021-02-15.csv")
# Save an object to a file
saveRDS(df, file = "my_data.rds")
# Restore the object
readRDS(file = "my_data.rds")
Related