GithubHelp home page GithubHelp logo

r-lib / rappdirs Goto Github PK

View Code? Open in Web Editor NEW
81.0 5.0 15.0 852 KB

Find OS-specific directories to store data, caches, and logs. A port of python's AppDirs

Home Page: https://rappdirs.r-lib.org

License: Other

R 95.12% C 4.88%
r appdir

rappdirs's Introduction

rappdirs

R-CMD-check Codecov test coverage

rappdirs is a port of appdirs to R. It lets you find the appropriate directory to save caches, logs, and data, on Linux, Mac, and Windows. It allows you to store files that need to shared across R sessions in a way that aligns with the CRAN policies.

Motivation

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>

Installation

Stable version:

install.packages("rappdirs")

Development version:

pak::pak("r-lib/rappdirs")

Usage

This kind of thing is what rappdirs is for. rappdirs 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())

For example, on Mac:

library(rappdirs)
appname <- "SuperApp"
appauthor <- "Acme"
user_config_dir(appname, appauthor)
#> [1] "~/Library/Application Support/SuperApp"
user_data_dir(appname, appauthor)
#> [1] "~/Library/Application Support/SuperApp"
site_data_dir(appname, appauthor)
#> [1] "/Library/Application Support/SuperApp"
user_cache_dir(appname, appauthor)
#> [1] "~/Library/Caches/SuperApp"
user_log_dir(appname, appauthor)
#> [1] "~/Library/Logs/SuperApp"

rappdirs's People

Contributors

fmichonneau avatar gaborcsardi avatar hadley avatar jefferis avatar trevorld avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rappdirs's Issues

user_data_dir under Mac

I like rappdirs::user_data_dir for providing a general solution to storing package-associated data.

However, I find the corresponding destination under Mac OS X

~/Library/Application Support/<AppName>

suboptimal - just due to the contained space.

https://superuser.com/questions/29111/what-technical-reasons-exist-for-not-using-space-characters-in-file-names

This seems to complicate things.

As an example:

data.dir <- rappdirs::user_data_dir()

html.file <- file.path(data.dir, "test.html")
content <- "<html>\n<body>\nHello World!\n</body>\n</html>"
writeLines(content, html.file)

# opens the browser in a terminal, but does nothing in RStudio
browseURL(html.file)

# works in RStudio, but not in a terminal anymore
html.file <- gsub(" ", "%20", html.file) 
browseURL(html.file)

What's the rationale behind choosing a file path with a space?

CRAN complains about `user_cache_dir()`

We got this message from CRAN when we used rappdirs::user_cache_dir():

Also, checking creates and leaves behind
   ~/.cache/R-bslib
in violation of the CRAN Policy's
   Packages should not write in the user's home filespace (including
   clipboards), nor anywhere else on the file system apart from the R
   session's temporary directory (or during installation in the location
   pointed to by TMPDIR: and such usage should be cleaned up). Installing
   into the system's R installation (e.g., scripts to its bin directory)
   is not allowed.
   Limited exceptions may be allowed in interactive sessions if the
   package obtains confirmation from the user.
   For R version 4.0 or later (hence a version dependency is required or
   only conditional use is possible), packages may store user-specific
   data, configuration and cache files in their respective user
   directories obtained from tools::R_user_dir(), provided that by
   default sizes are kept as small as possible and the contents are
   actively managed (including removing outdated material).
Can you please change as necessary, ideally using the
tools::R_user_dir() mechanism where available?

The code is here:
https://github.com/rstudio/bslib/blob/80a5059bcef0bec16c9d914b6458f70077d2707b/R/fonts.R#L387

@gaborcsardi has said that we should stick with rappdirs, since tools::R_user_dir() doesn't always do the right thing on Windows, but this is causing problems with packages submitted to CRAN.

For the record, on my Mac:

> R.version.string
[1] "R version 4.0.3 (2020-10-10)"
> tools::R_user_dir('foo', which = 'cache')
[1] "/Users/winston/Library/Caches/org.R-project.R/R/foo"
> rappdirs::user_cache_dir('foo')
[1] "/Users/winston/Library/Caches/foo"

And on Linux (using the wch1/r-debug docker image):

> R.version.string
[1] "R Under development (unstable) (2021-01-14 r79827)"
> tools::R_user_dir('foo', which = 'cache')
[1] "/root/.cache/R/foo"
> rappdirs::user_cache_dir('foo')
[1] "/root/.cache/foo"

