Rbuildignore and Excluding Directories

Rbuildignore and Excluding Directories

This appears to be an issue with RStudio. Using install from the devtools package seems to cause the folders to be ignored. Building and installing directly from the command line also seems to cause the folders to be ignored. The 'Build & Reload' button in RStudio, however, seems to not take into account the .Rbuildignore for those folders.

Exclude R files for R folder with .Rbuildignore

I think the .Rbuildignore file is useful for bundling of your package, and that's about it. If you want certain functionality in R/*.R files but only on your console when testing, then you may consider structuring the test files with a wrapping if expression.

For example, I'll the existence and true-ness of a variable named MYDEV to declare that I want to load the contents of the file.

if (exists("MYDEV") && MYDEV) {
stop("TEST")
}

In a default environment (MYDEV not defined), load_all, check, and document do not see the error. The use of exists(...) helps ensure that this will not error (object not found) in a different R instance, either on your computer or elsewhere.

If you set MYDEV <- TRUE, then all the devtools functions will see the stop and error accordingly. I believe there is no (reasonable) way to get the code sourced in load_all and not with document.

One problem you have with not erring with devtools::document is that it calls devtools::load_all internally, as such simple tests do not work. One could adapt a function from https://stackoverflow.com/a/62747050/3358272, with:

search_calling_pkg <- function(pkgs, funcs) {
# <borrowed from="rlang::trace_back">
frames <- sys.frames()
idx <- rlang:::trace_find_bottom(NULL, frames)
frames <- frames[idx]
parents <- sys.parents()[idx]
calls <- as.list(sys.calls()[idx])
calls <- purrr::map(calls, rlang:::call_fix_car)
#==# calls <- lapply(calls, rlang:::call_fix_car)
calls <- rlang:::add_pipe_pointer(calls, frames)
calls <- purrr::map2(calls, seq_along(calls), rlang:::maybe_add_namespace)
#==# calls <- Map(rlang:::maybe_add_namespace, calls, seq_along(calls))
# </borrowed>
calls_chr <- vapply(calls, function(cl) as.character(cl)[1], character(1))
ptn <- paste0("^(", paste(pkgs, collapse = "|"), ")::")
pkgres <- any(grepl(ptn, calls_chr))
funcres <- !missing(funcs) && length(calls_chr) &&
any(mapply(grepl, paste0("(^|::)", funcs, "$"), list(calls_chr)))
return(pkgres && funcres)
}

This function returns true if any of the package/function pairs are found, and this combination will "see" (error on) stop("TEST") with devtools::load_all but not with devtools::document:

if (search_calling_pkg("pkgload", "load_all") &&
!search_calling_pkg("devtools", "document")) {
stop("TEST")
}

Yes, this means you need search_calling_pkg somewhere in your environment, whether part of this package or not. Recall that if the .Rbuildignore file includes R/dev-myfeature.R, then package bundles and all down-stream users will never see this code. If you wanted to be even more defensive, you could use

if (exists("search_calling_pkg") &&
search_calling_pkg("pkgload", "load_all") &&
!search_calling_pkg("devtools", "document")) {
stop("TEST")
}

.Rbuildignore - how to exclude all invisible files?

Add this to .Rbuildignore (or/and in .Rinstignore if needed)

^[.].*$
^.*/[.].*$
  • First line to ignore hidden file(s) in folder
  • Second line to ignore hidden file(s) in sub-folder(s)

Exclude data sets from R package build

If you use .Rbuildignore you should first build then check your package (it's not a check-ignore). Here a few tests in a Debian environment and a random package:

l@np350v5c:~/src/yapomif/pkg$ ls
data DESCRIPTION man NAMESPACE R

l@np350v5c:~/src/yapomif/pkg$ R
> save(Formaldehyde, file = "data/formal.rda")

l@np350v5c:~/src/yapomif/pkg$ ls -l
totale 20
drwxr-xr-x 2 l l 4096 mag 1 01:31 data
-rw-r--r-- 1 l l 349 apr 25 00:35 DESCRIPTION
drwxr-xr-x 2 l l 4096 apr 25 01:10 man
-rw-r--r-- 1 l l 1189 apr 25 00:33 NAMESPACE
drwxr-xr-x 2 l l 4096 apr 25 01:02 R

l@np350v5c:~/src/yapomif/pkg$ ls -l data/
totale 4
-rw-r--r-- 1 l l 229 mag 1 01:31 formal.rda

Now i create exactly your .Rbuildignore

l@np350v5c:~/src/yapomif/pkg$ em .Rbuildignore
l@np350v5c:~/src/yapomif/pkg$ cat .Rbuildignore
^data/.+$

Ok let's build

l@np350v5c:~/src/yapomif/pkg$ cd ..
l@np350v5c:~/src/yapomif$ R CMD build pkg
> tools:::.build_packages()
* checking for file ‘pkg/DESCRIPTION’ ... OK
* preparing ‘yapomif’:
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
Removed empty directory ‘yapomif/data’
* building ‘yapomif_0.8.tar.gz’

Fine (you see the message about yapomif/data). Now check the package

l@np350v5c:~/src/yapomif$ R CMD check yapomif_0.8.tar.gz
> tools:::.check_packages()
* using log directory ‘/home/l/.src/yapomif/yapomif.Rcheck’
* using R version 3.1.0 (2014-04-10)
* using platform: x86_64-pc-linux-gnu (64-bit)
...

... everything as usual

Now let's check the file (moved to home directory to keep my
development dir clean)

l@np350v5c:~/src/yapomif$ mv yapomif_0.8.tar.gz ~
l@np350v5c:~/src/yapomif$ cd
l@np350v5c:~$ tar xvzf yapomif_0.8.tar.gz
l@np350v5c:~$ ls yapomif
DESCRIPTION man NAMESPACE R

so there is no data directory

BUT if

l@np350v5c:~/src/yapomif$ R CMD check pkg

...

Undocumented data sets:
‘Formaldehyde’

So, as stated, first build, then check.

HTH, Luca

Rbuildignore not ignoring .git

This one had a strange and unrelated solution, which I will post in case someone else runs into a similar issue. I should have seen the clue when I ran "build source package" (ordevtools::build() or devtools::check()) and in the long scrolling output would get a long series of

rm: /var/folders/1r/qj42pgb ... 3c4f: Operation not permitted
rm: /var/folders/1r/qj42pgb ... b6ad: Operation not permitted
rm: /var/folders/1r/qj42pgb ... s/e3: Directory not empty
rm: /var/folders/1r/qj42pgb ... c5d0: Operation not permitted

My guess is that when I transferred my work from a Windows machine to a Mac via external drive, some directory was corrupted or there was a permissions issue. If so, I could imagine my machine being unable to follow my .Rbuildignore, or exclude version control directories as mentioned in @user2554330's answer.

Ultimately, I downloaded a fresh version from Github (which I had been pushing to) and built/checked on that, and the issue was gone.

how can I stop r devtools::check() from noting .DS_Store

I restarted RStudio and it fixed itself. I have no idea why it didn't notice the changes in .Rbuildignore after the file was saved. I had saved and restarted the R session.

Retain files but exclude from compression R CMD build

You can build package with --no-resave-data option.

$ R CMD build --no-resave-data <pkg>


Related Topics



Leave a reply



Submit