Application Directories: Determine Where to Save Data, Caches, and Logs

An easy way to determine which directories on the users computer you should use to save data, caches and logs. A port of Python's 'Appdirs' (\url{ https://github.com/ActiveState/appdirs}) to R.


Build Status

rappdirs is a port of appdirs to R.

What directory should your app use for storing user data? If running on Mac OS X, you should use:

~/Library/Application Support/<AppName>

If on Windows (at least English Win XP) that should be:

C:\Documents and Settings\<User>\Application Data\Local Settings\<AppAuthor>\<AppName>

or possibly:

C:\Documents and Settings\<User>\Application Data\<AppAuthor>\<AppName>

for roaming profiles but that is another story.

On Linux (and other Unices) the dir, according to the XDG spec (and subject to some interpretation), is either:

~/.config/<AppName>     

or possibly:

~/.local/share/<AppName>

rappdirs to the rescue

This kind of thing is what the appdirs module is for. appdirs will help you choose an appropriate:

  • user data dir (user_data_dir)
  • user config dir (user_config_dir)
  • user cache dir (user_cache_dir)
  • site data dir (site_data_dir)
  • user log dir (user_log_dir)

and also:

  • is slightly opinionated on the directory names used. Look for 'Opinion' in documentation and code for when an opinion is being applied.

some example output

On Mac OS X:

library(appdirs)
appname <- "SuperApp"
appauthor <- "Acme"
user_config_dir(appname, appauthor)
user_data_dir(appname, appauthor)
# "/Users/trentm/Library/Application Support/SuperApp"
site_data_dir(appname, appauthor)
# "/Library/Application Support/SuperApp"
user_cache_dir(appname, appauthor)
"/Users/trentm/Library/Caches/SuperApp"
user_log_dir(appname, appauthor)
"/Users/trentm/Library/Logs/SuperApp"

On Windows 7:

library(appdirs)
appname <- "SuperApp"
appauthor <- "Acme"
user_config_dir(appname, appauthor)
# "C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp"
user_data_dir(appname, appauthor)
# "C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp"
user_data_dir(appname, appauthor, roaming=True)
# "C:\\Users\\trentm\\AppData\\Roaming\\Acme\\SuperApp"
user_cache_dir(appname, appauthor)
# "C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp\\Cache"
user_log_dir(appname, appauthor)
# "C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp\\Logs"

On Linux:

library(appdirs)
appname <- "SuperApp"
appauthor <- "Acme"
user_config_dir(appname, appauthor)
# "/home/trentm/.config/SuperApp
user_data_dir(appname, appauthor)
# "/home/trentm/.local/share/SuperApp
site_config_dir(appname, appauthor)
# "/etc/xdg/SuperApp"
user_cache_dir(appname, appauthor)
# "/home/trentm/.cache/SuperApp"
user_log_dir(appname, appauthor)
# "/home/trentm/.cache/SuperApp/log"

app_dir for convenience

library(appdirs)
dirs <- app_dir("SuperApp", "Acme")
dirs$config()
# "/Users/trentm/Library/Application Support/SuperApp"
dirs$data()
# "/Users/trentm/Library/Application Support/SuperApp"
dirs$site_data()
# "/Library/Application Support/SuperApp"
dirs$cache()
# "/Users/trentm/Library/Caches/SuperApp"
dirs$log()
# "/Users/trentm/Library/Logs/SuperApp"

Per-version isolation

If you have multiple versions of your app in use that you want to be able to run side-by-side, then you may want version-isolation for these dirs:

library(appdirs)
dirs <- app_dir("SuperApp", "Acme", version = "1.0")
dirs$data()
# "/Users/trentm/Library/Application Support/SuperApp/1.0"
dirs$site_data()
# "/Library/Application Support/SuperApp/1.0"
dirs$cache()
# "/Users/trentm/Library/Caches/SuperApp/1.0"
dirs$log()
# "/Users/trentm/Library/Logs/SuperApp/1.0"

If you set the argument expand = TRUE (the default) you can have directories that correspond to R versions:

user_config_dir("R", version="%p-platform/%v")
# "/home/trevorld/.config/R/x86_64-pc-linux-gnu-platform/3.0"

News

rappdirs 0.3

  • first CRAN release
  • xxx_dir() functions only use version when appname is not null
  • docs: basic package docs (?rappdirs)
  • docs: clarify primary purpose of os argument in xxx_dir() i.e. testing
  • fix typo in function name in README.md (app_dirs -> app_dir)
  • dev: add travis continuous integration
  • dev: add rstudio project
  • dev: update to roxygen2 v4.0.1

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("rappdirs")

0.3.1 by Hadley Wickham, 3 years ago


https://github.com/hadley/rappdirs https://github.com/ActiveState/appdirs


Report a bug at https://github.com/hadley/rappdirs/issues


Browse source code at https://github.com/cran/rappdirs


Authors: Hadley Wickham [trl, cre, cph] , RStudio [cph] , Sridhar Ratnakumar [aut] , Trent Mick [aut] , ActiveState [cph] (R/appdir.r , R/cache.r , R/data.r , R/log.r translated from appdirs) , Eddy Petrisor [ctb] , Trevor Davis [trl, aut] , Gabor Csardi [ctb] , Gregory Jefferis [ctb]


Documentation:   PDF Manual  


MIT + file LICENSE license


Depends on methods

Suggests testthat, roxygen2


Imported by AzureAuth, BIOMASS, LAGOSNE, Nmisc, TR8, addinslist, adklakedata, awsjavasdk, batchtools, bcmaps, beastier, binman, ccafs, ceramic, ebirdst, exifr, finch, getlandsat, hoardr, hydrolinks, icd, jmvconnect, keyring, learnr, liteq, mregions, nat.templatebrains, nesRdata, nhdR, nlaR, openadds, opencpu, packagetrackr, pkgcache, pleiades, ramlegacy, rdhs, rhub, rmd, rnoaa, rtika, smapr, sparklyr, tesseract, textdata, tidycensus, tidyhydat, tigris, virtuoso, wdpar.

Suggested by bomrang, getCRUCLdata, mlr.


See at CRAN