Can I access an external file when testing an R package? - r

I am using the testthat package to test an R package that is within a larger repository. I would like to test the contents of a file outside of the R package.
Can I reference a file that is located outside of an R package while testing?
What I have tried
A reproducible example can be downloaded as MyRepo.tar.gz
My repository is called "myRepo", and it includes an R package, "myRpkg" and a folder full of miscellaneous scripts
~/MyRepo/
~/MyRepo/MyRpkg
~/MyRepo/Scripts
The tests in "MyRpkg" are in the /tests/ folder
~/myRepo/myRpkg/tests/test.myscript.R
And I want to be able to test a file in the Scripts folder:
~/MyRepo/Scripts/myscript.sh
I would like to read the script to test the contents of the first line doing something like this:
check.script <- readLines("../../../Scripts/myscript.sh")[1]
expect_true(grepl("echo", check.script))
This works fine if I start from the MyRepo directory:
cd ~/MyRepo
R CMD check MyRpkg
But if I move to another directory, it fails:
cd
R CMD check MyRepo/MyRpkg

As it says in R-exts
The directory tests is copied to the check area, and the tests are run with the copy as the working directory and with R_LIBS set to ensure that the copy of the package installed during testing will be found by library(pkg_name).
By default, the check directory is created in the current directory. Thus when running R CMD check from ~/MyRepo, the tests directory is copied to ~/MyRepo/MyRpkg.Rcheck/tests and hence
check.script <- readLines("../../../Scripts/myscript.sh")[1]
is interpreted as
check.script <- readLines("~/MyRepo/Scripts/myscript.sh")[1]
as required. However starting from ~/ would imply
check.script <- readLines("~/Scripts/myscript.sh")[1]
which isn't what you want. A work-around is to specify the directory in which the check directory is created, i.e.
R CMD check -o MyRepo MyRepo/MyRpkg
so that the copied tests directory has the same "grandparent" as the original tests directory.
Still, I wonder why the file must be external to the package. If you want to use the file in the package tests, it would make sense to include the file in the package. You could create an inst directory and put the Scripts directory in there, so that the Scripts directory will be copied to the package directory on installation and then
check.script <- readLines("../foo/Scripts/myscript.sh")[1]
could be used inside the test script, since the package is installed in MyRpkg.Rcheck/foo during R CMD check. Alternatively you could create an exec directory and put the script file in there, then
check.script <- readLines("../foo/exec/myscript.sh")[1]
would work. As both of these solutions only need to find the package installed during testing it wouldn't matter where you ran R CMD check from. See Package subdirectories and Non-R scripts in packages for more info.

Related

How to add external data folder into developing R package? [duplicate]

In the documentation, R suggests that raw data files (not Rdata nor Rda) should be placed in inst/extdata/
From the first paragraph in: http://cran.r-project.org/doc/manuals/R-exts.html#Data-in-packages
The data subdirectory is for data files, either to be made available
via lazy-loading or for loading using data(). (The choice is made by
the ‘LazyData’ field in the DESCRIPTION file: the default is not to do
so.) It should not be used for other data files needed by the package,
and the convention has grown up to use directory inst/extdata for such
files.
So, I have moved all of my raw data into this folder, but when I build and reload the package and then try to access the data in a function with (for example):
read.csv(file=paste(path.package("my_package"),"/inst/extdata/my_raw_data.csv",sep=""))
# .path.package is now path.package in R 3.0+
I get the "cannot open file" error.
However, it does look like there is a folder called /extdata in the package directory with the files in it (post-build and install). What's happening to the /inst folder?
Does everything in the /inst folder get pushed into the / of the package?
More useful than using file.path would be to use system.file. Once your package is installed, you can grab your file like so:
fpath <- system.file("extdata", "my_raw_data.csv", package="my_package")
fpath will now have the absolute path on your HD to the file.
You were both very close and essentially had this. A formal reference from 'Writing R Extensions' is:
1.1.3 Package subdirectories
[...]
The contents of the inst subdirectory will be copied recursively
to the installation directory. Subdirectories of inst should not
interfere with those used by R (currently, R, data, demo,
exec, libs, man, help, html and Meta, and earlier versions
used latex, R-ex). The copying of the inst happens after src
is built so its Makefile can create files to be installed. Prior to
R 2.12.2, the files were installed on POSIX platforms with the permissions in the package sources, so care should be taken to ensure
these are not too restrictive: R CMD build will make suitable
adjustments. To exclude files from being installed, one can specify a
list of exclude patterns in file .Rinstignore in the top-level
source directory. These patterns should be Perl-like regular
expressions (see the help for regexp in R for the precise details),
one per line, to be matched(10) against the file and directory paths,
e.g. doc/.*[.]png$ will exclude all PNG files in inst/doc based on
the (lower-case) extension.

