Link to vignette from readme.rda - r

I am building an R package. I have several vignettes that I would like to include links to in my README.Rmd.
I know that vignettes are to be built optionally when installing the package.
I do not really understand where I should start. I am in the process of building the package in R studio. I would the user to be able to see the vignette just by clicking at link in the readme on GitHub. Is this possible? How?
The following obviously does not work.
[The main vignette](vignettes/Vignette.html)

You can do this, but it might be more trouble than it's worth.
The problems are
Your package directories are different in the source on Github than they are when your package is installed in R. The link you give would be fine if you actually put Vignette.html in the vignettes directory, but when your package is installed, it will be in doc.
RStudio won't put the processed vignette in either of those locations by default if you just knit Vignette.Rmd.
You don't normally commit output files on Github.
So here's what you could do to work around this. Make the link look like
[The main vignette](doc/Vignette.html)
To make sure that file is there on Github, in RStudio create the doc directory and run
rmarkdown::render("vignettes/Vignette.Rmd", output_file="doc/Vignette.html")
You'll need to commit the output file and push it to Github, but you don't want to include it when you build the .tar.gz file, so you'll also need to add the lines
^doc$
^doc/Vignette.html$
to the .Rbuildignore file in the main package directory.
With all these changes I think your vignette will be visible on Github and also after you install the package in R.
A much simpler approach is just to tell the user to run
vignette("Vignette", package = "yourpackagename")
after installing the package, but this won't make it visible on Github.

Related

Questions about R package publishing and code visibility

When I use a package in R I install it and use it with loading it. Now what if I add a package which uses another package? Is this package automatically downloaded and loaded too? Or is it in general forbidden for a R package to use another package? I don't think that.
Suppose I want to publish a R package. Within my code, can I use functions from other packages and install and load these packages? Or how does this work when I need functions from other packages? Do I have to implement a message that this and that package is needed and that the user has to install and load it prior to it and I need to implement error catching functions in case the package cannot be found on the pc system?
When I want to publish a R package, can I use/call Java code within my package/code?
For a package which was already published - so let's take just as an example the fGarch package - I would like to see the complete code. How can I see this? I know that R is open source and I think it is more or less possible to just enter a function empty and get the code displayed, but sometimes this does not work and especially my question is: Is there a way I can look into the whole code of the package?
For a package which was already published, is it possible to see and look into all files which were submitted? So like a repository as git where all files are submitted - the code itself and further files which are needed like description files or whatever - and I can see these files and look into them?
Furthermore regarding this post here and hiding functions: Is there code in a R package which I cannot see as an end user? This refers also to my previous question, how can I or which way can I see the whole code in a R package?
I guess you have a few different questions here. Let's take them in the order you asked them:
What if I add a package which uses another package? Is this package automatically downloaded and loaded too? Or is it in general forbidden for a R package to use another package?
It is certainly not forbidden for an R package to use another R package. In fact, the majority of R packages rely on other packages.
The source code for each R package must include a text-based DESCRIPTION file in the root directory. In this file you will find (among other things) a "Depends" field, and an "Imports" field. Together, these two fields list all the other packages required to use this package. If a user doesn't already have these other packages installed in their local library, R will install them automatically when it installs the requested package.
If your package lists a dependency in "Depends", then the dependency package is attached whenever your package is attached. Thus if you looked at the source code for a package called "foo" and you see that its DESCRIPTION file contains the line
Depends: bar,
you know that when you call library(foo) in your R console, you have effectively done library(bar); library(foo)
This isn't always ideal. The package foo might only need a couple of functions from package bar, and bar might contain some other functions whose names could clash with other commonly used functions. Therefore, in general, if you are writing a package and you only want to use a few functions from another package, it would be better to use "Imports" rather than "Depends" to limit the number of unnecessary symbols being added to your user's search path.
Suppose I want to publish a R package. Within my code, can I use functions from other packages and install and load these packages
Yes, you can use functions from other packages. The simplest way to do this is to include the name of the package in the Depends field of your DESCRIPTION file.
However, when using just a few functions from another package inside your own package, best practice is to use the "Imports" field in the DESCRIPTION file, and use a namespace qualifier for the imported function in your actual R code. For example, if you wanted to use ggplot from the ggplot2 package, then inside your function you would call it ggplot2::ggplot rather than just ggplot.
If you publish your package for others to use, the dependencies will be installed automatically along with your package if the user calls install.packages with the default settings. For example, when I did:
install.packages("fGarch")
I got the associated message:
#> also installing the dependencies ‘timeSeries’, ‘fBasics’, ‘fastICA’
Do I have to implement a message that this and that package is needed and that the user has to install and load it prior to it and I need to implement error catching functions in case the package cannot be found on the pc system?
No, not in general. R will take care of this as long as you have listed the correct packages in your DESCRIPTION file.
When I want to publish a R package, can I use/call Java code within my package/code?
R does not have a native Java API, but you can use your own Java code via the rJava package, which you can list as a dependency for your package. However, there are some users who have difficulty getting Java to run, for example business and academic users who may use R but do not have Java installed and do not have admin rights to install it, so this is something to bear in mind when writing a package.
For a package which was already published - so let's take just as an example the fGarch package - I would like to see the complete code. How can I see this?
Every package available for download from CRAN has its source code available. In the case of fGarch, its CRAN page contains a link to the gzipped tarball of the source code. You can download this and use untar in R to review all the source code. Alternatively, many packages will have an easily-found repository on Github or other source-control sites where you can examine the source code via a browser. For example, you can browse the fGarch source on Github here.
For a package which was already published, is it possible to see and look into all files which were submitted? So like a repository as git where all files are submitted - the code itself and further files which are needed like description files or whatever - and I can see these files and look into them?
Yes, you can look at all the sources files for all the packages uploaded to CRAN on Github at the unofficial Github CRAN mirror here
Is there code in a R package which I cannot see as an end user? This refers also to my previous question, how can I or which way can I see the whole code in a R package?
As above, you can get the source code for any package via CRAN or Github. As you said, you can look at the source code for exported functions just by typing the name of that function into R. For unexported functions, you can do the same with a triple colon. For example, ggplot2:::adjust_breaks allows you to see the function body of the unexported function adjust_breaks from ggplot2. There are some complexities when an object-oriented system like S4, ggproto or R6 is used, or when the source code includes compiled C or C++ code, but I haven't come across a situation yet in which I was not able to find the relevant source code after a minute or two with an R console and a good search engine.