cc @cpsievert, @schloerke

Option to change default path for `user_cache_dir`?

Maybe I'm missing this in the documentation, but is there a way to override the default cache directory prefix defined by rappdirs::user_cache_dir()? Such an option is useful in some cases when running R on a remote instance. Is there a global environment variable you can set in Renviron or Rprofile?

Using unallowed path?

Happy New Year!

In my weathercan package I included an option for saving a data frame of stations data to a cache directory, found with rappdirs.

stations_file <- function() {
  file.path(rappdirs::user_data_dir("weathercan"), "stations.rds")
}

However, I recently heard from CRAN

"And if I run stations_dl() it asks

weathercan would like to store the updated stations data to:
~/Library/Application Support/weathercan/stations.rds

which is not an allowed area under the CRAN policy"

I definitely don't blame you for my package getting booted, but I thought I might check in case I used the function incorrectly, or there was a change to CRAN policy. Any suggestions?

HTTP status was '404 Not Found'

Partial result when attempting to install devtools:

trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/rappdirs_0.3.3.tgz'
Warning in install.packages :
  cannot open URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/rappdirs_0.3.3.tgz': HTTP status was '404 Not Found'
Error in download.file(url, destfile, method, mode = "wb", ...) : 
  cannot open URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/rappdirs_0.3.3.tgz'
Warning in install.packages :
  download of package ‘rappdirs’ failed

After the recent update to this package, when downloading as a dependency for devtools, I am unable to do so. When manually following the link to 'https://cran.rstudio.com/bin/macosx/contrib/4.0/rappdirs_0.3.3.tgz' you can also see that it's not found.

Release rappdirs 0.3.3

Prepare for release:

  • devtools::build_readme()
  • Check current CRAN check results
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • revdepcheck::revdep_check(num_workers = 4)
  • urlchecker::url_check()
  • Update cran-comments.md
  • Polish NEWS

Submit to CRAN:

  • usethis::use_version('patch')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted 🎉
  • usethis::use_github_release()
  • usethis::use_dev_version()

Move `master` branch to `main`

The master branch of this repository will soon be renamed to main, as part of a coordinated change across several GitHub organizations (including, but not limited to: tidyverse, r-lib, tidymodels, and sol-eng). We anticipate this will happen by the end of September 2021.

That will be preceded by a release of the usethis package, which will gain some functionality around detecting and adapting to a renamed default branch. There will also be a blog post at the time of this master --> main change.

The purpose of this issue is to:

  • Help us firm up the list of targetted repositories
  • Make sure all maintainers are aware of what's coming
  • Give us an issue to close when the job is done
  • Give us a place to put advice for collaborators re: how to adapt

message id: euphoric_snowdog

Depend on methods, for Rscript

The usual, a Depends: methods is needed, otherwise Rscript does not work:

/tmp$ Rscript -e  'library(rappdirs); app_dir("foo", "bar")' 
Error in appdirs$new : could not find function "loadMethod"
Calls: app_dir -> $
Execution halted
/tmp$ Rscript -e  'library(rappdirs); library(methods); app_dir("foo", "bar")' 
Reference class object of class "AppDirs"
Field "appname":
[1] "foo"
Field "appauthor":
[1] "bar"
Field "version":
NULL
Field "expand":
[1] TRUE
Field "os":
[1] "mac"

xxx_dir(os) argument is broken

The ability to specify an os other than the current one in the xxx_dir functions is fundamentally broken. This is because the internal file_path function uses normalizePath and path.expand, which have no notion of an os other than the current platform. For example on windows:

> user_data_dir(os='mac')
[1] "C:\\Users\\superFlyGuy\\Documents\\Library\\Application Support"

> path.expand("~")
[1] "C:\\Users\\superFlyGuy\\Documents"

I vote that we remove the os argument from all functions. It was introduced in @trevorld's huge 471aa1c commit without comment. I don't really see a strong case for wanting this. FWIW python appdirs does not have it https://github.com/ActiveState/appdirs/blob/master/appdirs.py

Removing os will in turn invalidate most of the tests, which can only be run in a platform specific way. The best that I think we can do is to run unix and windows tests based on the value of .Platform$OS.type. Likewise many examples will need to be changed to remove the os argument.

Alternatively, we could leave the os argument, but then it would only work for different flavours of unix and would have to error out otherwise. Or we could try to emulate normalizePath and path.expand with an os argument but that sounds like it would be costly and fragile and I don't see that the benefits warrant that effort.

Views @hadley? I think this is a blocker and you'll need to approve the strategy. @trevorld was there a particularly reason for the os argument that I'm missing? It looks like it may have been introduced for testing as much as anything.

CRAN release?

I have a package that needs to download additional data. I'd like to use rappdirs as a dependency to choose a sensible location for that data, but I'd rather not have a non-CRAN dependency that requires compilation. Do you have any plans to submit this to CRAN? Is there a reason not to? Some alternative? Many thanks, Greg Jefferis.

Upkeep for rappdirs

Pre-history

  • usethis::use_readme_rmd()
  • usethis::use_roxygen_md()
  • usethis::use_github_links()
  • usethis::use_pkgdown_github_pages()
  • usethis::use_tidy_github_labels()
  • usethis::use_tidy_style()
  • usethis::use_tidy_description()
  • urlchecker::url_check()

2020

  • usethis::use_package_doc()
    Consider letting usethis manage your @importFrom directives here.
    usethis::use_import_from() is handy for this.
  • usethis::use_testthat(3) and upgrade to 3e, testthat 3e vignette
  • Align the names of R/ files and test/ files for workflow happiness.
    usethis::rename_files() can be helpful.

2021

  • usethis::use_tidy_dependencies()
  • usethis::use_tidy_github_actions() and update artisanal actions to use setup-r-dependencies
  • Remove check environments section from cran-comments.md
  • Bump required R version in DESCRIPTION to 3.4
  • Use lifecycle instead of artisanal deprecation messages, as described in Communicate lifecycle changes in your functions
  • Add RStudio to DESCRIPTION as funder, if appropriate

2022

user_data_dir: Consistency of ~ vs expanded version

When I run "user_data_dir("test")` on my Windows 10 machine:

C:\\User\\Dean\\AppData\\Local\\test\\test

On my MacOS 10.15 Catalina:

"/Users/dean/Library/Application Support/test"

Both of those use expanded paths for home dir.

But in the readme and in a report by a user of a package of mine, the path on Mac (which I assume was a different version) seems to be

"~/Library/Application Support/test"

There are two issues here:

  1. Consistency across OSes - it would be ideal if the function would always return the expanded version so that the end user knows what to expect
  2. More importantly, consistency within an OS - it seems not great that two different Macs produce different results

get_os() is private function but used in public docs

The default arguments for app_dir include:

os=get_os()

However, get_os() is a private function inside rappdirs. Typically calling a function with the example usage doesn't result in an error when the result is reasonably well defined by the function call itself. It seems like either the usage section should be changed or a caveat added to the documentation in locations where the default options refer to private functions. YMMV.

Release rappdirs 0.3.2

Prepare for release:

  • devtools::build_readme()
  • Check current CRAN check results
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • revdepcheck::revdep_check(num_workers = 4)
  • urlchecker::url_check()
  • Update cran-comments.md
  • Polish NEWS

Submit to CRAN:

  • usethis::use_version('patch')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted 🎉
  • usethis::use_github_release()
  • usethis::use_dev_version()

Windows: appname double in user_config_dir

Hallo,

An appname is shown twice when I use user_config_dir on my windows system:

packageVersion("rappdirs")
#[1] ‘0.3.1’
rappdirs::user_config_dir("mypackage", expand = FALSE)
#[1] "C:\\Users\\name\\AppData\\Roaming\\mypackage\\mypackage"

My linux systems show it only once. Is this correct?

Thanks!

Upkeep for rappdirs (2023)

Pre-history

  • usethis::use_readme_rmd()
  • usethis::use_roxygen_md()
  • usethis::use_github_links()
  • usethis::use_pkgdown_github_pages()
  • usethis::use_tidy_github_labels()
  • usethis::use_tidy_style()
  • urlchecker::url_check()

2020

  • usethis::use_package_doc()
  • usethis::use_testthat(3)
  • Align the names of R/ files and test/ files

2021

  • Remove check environments section from cran-comments.md
  • Use lifecycle instead of artisanal deprecation messages

2022

  • Handle and close any still-open master --> main issues
  • usethis:::use_codecov_badge("r-lib/rappdirs")
  • Update pkgdown site using instructions at https://tidytemplate.tidyverse.org
  • Update lifecycle badges with more accessible SVGs: usethis::use_lifecycle()

2023

  • Update email addresses *@rstudio.com -> *@posit.co
  • Update copyright holder in DESCRIPTION: person("Posit Software, PBC", role = c("cph", "fnd"))
  • Run devtools::document() to re-generate package-level help topic with DESCRIPTION changes
  • usethis::use_tidy_logo()
  • usethis::use_tidy_coc()
  • Use pak::pak("r-lib/rappdirs") in README
  • Consider running usethis::use_tidy_dependencies() and/or replace compat files with use_standalone()
  • #45
  • #46
    or file an issue if you don't have time to do it now
  • Change files ending in .r to .R in R/ and/or tests/testthat/
  • #47

Eternal

  • usethis::use_mit_license()
  • usethis::use_package("R", "Depends", "3.6")
  • usethis::use_tidy_description()
  • usethis::use_tidy_github_actions()
  • devtools::build_readme()
  • Re-publish released site if needed

Created on 2023-11-04 with usethis::use_tidy_upkeep_issue(), using usethis v2.2.2.9000

Is there a way to make (force?) rappdirs::user_data_dir()to point to a local directory when app is deployed on Posit Connect?

Hi,

We are building a shiny app that contains several inputs. When the user visits the app for the first time, we prompt them to save their inputs. The input file is saved in the directory provided by rappdirs::user_data_dir(). e.g in my machine (Mac), the file is saved in "~/Library/Application Support/xxx", xxx being the name of the folder. This works perfectly locally.

Now when we deploy the app to Posit Connect, rappdirs::user_data_dir() registers a dir on the posit connect server such that another user can load that saved input file. Is there a way to tweak the function so that it points to the user's machine?

Path separator consistency on Windows

For now it is just an observation and it does not cause issue directly. However, it seems not quite right to have different path separator in the same PATH.

On windows, the path separator is not consistent when path are built - see below

# \\ at the beggining, / at the end. 
rappdirs::user_cache_dir("dummy", version = "1.1.0")
#> [1] "C:\\Users\\chris\\AppData\\Local/dummy/dummy/1.1.0/Cache"

# this is because this does not use .Platform$file.sep
rappdirs:::win_path("local")
#> [1] "C:\\Users\\chris\\AppData\\Local"

# but is used as based with file_path using the sep
rappdirs:::file_path("a", "b")
#> [1] "a/b"
dir <- rappdirs:::file_path(rappdirs:::win_path("local"), "dummy", "1.1.0")
dir
#> [1] "C:\\Users\\chris\\AppData\\Local/dummy/1.1.0"

# Path needs to be normalize to be consistent
fs::path_norm(dir)
#> C:/Users/chris/AppData/Local/dummy/1.1.0

# But R 4.0+ does the same so maybe it is expected ? 
tools::R_user_dir("dummy", "cache")
#> [1] "C:\\Users\\chris\\AppData\\Local/R/cache/R/dummy"

Created on 2021-07-07 by the reprex package (v2.0.0)

Is the good practice to normalize the path built anyway ?

rappdirs 0.3.2: 'version' argument is now vectorized, e.g. in user_cache_dir()

Posting in case others run into this and also to make sure this update was intended.

Previously, I've used:

> packageVersion("rappdirs")
[1] ‘0.3.1> 
> rappdirs::user_cache_dir("foo", version = c("a", "b"))
[1] "/home/hb/.cache/foo/a/b"

With the new rappdirs 0.3.2, I now get:

> packageVersion("rappdirs")
[1] ‘0.3.2> rappdirs::user_cache_dir("foo", version = c("a", "b"))
[1] "~/.cache/foo/a" "~/.cache/foo/b"

The reason appears to be that rappdirs:::file_path(x) is now vectorized;

> packageVersion("rappdirs")
[1] ‘0.3.2> rappdirs:::file_path(c("a", "b"))
[1] "a" "b"
> packageVersion("rappdirs")
[1] ‘0.3.1> rappdirs:::file_path(c("a", "b"))
[1] "a/b"

The solution/workaround for is:

> packageVersion("rappdirs")
[1] ‘0.3.2> rappdirs::user_cache_dir("foo", version = do.call(file.path, as.list(c("a", "b"))))
[1] "/home/hb/.cache/foo/a/b"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.