devtools build_vignette can't find functions - r

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.

Related

Can I add additional R packages to Overleaf?

Is there any way to add packages that knitr doesn't know? I use knitr in Overleaf, and I need to use packages "pacma", "nonlinearTseries" and "RHRV". But when I tried to install them it didn't work.
I tried do add them with library function but it get me an error massage.
library("RHRV")
Error in library("RHRV"): there is no package called ’RHRV’
Unfortunately, installing a package on Overleaf is not possible, see e.g. https://tex.stackexchange.com/questions/566054/cannot-install-r-packages-in-the-knitr-environment-overleaf
You could try to contact the team behind Overleaf and ask them politely, but they can have reasons why they cannot help you.
As Overleaf is simply a TeX distribution and compiliation, with some R thrown into it, you you could consider installing R locally. Installing a TeX distribution can be more overwhelming than installing a operating system. Luckily, for knitr, the author behind made an R-package, tinytex (https://yihui.org/tinytex/) - which does almost everything for you.

Link to vignette from readme.rda

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.

R package not found, but present in correct library

I have a problem with the R package "gridExtra". Everything is ok, when I go into R and load it, but when I execute a python script, which among others also loads this package, everything stops. I don't understand it, since I checked the library and the right library is selected.
It's a little complicated, because the script is from someone else. It can be accessed from here: https://github.com/guigolab/ggsashimi/blob/master/sashimi-plot.py. The package is loaded at row 430. Loading the previous packages was no problem, after installing them into the custom library. During the python script, a custom R script is generated and executed. Within this R script, the package is loaded usign the function library(gridExtra).
The error message is:
Error in library(gridExtra) : there is no package called ‘gridExtra’
Execution halted
I checked, if the generated R script really accesses the right library and it does, so there is no problem with the library selection.

imported packages in devtools

I develop my R package in RStudio. I use load_all() from devtools (either typing in the console or from the Build menu), which seems to work fine. If I then work on an R function in my package and source it, when I try and run the function I need to load the libraries it depends on, despite these other packages being imported (through the DESCRIPTION file).
So for example, I will get the message:
could not find function "trellis.par.get"
and I need to load the lattice package etc.
Is this expected behaviour? I would have thought devtools would have imported these other package dependencies, or am I misunderstanding something here?
Thanks
David

R CMD check does not respect selective code evaluation in knitr code chunks

I am building a package in R 3.1.0 on Windows 7 32-bit (and also on a different machine running Windows 8 64bit) and I am using knitr to write vignettes using Markdown. I am hoping to publish this package on CRAN so I am using R CMD check to check that my demos, datasets and vignettes all behave properly. I keep my vignettes as .Rmd files (rather than making them outside of the package building process and stashing them in inst/doc) because they serve as extra tests on my package and aren't very big anyway.
My problem is that R CMD check fails when building my vignettes, even though if I run R CMD build and then R CMD INSTALL --build everything works out fine. Looking at the log file, it appears to be failing because it tries to evaluate code that I have explicitly told knitr NOT to evaluate. As a generic example, if I write
```{r example-chunk eval=c(1:3, 5:6), tidy=FALSE}
foo = 5
bar = 3
## don't evaluate the next line
file.show("i/dont/really/exist.csv")
## ok, start evaluating again
foobar = foo*bar
```
In a .Rmd file, running R CMD check will fail because it will try to evaluate line 4. However, the chunk will be correctly evaluated if I run R CMD build mypackage and then R CMD install --build mypackage.tar.gz (I know this because I can go to my Rlibs folder and find the flawless html vignettes in mypackage/doc. Similarly, the chunk will also be evaluated correctly I if run R CMD Sweave to build the vignette.
If you want to try this yourself, the package I am building (where I am running into the issue) is on Github: https://github.com/mkoohafkan/flowdurr-edu. You can look at raw/packagemaker.html for instructions, hopefully it's straightforward (the R code runs through the process of making the package directory, building the help files and copying some manually edited files into the package directory). R CMD check fails on all of my vignettes: when building flowdurr-datasets.Rmd, it insists on evaluating a line with a fake path even though I told it not to. When building hspf-analysis.Rmd, R CMD check insists on evaluating a line I excluded because it takes a really long time to complete (using rgenoud to fit some distribution parameters). R CMD check also fails on vignette.Rmd, but for a different reason; I purposely throw errors to show examples of what you can't do with a particular function, and while knitr doesn't have a problem with it R CMD check sure does!
EDIT: My build script was getting some hate so I made this dummy package that reproduced the problem on both of my machines. It illustrates that 1) R CMD check evaluates a line that it shouldn't, and 2) R CMD check does not support error evaluation in a vignette, even though knitr will write error output without issue.
So I guess my question is: What is it that R CMD check is doing differently from R CMD Sweave and R CMD build/install when it comes to vignette building, and is there anything I can do to make R CMD check respect knitr's 'eval' specification? Note that if I use eval=FALSE, R CMD check will respect it and everything is fine; the problem only occurs if I try selective evaluation of a chunk.
I have added vignette engines with the suffix _notangle in the knitr development version 1.6.2. For the original vignette engine knitr::foo, you can use the new engine knitr::foo_notangle to disable tangle (e.g. knitr::knitr_notangle, knitr::rmarkdown_notangle, ...).
If you do not want to wait for the next version of knitr to be on CRAN (which might take a while), you can certainly register a package vignette engine by yourself. Hint: you can make use of existing engines in tools::vignetteEngine(package = 'knitr') so you do not have to completely redefine the knitr vignette engines.
It seems that the issue is more nuanced than I originally thought, so this might not get resolved anytime soon. my workaround is to:
manually build the vignettes using R CMD Sweave
Copy the HTML outputs to inst/doc
Delete the vignettes folder (or add entries to .Rbuildignore--thanks #Ben!)
Build and check
It's not ideal, but right now it looks like the only way for my package to get through CRAN checks.

Resources