How to include static Vignettes in R Package using R.rsp

I would like to include static vignettes in my R package that will show up for users using the R function browseVignettes().
I need the vignettes to be static because the vignettes take too long to build, causing me to fail CRAN checks on win-builder if they are included dynamically.
The package R.rsp seems to provide exactly what I want; described here: https://cran.r-project.org/web/packages/R.rsp/vignettes/R_packages-Static_PDF_and_HTML_vignettes.pdf .
I followed the instructions from R.rsp. I have a vignettes folder that contains a .html file with a corresponding .Rmd file and a .html.asis file, as directed. I also have the appropriate code in the DESCRIPTION file. With these settings, I pass CRAN tests on win-builder, travis, etc. However, when I download the package from github, it says that there are no vignettes associated with the package. Am I missing something about the R.rsp package? Is this capability outdated? Do I need to copy the built vignettes from the Vignettes folder into an inst/doc folder? (I have seen conflicting information about vignettes folder vs inst/doc folder).
Is using something like R.rsp::asis the best way to include vignettes that take too long to build to include dynamically? Another method that I have seen is to include the vignettes folder in .Rbuildignore, and then include a note in the README file telling users that they can build the vignettes separately from github in order to view them. I am also considering including links in the README to external vignettes. Is this a better approach? Which might CRAN prefer?
Add build_vignettes=TRUE to your install_github call.

devtools build_vignette can't find functions

