tmpdir <- tempfile(pattern = "dir")
dir.create(tmpdir)
writeLines("this file signifies that its directory is the project root",
this.path::path.join(tmpdir, ".here"))
FILE.R <- this.path::path.join(tmpdir, "src", "R", "script1.R")
dir.create(this.path::dirname2(FILE.R), recursive = TRUE)
this.path:::.writeCode({
this.path::this.path()
this.path::this.proj()
## use 'here' to run another script located nearby
this.path::here("script2.R")
## or maybe to read input from a file
this.path::here(.. = 2, "input", "data1.csv")
## but sometimes it is easier to use the project root
## this allows you to move the R script up or down
## a directory without changing the .. number
this.path::this.proj("input", "data1.csv")
}, FILE.R)
source(FILE.R, echo = TRUE)
unlink(tmpdir, recursive = TRUE)
Run the code above in your browser using DataLab