Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use file.path instead of paste0 in cache_rds #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

avila
Copy link

@avila avila commented Oct 9, 2022

Very simple change, so that is not necessary to pass a trailing slash (/) to the dir argument of cache_rds. This is specially useful when the using file.path() itself for the dir argument, such as:

path <- list(checkpoints = file.path("checkpoints"))

long_calcs <- cache_rds(
  expr = {
    Sys.sleep(10)
  },
  dir = file.path(path$checkpoints)
)

Maybe it is a better practice to always use a trailing slash for folders and no slash for files, but it is not always practical. The output of here::here(), for example, doesn't differ between folder and files in this way.

Please let me know if I am doing something wrong, or just disregard the PR. Thank you for these great miscellaneous functions :)

@CLAassistant
Copy link

CLAassistant commented Oct 9, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I was intentionally using paste0() here mainly because I was considering the case of using this function inside knitr code chunks. The chunk option cache.path is not necessarily a dir but just a path prefix.

I totally understand that file.path() would make more sense without considering this special case, and just suggested two changes that would make the special case still work. Do they make sense to you?

BTW, xfun::mark_dirs() can add the trailing slash to dirs (but only if a dir exists).

@@ -112,7 +112,7 @@ cache_rds = function(
if (missing(dir) && !is.null(d <- knitr::opts_current$get('cache.path')))
dir = d
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dir = d
dir = I(d)

@@ -112,7 +112,7 @@ cache_rds = function(
if (missing(dir) && !is.null(d <- knitr::opts_current$get('cache.path')))
dir = d
}
path = paste0(dir, file)
path = file.path(dir, file)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
path = file.path(dir, file)
path = (if (inherits(dir, 'AsIs')) paste0 else file.path)(dir, file)

@yihui
Copy link
Owner

yihui commented Oct 14, 2022

Also the example is failing on Windows: https://github.com/yihui/xfun/actions/runs/3215127507/jobs/5256006471

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants