0

I am creating a package and would like to store settings data locally, since it is unique for each user of the package and so that the setting does not have to be set each time the package is loaded.

How can I do this in the best way?

Héctor van den Boorn
  • 1,218
  • 13
  • 32

1 Answers1

0

You could save your necessary data in an object and save it using saveRDS() whenever a change it made or when user is leaving or giving command for saving. It saves the R object as it is under a file name in the specified path.

saveRDS(<obj>, "path/to/filename.rds")

And you can load it next time when package is starting using loadRDS(). The good thing of loadRDS() is that you can assign a new name to the obj. (So you don't have to remember its old obj name. However the old obj name is also loaded with the object and will eventually pollute your namespace.

newly.assigned.name <- loadRDS("path/to/filename.rds")
# or also possible:
loadRDS("path/to/filename.rds") # and use its old name

Where to store

Windows

Maybe here:

You can use %systemdrive%%homepath% environment variable to accomplish this.

The two command variables when concatenated gives you the desired user's home directory path as below:

Running echo %systemdrive% on command prompt gives:

C:

Running echo %homepath% on command prompt gives:

\Users\

When used together it becomes:

C:\Users\

Linux/OsX

Either in the package location of the user,

path.to.package <- find.package("name.of.your.pacakge", 
                                lib.loc = NULL, quiet = FALSE,
                                 verbose = getOption("verbose"))
# and then construct with 
destination.folder.path <- file.path(path.to.package, 
                                     "subfoldername", "filename")`
# the path to the final destination
# You should use `file.path()` to construct such paths, because it detects automatically the correct ('/' or '\') separators for the file paths in Unix-derived systems (Linux/Mac Os X) versus Windows.

Or use the $HOME variable of the user and there in a file - the name of which beginning with "." - this is convention in Unix-systems (Linux/Mac OS X) for such kind of file which save configurations of software programs. e.g. ".your-packages-name.rds".

If anybody has a better solution, please help!

Gwang-Jin Kim
  • 9,303
  • 17
  • 30
  • But which path should I use to save the file? I cannot use the working-directory as it may change and I don't know were to store the file on disk. It could be on the C-disk or something else and I don't know which folder to use. Is there a special folder I can use? – Héctor van den Boorn May 27 '18 at 23:46
  • The path of your package on the local computer of ther user. Look at http://stat.ethz.ch/R-manual/R-devel/library/base/html/find.package.html - with this command you should be able to automatically detect the path from your package in users computer. You have to automatically detect the path - because not all users use Windows but others (like me) linux, and others Mac (Os X). – Gwang-Jin Kim May 28 '18 at 07:30
  • Hopefully the user has not installed the package in a non-home area in mac or linux, but locally in his home directory. Else R has not the right to write there, except he starts R with sudo. – Gwang-Jin Kim May 28 '18 at 07:32
  • I added this part to my answer. – Gwang-Jin Kim May 28 '18 at 07:40