How to put an R script into a dockerfile?

I am trying to add my R script into a dockerfile. The beginning of the file (loading base image, installing necessary packages) works fine when I run it in my terminal, but I keep getting this error when it gets to the line that contains the R script I want to run:
Step 15/17 : COPY /Users/emma/Documents/folder1/examples/question-1/model-1.r .
COPY failed: stat /var/lib/docker/tmp/docker-builder376572603/Users/emma/Documents/folder1/examples/question-1/model-1.r: no such file or directory
I am already running the terminal out of the "question-1" directory (my shell command looks like this) :
Emmas-MacBook-Air-2:question-1 emma$
and the R script file "model-1.r" is in that folder. What am I doing wrong in detailing the path to the R script? Do I have to somehow transform the script before adding it to the dockerfile?
Thank you
I believe, that you have to specify relative (to your build folder) path to copy from. Source:
Multiple resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.
And file to copy should be inside context of the build. So if your Dockerfile is located in folder A, then the objects you would like to copy should be placed in the folder A or it's subfolders.

R Package unable to access contents from `inst` folder [duplicate]

In the documentation, R suggests that raw data files (not Rdata nor Rda) should be placed in inst/extdata/
From the first paragraph in: http://cran.r-project.org/doc/manuals/R-exts.html#Data-in-packages
The data subdirectory is for data files, either to be made available
via lazy-loading or for loading using data(). (The choice is made by
the ‘LazyData’ field in the DESCRIPTION file: the default is not to do
so.) It should not be used for other data files needed by the package,
and the convention has grown up to use directory inst/extdata for such
files.
So, I have moved all of my raw data into this folder, but when I build and reload the package and then try to access the data in a function with (for example):
read.csv(file=paste(path.package("my_package"),"/inst/extdata/my_raw_data.csv",sep=""))
# .path.package is now path.package in R 3.0+
I get the "cannot open file" error.
However, it does look like there is a folder called /extdata in the package directory with the files in it (post-build and install). What's happening to the /inst folder?
Does everything in the /inst folder get pushed into the / of the package?
More useful than using file.path would be to use system.file. Once your package is installed, you can grab your file like so:
fpath <- system.file("extdata", "my_raw_data.csv", package="my_package")
fpath will now have the absolute path on your HD to the file.
You were both very close and essentially had this. A formal reference from 'Writing R Extensions' is:
1.1.3 Package subdirectories
[...]
The contents of the inst subdirectory will be copied recursively
to the installation directory. Subdirectories of inst should not
interfere with those used by R (currently, R, data, demo,
exec, libs, man, help, html and Meta, and earlier versions
used latex, R-ex). The copying of the inst happens after src
is built so its Makefile can create files to be installed. Prior to
R 2.12.2, the files were installed on POSIX platforms with the permissions in the package sources, so care should be taken to ensure
these are not too restrictive: R CMD build will make suitable
adjustments. To exclude files from being installed, one can specify a
list of exclude patterns in file .Rinstignore in the top-level
source directory. These patterns should be Perl-like regular
expressions (see the help for regexp in R for the precise details),
one per line, to be matched(10) against the file and directory paths,
e.g. doc/.*[.]png$ will exclude all PNG files in inst/doc based on
the (lower-case) extension.

testInstalledPackage in custom libs folder

