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.
Related
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.
I am creating a question in r-exams that contains a graph made in TikZ, more specifically https://texample.net/tikz/examples/the-3dplot-package/. For its correct operation it is required that the 3dplot.sty file be in a certain R folder. In which folder should I include this file?
Error message in RStudio: "!LaTeX Error: File`3dplot.sty'not found".
If you only need it for one project, simply place the .sty file in the same folder as your .rmd file. The current working folder is the normally the first place latex searches for packages, before looking in your personal texmf folder or your tex distribution.
I would strongly recommend to install this in the texmf tree of your LaTeX installation. Then it is always found, no matter where you compile a LaTeX file.
Alternatively, you can also specify it using the header argument in include_tikz() with the full absolute file path:
include_tikz(..., header = "\\usepackage{/full/path/to/3dplot}")
Note that the .sty suffix is not to be included, even when using the full file path.
I have the following package structure:
mypackage/
|-- .Rbuildignore
|-- .gitignore
|-- DESCRIPTION
|-- NAMESPACE
|-- inst
|-- extdata
|-- mydata.csv
|-- vignettes
|-- R
|-- utils.R
`-- mypackage.Rproj
Currently I stored all the functions in R/ directory. My question is
where should I put scripts (e.g. named try_functions.R) to try the functions stored in R/, that scripts. It also use data stored in inst/extdata/
And in development process using RStudio, what's the workflow like to update and try the package after we add and fixed functions in R/.
It sounds to me like testthat is the package you are looking for. By "try", I presume you mean "test," and the way that it is canonically done for the testthat package is within a tests/testthat directory for the package.
Hadley's "Advanced R" book has a good deal more information about best practices, and you can find many good examples by looking at github.
Some excerpts from the docs:
Testing is a vital part of package development. It ensures that your
code does what you want it to do. Testing, however, adds an additional
step to your development workflow. The goal of this chapter is to show
you how to make this task easier and more effective by doing formal
automated testing using the testthat package.
And implementing:
To set up your package to use testthat, run:
devtools::use_testthat()
This will:
Create a tests/testthat directory.
Adds testthat to the Suggests field in the DESCRIPTION.
Creates a file tests/testthat.R that runs all your tests when R CMD
check runs. (You’ll learn more about that in automated checking.)
You also might look at the rprojroot package for referencing various places within the directory of the package.
The canonical place for keeping arbitrary R scripts is inst/ subdirectory.
Note that tests of your package functionality it is better to put in tests/ subdirectory. Loose scripts that are not tests (at least test of your package) should be in placed inst/. Those can be test scripts for checking deployment environment, test for checking production data quality, exec scripts to be plugged in crontab, whatever is useful/necessary in putting your package into action.
Quoting Writing R Extensions manual "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. 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 case-insensitively against the file and directory paths, e.g. doc/.*[.]png$ will exclude all PNG files in inst/doc based on the extension.
I have a rstudio addin package located here.
One of the addins allows the user to define a directory and it will copy a file that is located in the package to that directory.
the file is located:
atProjectManageAddins/inst/Docs/RMarkdownSkeleton.Rmd
And I am trying to copy it to the user defined directory with something like this:
file.copy("inst/Docs/RMarkdownSkeleton.Rmd",
paste0(Dir, FolderName, "/Reports/", FolderName, "_report.Rmd"))
Where I am trying to copy it from where it is in the package, to where the user defines it to be (Based on two separate arguments Dir and FolderName).
But this doesn't seem to work. My assumption is that I am not referring to the package directory in the correct way. I've tried ./Inst/, ~/Inst/ and maybe a couple more. My assumption now is that there is a more systematic reason for my inability to get file.copy() to work.
Any suggestions? Is this even possible?
Note that if I run the function locally via source() and runGadget(), it works just fine. Only when the package is installed and I use the RStudio addins GUI where it references the intalled package, does it fail. Thus, I'm quite certain I am not correctly defining the file path for the installed .Rmd files.
Edit: I've changed to the following, based on Carl's suggestion (as can be seen on github), but the files are still not being copied over.
file.copy(system.file("Docs","Rmarkdownskeleton.rmd",package="atProjectManageAddins"),
paste0(Dir, FolderName, "/Reports/", FolderName, "_report.Rmd"))
system.file is the best function for getting a file from a package. I believe this should work for you:
file.copy(system.file("Docs","Rmarkdownskeleton.rmd",package="atProjectManageAddins"),
paste0(Dir, FolderName, "/Reports/", FolderName, "_report.Rmd"))
You got the right idea putting the files in inst/.
Use this code to copy the file from the package dir to the current dir :
file.copy(from = file.path(path.package("packagename"), "path/to/file"),
to = file.path("path/to/file"), overwrite = T)
file.path creates a path by concatenating the strings passed to it (OS-specific separators are automatically added).
path.package retrieves the path of a loaded package. Files presents in inst/ are copied at the root of the package dir when installed, thus "path/to/file" here should be the path relative to your inst/ dir.
overwrite can be used to overwrite the file if it already exists.
In your specific case, this should do the trick :
file.copy(file.path(path.package("atProjectManageAddins"), "Docs/RMarkdownSkeleton.Rmd",
file.path(getwd(), "Reports", paste0(reportName, "_report.Rmd")))
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.