If I use devtools::use_vignette("my-vignette") in my package, running devtools::build_vignette() works fine.
However, once I add a call to anything from my package, it stops working, with error could not find function "myfunc". If I add a library(mypackage) call, I get the error there is no package called 'mypackage'.
(I should note that my package checks, builds & installs perfectly cleanly [with no vignettes], and running devtools::load_all() also works fine for interactive sessions.)
I know that if I build & install my package, I can then get the vignettes built. This seems like a really inefficient and dangerous way to develop; essentially forcing me to re-build and re-install the entire package on every commit, to test that the vignette isn't breaking.
Is there another way to get the vignette to recognize the package-in-progress?
If you are using RStudio IDE (which is very helpful for package developpement), you can render your Rmd document created by devtools::use_vignette, by clicking on the Knit button. It will create a preview version of your vignette.
By the way, RStudio IDE provides you with helpful shortkeys and buttons to execute your Rmd document chunk by chunk to test if it's working.
If you are not using RStudio IDE, you could render your document without building the package by using the function rmarkdown::render.
However, in order to be working, your vignette requires your package to be loaded. So, as you said, you'll have to call library(mypackage) and so your package have to be installed.
You can install your package without the vignette in the command line with devtools::install(build_vignette = FALSE). In the RStudio IDE, the button Build & Reload is enougth to intall your package.`
Another solution for non user of Rstudio IDE is to use devtools::load_all(path to your package) in your vignette in order to simulate the installation of your package in the vignette environment. Then you can build your vignette with devtools::build vignette whithout needing to install your package before.
I should underline that vignette is build automatically when your build your package. So, when development are finish, replace in the vignette devtools::load_all by library because your package is loaded before building the vignette when you build a package.
If you look up Hadley Wickham's packages in github, you will see he includes a library(xyz) at the top of his vignette, e.g. https://github.com/tidyverse/dplyr/blob/master/vignettes/dplyr.Rmd
Then his recommended way to build vignettes works:
You can build all vignettes from the console with
*devtools::build_vignettes()*, but this is rarely useful. Instead
use devtools::build() to create a package bundle with the vignettes
included.
I believe this is what you will need to submit a package to CRAN.
It is a slow development cycle, though, so for active coding, you can insert a line with devtools::load_all() to use the knit in RStudio.
I know there is already an answer but this is how I solved the same issue when using a hand written vignette.
I'm not sure if this will work for build_vignette() but when I was having trouble knitting my vignette because of this same error, what I had to do was:
<path/to/your/Rpackage>/NAMESPACE I had to add export(myFuncName) for each function I want to use in the vignette along with useDynLib(myPackageName) once.
I had to specify a VignetteBuilder option in the <path/to/your/Rpackage>/DESCRIPTION file for me it is VignetteBuilder: knitr.

Creating a package in R using RStudio

I've created a bunch of files:
init.r
auth.r
class.r
modules/status.r
modules/mgmt.r
modules/core.r
modules/mcf.r
The source of the init.r file is:
# initiation of package
# include libraries
library(RCurl);
library(rjson);
# include files
source('auth.r');
source('class.r');
# extend class
source('modules/status.r');
source('modules/mgmt.r');
source('modules/core.r');
source('modules/mcf.r');
How do I go about creating a package out of this? The init.r file obviously needs to be initiated first.
Start with following the steps in this video:
Build an R Package in under 2 minutes with RStudio
Then read more about RStudio's Package Development feature, and also Hadley Wickam's Package basics.
See Writing R Extensions for the process of making a package. You might want to use package.skeleton to get started.
But essentially,
get rid of your init.r file,
put all your other .R files in the R directory
write Depends: RCurl, rjson in your DESCRIPTION file.
1. Build the prerequisites:
To build R Packages using RStudio, you should have the following prerequisites like
R,
RStudio,
Rtools,
Basic MikTex,
roxygen2 and devtools packages.
2. Create the RPackage project in RStudio and add all your R files (init.r,auth.r,class.r,modules/status.r,modules/mgmt.r,modules/core.r,modules/mcf.r) under the R folder of the project.
3. Add the documentation by editing the package description file. Build the project and now your package is ready to use.
it won't take more than 15 minutes to build a simple package.
If you wish to have step by step explanation to create a simple package, please visit this blog.
https://veeramaninatarajanmca.wordpress.com/2017/02/10/how-to-create-a-simple-package-in-r-using-rstudio/

Where to put package vignettes for CRAN submission?

From the Writing R Extensions Manual, I read that
As from R 2.14.0 the preferred location for the Sweave sources is the
subdirectory vignettes of the source packages, but for compatibility
with earlier versions of R, vignette sources will be looked for in
inst/doc if vignettes does not exist.
However, when I create a vignettes subdirectory of the package source, when I run devtools::check() or R CMD check I get a warning for Package vignette(s) without corresponding PDF. If I put the vignette (.Rnw and .pdf) in inst/doc the check completes without complaints. I tried looking in my library at installed packaged and did not see any directories named vignettes. Should I still use the deprecated location?
You put the .Rnw sources in vignettes/ as you did, but you missed out a critical step; don't check the source tree. The expected workflow is to build the source tarball and then check that tarball. Building the tarball will create the vignette PDF.
R CMD build ../foo/pkg
R CMD check ./pkg-0.4.tar.gz
for example will build a source package tarball from the sources in ../foo/pkg creating the .tar.gz package in the current directory with the package name and version appended. Then you run R CMD check on that source package.
If you want your vignette built for you put it in vignettes/ and build the source package. At some future date, R Core may remove the ability to build vignettes from inst/doc so go with the advised location now and avoid check the sources directly.
I had a hard time interpreting this too.
I believe the intention is that you should put the .Rnw file in vignettes/ and the PDF (suitably compacted) in inst/doc/, which technically agrees with the documentation if you read carefully enough. (That is, it says the sources should go in vignettes/. I don't see where it says in so many words that you ought to put the corresponding PDF in inst/doc/, but it doesn't not say it, and that interpretation seems to make R CMD check happy ...)
The resolution is in #GavinSimpson's answer (i.e. one is expected to build the tarball and then check it, rather than checking the source directory itself). (My two cents is that it might be best if R-core officially deprecated (and eventually removed) direct source checking rather than confusing all of us groundlings ...)

Resources