How to load .Rdata from an R package? - r

I am developing an R package. In the package, I stored some .Rdata file from the simulation results. Now I would like to load/use those .Rdata in the rmarkdown so that I can write vignettes. I can load those data from my computer. But my question is how do I load data from my own package directory? Because in the future some users of the package may need to follow the same code from the vignette and reproduce the results or plots.

Assuming you used devtools::use_data(), you can use library(YOUR_PACKAGE); data("dataset") in your vignette, where "dataset" is the data you're talking about. Running devtools::load_all() will make the data available to you as you are writing the vignette.

Related

How do I reference a model object in the example section of documentation in an R package

I'm creating an R package. In my package project folder, I have a model object saved as a .rds file. in a folder called data. Additionally, I have a function saved in the R folder.
How do I reference the model object in the example section of documentation of the function in the R folder?
I also have the same questions for dataset files saved as .rds files in the data folder. How do I reference those datasets in the example section when documenting functions.
Everything you need you could find out in the R packages book which is distributed for free. Hadley Wickham and Jenny Bryan
This part of the book is directly connected with your question but please check others too
I recommend usage of usethis package, which togather with roxygen2 does most things you need.

How to use an R package's own data when writing a vignette

I have written most of an R package and now wish to write a vignette that uses my own data, that is already in the package. The data is correctly stored as my_data.Rda in the Data folder, and when the package is loaded I can access it in the Console, for instance by using data(my_data).
My problem comes when, using usethis::use_vignette("my_vignette") , I want to include something like this (much more complex in practice, of course) in the vignette:
The mean of my_data is given by
```{r} data(my_data)
mean(my_data)
```
When I knit the vignette I get the message
"Error in assert_engine(is_numeric, x, .xname = get_name_in_parent(x),
: object 'my_data' not found"
I have looked at this post: How to add external data file into developing R package? but that addresses external data.
What am I doing wrong?
I have created a minimal R package with the relevant Rmd file in the vignettes folder. link to Github
I think you are supposed to use
data(my_dataset, package = "my_package")
to load your package's data into the session where your vignette is built.
Could you confirm that your datasets are stored inside the ./data directory of your package as *.rda files

Downloading CSV_GDX_tools.exe package

I have to work with GAMS and R to extract data however I am a new R user and never have used GAMS before. I need to download a package called CSV_GDX_tools.exe and I have no idea what that is...
When I try to install it in R, I get this error message:
Warning in install.packages :
package ‘CSV_GDX_tools.exe’ is not available (for R version 3.3.2)
Can anyone please help me how and where I can download the package?
First, that does not sound like an R-package, but rather like an outdated utility program for GAMS.
I say outdated, because GAMS now has built-in the functionality to convert GDX (GAMS data files) to CSV files, which can be read by any statistics program including R.
GAMS also gives you the option of exporting your data to an SQLite database file (.db), which can be read by R.
Have a look here:
https://www.gams.com/help/index.jsp?topic=%2Fgams.doc%2Fuserguides%2Fmccarl%2Fgdx_utilities.htm

How can i specify R code in the data files in R package

In my R package, I want R to load some data from an external server to main memory. Looking around i found the option of plain R files in the data file section of a package - http://cran.r-project.org/doc/manuals/R-exts.html#Data-in-packages.
But then i couldnt find literature on how the R code will work in the data file section of a package nor how to invoke it.
It would be useful if someone can point me to a sample package too.

What methods exist for distributing a semi-live dataset with an R package?

I am building a package for internal use using devtools. I would like to have the package load in data from a file/connection (that differs depending on the date package is built). The data is large-ish so having a onetime cost of parsing and loading the data during package building is preferable.
Currently, I have a data.R file under R/ that assigns the data to package-level variables, the values are assigned during package installation (or at least that's what appears to be happening). This less than ideal setup mostly works. In order to get all instances of the package to have the same data I have to distribute the data file with the package (currently it's being copied to inst/ by a helper script before building the package) instead of just having it all be packaged together. There must be a better way.
Such as:
Generate .rda files during package building (but this requires not running the same code during package install)
I can do this with a Makefile but that seems like overkill
Can I have R code that is only run during package building and not during install?
Run R code in data/
But the data is munged using code in the package in question. I can fix that with Collate (I think) but then I have to maintain the order of all of the .R files (but with that added complexity I might as well use a Makefile?)
Build two packages, one with all of the code I want, one with the data.
Obvious, clever things I've not thought of.
tl;dr: What are some methods for adding a snapshot of dynamically changing data to an R package frozen for deployment?
As #BenBolker points out in the comments above, splitting the dataset out into a different package has precedent in the community (most notably the core package datasets) and has additional benefits.
The separation of functions from data also makes working on historic versions of the data easier to do with the up to date functions.
I currently have an tools-to-munge package and a things-to-munge package. Using a helper script I can build the tools-to-munge and setup a Suggests (or Depends) in the DESCRIPTION of both packages to point to the appropriate incrementing version of the packages. After the new tools-to-munge package has been built I can build the things-to-munge package as necessary using the functions in the tools-to-munge package.

Resources