auto_test and sourcing using relative paths #41

Closed
ssimeonov opened this Issue Aug 21, 2012 · 5 comments

2 participants

@ssimeonov

I am not certain this is a legitimate issue but it is a behavior I've only experienced when using auto_test and not when I'm doing package development with devtools + testthat. The problem seems to arise from how auto_test sets up the loading of files. It breaks relative path sourcing. I've described the issue here.

Is there a way to make auto_test interoperate with relative path sourcing?

@hadley
Owner

That link doesn't mention auto_test at all?

@ssimeonov
@hadley
Owner

I really don't think that approach is a good idea in general - you're much better always using relative paths and understanding how working directories work in R. testthat should run tests with the working directory set to the tests directory, so relative paths should work without any additional hacks.

@hadley
Owner

You should only need to do:

source_dir("customizations/")
source("report_options.R")

i.e. always just use relative paths, and then ensure that however you're sourcing report_meta_data.r sets up the path directly - i.e. source("R/report/report_meta_data.r", chdir = T).

The R works with directories is rather different to ruby etc, but I think once you get your head around it, it's just as effective. (Particularly once you start using packages to manage code reuse)

@ssimeonov

@hadley your suggestion worked well, thanks.

One minor caveat: source_dir is from testthat and was not generally available to my app so I created sourceDir instead, which, in addition, does not load into a new environment by default the way source_dir does.

sourceDir <- function (path, pattern = "\\.[rR]$", env = NULL, chdir = TRUE) 
{
    files <- sort(dir(path, pattern, full.names = TRUE))
    lapply(files, source, chdir = chdir)
}
@ssimeonov ssimeonov closed this Dec 23, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment