How do I run Rcpp Hello World? - r

OK, so I have created an R package foo with function Rcpp.package.skeleton. I have also compiled the Hello World C++ file with
R CMD SHLIB foo/src/rcpp_hello_world.cpp
However, when I call rcpp_hello_world I get an error:
> source("foo/R/rcpp_hello_world.R")
> rcpp_hello_world()
Error in .Call("rcpp_hello_world", PACKAGE = "foo") :
"rcpp_hello_world" not available for .Call() for package "foo"
Any clues?

"Package skeleton" implies that you are supposed to follow the creation of a (simple, skeleton) package with (optionally) building the package (into a tar.gz) as well as installing it.
Once installed you can load it and then you can in fact execute the new function.
Alternatively, you can work on the fly via Rcpp Attributes and/or the inline package.

To run the "hello world" example, do the following:
Start R and install the Rcpp package by:
install.packages('Rcpp')
Generate Rcpp template, in R:
Rcpp.package.skeleton("mypackage")
Next, create an archive for the package:
R CMD build mypackage
Exit R. You should see a folder "mypackage" generated. Type the following to check the package:
R CMD check mypackage
Now, you will see an archive mypackage_1.0.tar.gz. Install it:
R CMD INSTALL mypackage_1.0.tar.gz
Let's run the package in R. Start R and do the following:
library('mypackage')
rcpp_hello_world() # Try the C++ function generated in the template
[[1]]
[1] "foo" "bar"
[[2]]
[1] 0 1

Related

R package installation error: "cannot remove earlier installation"

I'm currently making a R package (tessellation) which includes some C code. When I do some modifications and click "Install and restart" in RStudio, I get this error:
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source tessellation
* installing to library 'C:/PortableApps/R/R-4.1.2/App/R-Portable/library'
* installing *source* package 'tessellation' ...
ERROR: cannot remove earlier installation, is it in use?
When I do "Restart R session" before "Install and restart", sometimes this works, sometimes not. So I have to close the project and reopen it, and this is annoying. Do you know what could I do to work more conveniently?
You need to be sure that the process is not being used (or loaded into R's search path) for the package. I suggest stopping all R sessions, deleting the installed package folder manually, start an R vanilla session, check the attached packages/search path, and finally attempt re-installation/building the package:
# from terminal
R --vanilla
# check search path
ls()
search()
# attempt installing package
devtools::install() # or devtools::build()

Rscript sees a different R version than the one installed

I am trying to call an R script from inside Python however in the 1st line of the R script I am calling the "lubridate" library which gives me the following error
Error in readRDS(pfile) : cannot read workspace version 3 written by R 4.0.2; need R 3.5.0 or newer Calls: library -> find.package -> lapply -> FUN -> readRDS Execution halted
After calling .libPaths() in Rstudio I find that the library directories are :
[1] "/zhome/c9/f/144817/R/x86_64-pc-linux-gnu-library/4.0" [2] "/appl/R/4.0.2-mkl2020/lib64/R/library"
However, I put the print(.libPaths()) command at the first line of my R script and then run Rscript from inside my Python code, and the path that gives me is the following:
[1] "/appl/R/3.2.2/lib64/R/library"
It seems that the .libPaths() sees different library directories when called from Rstudio and when called from inside Python withe the Rscript command.
Any advice here, how I could make it look to the right library location?

Loading R package in vignette for the package itself

I am currently building an R package, call it mypackage. I generated a package vignette using devtools::use_vignette("my-vignette"), and filled in my-vignette with content. Of course, because it is a package vignette, I need to demonstrate the use of the functions and data contained in this package itself. However, I'm having issues loading the package and its contents when knitting the vignette.
All of the code chunks in the vignette run without error on my local computer, even after restarting R and clearing the workspace. However, when I attempt to build the package using devtools::build(), the build fails with:
Error in library(mypackage): there is no package called `mypackage` Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> library Execution halted
The full output in the Console from devtools::build() is:
* checking for file ‘/path/to/mypackage/DESCRIPTION’ ... OK
* preparing ‘mypackage’:
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
* creating vignettes ... ERROR
Quitting from lines 39-44 (my-vignette.Rmd)
Error: processing vignette 'my-vignette.Rmd' failed with diagnostics:
'my_function' is not an exported object from 'namespace:mypackage'
Execution halted
devtools::document() runs without error, but devtools::build_vignettes() aparently reaches a different error where it cannot use a specific function (but seems to be able to load the package itself):
> devtools::build_vignettes()
Building mypackage vignettes
Moving basic-mypackage-vignette.html, basic-mypackage-vignette.R to inst/doc/
Copying basic-mypackage-vignette.Rmd to inst/doc/
> devtools::build()
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file --no-environ --no-save --no-restore --quiet CMD build \
'/path/to/mypackage' --no-resave-data --no-manual
* checking for file ‘/path/to/mypackage/DESCRIPTION’ ... OK
* preparing ‘auctestr’:
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
* creating vignettes ... ERROR
Quitting from lines 39-44 (my-vignette.Rmd)
Error: processing vignette 'my-vignette.Rmd' failed with
diagnostics:
could not find function "my_function"
Execution halted
Error: Command failed (1)
> devtools::document()
Updating mypackage documentation
Loading mypackage
I assume this is because the package itself does not exist in my R library; I am just loading that package using devtools::load_all() when doing development. How can I get around this and load my package in order to use its functions and data in the package vignette? Specifically, how can I make the package in its functions available to whatever environment knitr is using to knit the vignette?
I have seen other package vignettes load the package directly using library() without issue; for example, in the stringr vignette.
I hope readers can evaluate this without a fully reproducible example; reproducing the entire example is difficult because it requires a full R package directory structure that is difficult to replicate or share here.
I do load directly my package in my vignettes (library(my_package)).
But considering your error, it might not be the problem...
Can you make sure that my_function is indeed exported. You should add the following code ate the end of the documentation of your function.
#' #export
I added a chunk
```{r, include=FALSE}
devtools::load_all(".")
```
at the beginning of the vignette to work around the problem.
However, this doesn't fix the actual problem, as this should not be necessary.
Alternatively you could use the ::: notation, especially if you are not exporting that function, e.g.
mypackage:::my_function()

The dbplyr package is installed and working on travis, why do I receive a message "there is no package called 'dbplyr'"?

I'm testing an open source package called eutradeflows on travis-ci.
I placed the following instruction in .travis.yml to install dbplyr:
r_packages:
- dbplyr
I check that dbplyr works by calling one of its function with the following .travis.yml instruction:
- Rscript -e "dplyr::check_dbplyr(); dbplyr::translate_sql(x +1)"
The dbplyr::translate_sql function does indeed return a result in travis see build 268358848 line 1162:
$ Rscript -e "dplyr::check_dbplyr(); dbplyr::translate_sql(x +1)"
<SQL> "x" + 1.0
But testthat tests that use other dbplyr functions inside my package complain, see build 268358848 line 1292:
"there is no package called 'dbplyr'"
The dbplyr is installed and working, how is that possible?
You just need to add package dbplyr to the DESCRIPTION file of your package.

Rcpp Rcpp.package.skeleton("mypackage") "rcpp_hello_world" not available for .Call() for package "mypackage"

I've managed to get the Rcpp.package.skeleton to INSTALL in Windows by the following commands at the R prompt -
Rcpp.package.skeleton("mypackage")
system("R CMD build mypackage")
system("R CMD INSTALL mypackage")
library(mypackage)
This creates the mypackage.dll. However when I do the following commands -
rcpp_hello_world <- function(){ .Call( "rcpp_hello_world", PACKAGE = "mypackage")}
rcpp_hello_world()
I get the following error:
Error in .Call("rcpp_hello_world", PACKAGE = "mypackage") :
"rcpp_hello_world" not available for .Call() for package "mypackage"
I run sessionInfo() and I get the following:
attached base packages:
[1] tools stats graphics grDevices utils datasets methods base
other attached packages:
[1] mypackage_1.0 inline_0.3.13 Rcpp_0.11.1
Stating that my new mypackage is there.
Are there any further checks I can do to see what is happening? Any ideas?
FWIW, I just got a similar error while retrofitting an existing R-only package with Rcpp, and the problem was a missing useDynLib(mypackage) in NAMESPACE.
The package is tested extensively before every release, including on the Windows-using Win-builder. The regression tests even include building a package this way via a call to package.skeleton().
It is also rebuilt by CRAN post-release. Many people use it.
For all of those people, tests are appropriate and when long long cannot be used, it is #define-d away.
Now, you insist on building in a non-standard way: no source I know of recommends calling R CMD INSTALL via system(). I suspect you simply have a $PATH mishap and find another wrong g++ version.
I would suggest to do what the documentation suggests and run
R CMD INSTALL mypackage*tar.gz
in a cmd.exe prompt.
If the package NAMESPACE file contains the line
useDynLib(mypackage, .registration = TRUE)
(perhaps via a roxygen line #' #useDynLib, .registration = TRUE), then it is necessary to remove PACKAGE = "mypackage" from .C / .Call function calls:
i.e.
.Call( "rcpp_hello_world", PACKAGE = "mypackage")
becomes .Call("rccp_hello_world").

Resources