Question : How do I make tools::testInstalledPackage work when I have a custom lib path defined in Rprofile.site.
I'm not an experience unix user so I might be wrong about this. Theres a line in tools::testInstalledPackage (shown below) which I suppose runs R in vanilla mode, i.e. my custom lib path in Rprofile.site does not get added in.
cmd <- paste(shQuote(file.path(R.home("bin"), "R")),
"CMD BATCH --vanilla --no-timing", Ropts, shQuote(Rfile),
shQuote(failfile))
In this case, when I try to test the package zoo, tools::testInstalledPackage returns a zoo-Ex.Rout.fail file with an error saying that there is no package called 'zoo' which makes sense as vanilla R does not contain my custom Lib folder.
Is there a way to use tools::testInstalledPackage to test my packages in my custom folder? Or do I have to copy the folder into the default file path
p.s. my current workaround is to create a new function without the --vanilla and attach it to the tools namespace but I don't think its a very elegant solution.

What roxygen should I put when I use a function of another package in my function

I am writing many functions and I am trying to document using roxygen2
I use the futile.logger package a lot, say I use the flog.debug function in a function. What #* should I use to document it ?
First, being aware of your
sessionInfo()
getwd() # your R's working directory
.libPaths() # your R's library location
Step0 Download and install the necessary packages:
library(roxygen2)
library(devtools)
library(digest)
Step1 Put all your related ".R" files (yourfunction1.R, yourfunction2.R, yourfunction3.R) to your R's working directory.
Step2 Create your package skeleton in your R's working directory:
Be sure that there is no folder named "yourpackage" in your R's working directory before running the following command. (from R's console)
package.skeleton(name = "yourpackage", code_files = c("yourfunction1.R", "yourfunction2.R", "yourfunction3.R"), path = ".")
After running package.skeleton, the folder yourpackage is created in your R's Working Directory.
Delete Read-and-delete-me file from Windows Explorer.
Delete "yourpackage-package.Rd" file in YourR'sWorkingDirectory\yourpackage\man folder
(Do NOT delete "yourpackage.Rd" file in YourR'sWorkingDirectory\yourpackage\man folder!)
Step3 At the end of the preamble of your ".R" file (yourfunction.R), put the following (if you had not done it so in Step1):
#' #importFrom futile.logger flog.debug
#' #export
yourfunction <- function(...) {...
Step4 In the DESCRIPTION file of your package, in the Imports part, add:
Imports:
futile.logger(>= VersionNumber)
where VersionNumber is the version number of the futile.logger package you are using. You can find the version number by right-click any function (from yourpackage) in Object Browser of RevolutionREnterprise; and going the bottom of the resultant .html help file. There, the version number of the package is shown.
In Step2, package.skeleton automatically produced a NAMESPACE file whose content is:
exportPattern("^[[:alpha:]]+")
Do not handle this NAMESPACE file manually.
Step5 roxygenize the package you wanna create ("yourpackage")
library(roxygen2)
roxygenize("yourpackage")
Upon roxygenization, the content of the NAMESPACE file of yourpackage is automatically converted from exportPattern("^[[:alpha:]]+") to
# Generated by roxygen2: do not edit by hand
export(yourfunction)
importFrom(futile.logger,flog.debug)
Step6 Build your package:
(first, delete "src-i386" and "src-x64" folders (if any) in YourR'sWorkingDirectoryFolder\yourpackage folder from Windows Explorer)
(Be sure again that there is no "yourpackage-package.Rd" file in YourR'sWorkingDirectory\yourpackage\man folder. If there is, delete it before building)
build("yourpackage")
Step7 Install your package:
install("yourpackage")
Step8 Check that all is going well by loading your package and running a function in the package.
library(yourpackage)
yourfunction(6,1,2) # "yourfunction" is a function in the package "yourpackage"
Step9 Check that your package is loadable to CRAN (Comprehensive R Archieve Network) (if you wanna share your package):
(first, delete "src-i386" and "src-x64" folders (if any) in YourR'sWorkingDirectoryFolder\yourpackage folder from Windows Explorer)
(Be sure again that there is no "yourpackage-package.Rd" file in YourR'sWorkingDirectory\yourpackage\man folder. If there is, delete it before checking)
From DOS Command Prompt:
Start – cmd - Enter. Pass to R's working directory (your R's working directory is known via getwd()) and do CRAN check:
cd C:\Users\User\Documents\Revolution
R CMD check yourpackage
From R's console:
devtools::check("C:/Users/User/Documents/Revolution/yourpackage")

Resources