Save package settings between sessions

Viewed 755

Is there a definitive way to save options or information pertaining to a certain package between sessions?

For example say somebody made a game and released it as an R package. If they wanted to save high scores and not have them reset each time R started a new session what would be the best way to do this? Currently I can only think of storing a file in the users home directory but I'm not sure if I like that approach.

3 Answers

The modern answer to this problem is well explained at https://blog.r-hub.io/2020/03/12/user-preferences/

I think I will be trying the hoardr package! Here is an example that worked for me :)

x <- hoardr::hoard()
x$cache_path_set("yourpackage", type = 'user_cache_dir')
x$mkdir()

scores<-data.frame(
  user=c("one","two","three"),
  score=c("500,200,1100")
  )
save(scores,file = file.path(x$cache_path_get(), "scores.rdata"))

x$list()
x$details()

#new session
x <- hoardr::hoard()
x$cache_path_set("yourpackage", type = 'user_cache_dir')
x$list()
x$details()
load(file = file.path(x$cache_path_get(), "scores.rdata"))

PS - you can see a working example in the rnoaa package found on at github "opensci/rnoaa". Check their R/onload.r file! I can expand if needed.

Related