Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put an r script into a package

Tags:

package

r

I'm writing my first R package and have made a successful build with documentation using roxygen2 and added data sets.

However, I would also like ship an example script with how I use the functions in the r package. But I don't know where to put it.

Let's say I have created MyPackage. I have put my function scripts in the /R folder. Let's say I have:

foo1.R
foo2.R
foo3.R

Somewhere I'd also like to put a script with my workflow. Let's say I have a file, MyWorkflow.R:

library(MyPackage)
load(file='inData.R') # Loads indata variables A, B and C
X=foo1(A)
Y=foo2(X,B)
Z=foo3(Y,C)

Can I do this? If so, where do I put it? Is it an OK procedure - or generally frowned upon?

Any help or thoughts are appreciated. Thanks. Carl

Edit:

I looked at the link on demo/ and exec/, but didn't understand the exec/ folder thing. Grateful if you could clarify/exemplify/point to good uses of...

If I understand correctly, I'm not looking for an example or demo/, since the script won't necessarily be executable without tweaking by the user (e.g. to provide input data or paths). I "just" want to add an example script showing how I work with these functions.

I realise I should probably dive into the world of vignettes, but have difficulty in finding the time/oomph/energy to do so.

I also saw that there's the inst/ folder. Could you shed some light on the different uses of these options or hint at good examples of where they've been used (I often find examples more informative than reading an explanatory text that's above my level - I often get the feeling of being like a dog looking at a ceiling fan ;)

Will add info to the GitHub README. Thx for good suggestion!

like image 235
CarlBrunius Avatar asked Dec 02 '15 13:12

CarlBrunius


People also ask

How do you package a file in R?

To add documentation to an R package, you need to create a subdirectory “ man ” containing a set of files, one per function, in a special R Documentation format ( . Rd ). These will be the source for the documentation for each function; R processes them to create plain text, PDF, and HTML versions.

What package do you need for %>% in R?

The R packages dplyr and sf import the operator %>% from the R package magrittr. Of course the package must be loaded before by using e.g.


2 Answers

Created inst/Workflow_Example/workflow.R. Upon build & reload, a Workflow_Example folder was created in the library with workflow.R script in it.

In combination with an explanatory remark in the README, this looks like what I was after. Problem solved or am I not seeing something obvious? Am I e.g. violating conventions/conduct/good practice?

like image 190
CarlBrunius Avatar answered Sep 20 '22 12:09

CarlBrunius


You could either put it in demo/ or exec/ depending on the format of the script. See here for more details. I would mention the workflow and where it lives in the README regardless, and if you host your code on Github, you could create a wiki to describe the workflow and place the script there. This would be similar to what nrussell has mentioned in a comment above.

like image 39
Chris C Avatar answered Sep 19 '22 12:09

Chris C