Creating R package with stan

Hello,

I read few post on this but I could not get my head around.

I am creating an R package of my algorithm that uses stan. I use devtools for this.

rstan is loaded as external library in the default file

DESCRIPTION

Depends: R (>= 3.3.0)
License: GPL-v3
Encoding: UTF-8
LazyData: true
Imports:
rstan
RoxygenNote: 6.0.1

However with the command

install_github(“stemangiola/ARMET_tc”)
I get the error

Downloading GitHub repo stemangiola/ARMET_tc@master
from URL https://api.github.com/repos/stemangiola/ARMET_tc/zipball/master
Installing ARMET.tc
trying URL 'https://cran.rstudio.com/src/contrib/rstan_2.15.1.tar.gz'
Content type 'application/x-gzip' length 725186 bytes (708 KB)
==================================================
downloaded 708 KB

Error: Could not find build tools necessary to build rstan

Can you give me some tips?

Thanks

Does the standard rstan work for you? My first idea would be that you may not have the necessary C++ compiler available (see https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started and especially the bit on installing Toolchain/Rtools, which even with these instructions is a nightmare in my experience on Windows machines), but if rstan works for you, then that is perhaps not it, after all.

Yes RStan is installed and working. I am using linux. You can replicate the error calling

install_github(“stemangiola/ARMET_tc”)

Sounds like your package doesn’t set paths correctly so it can’t find the compiler. You might get more detailed messages by building from the command line (not sure if there are verbosity options to install_github), but trying R CMD build is a good next step.

Sorry it seems to be a problem of install_github, not relative to rstan

I just tried to install your package but if fails on loading. Check the first lines of this file: https://github.com/stemangiola/ARMET_tc/blob/master/R/ARMET_tc.R

rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

You are calling a rstan function outside of any function, so basically you are assuming that rstan is loaded before your package.

devtools::install_github("stemangiola/ARMET_tc")
Downloading GitHub repo stemangiola/ARMET_tc@master
from URL https://api.github.com/repos/stemangiola/ARMET_tc/zipball/master
Installing ARMET.tc
trying URL 'https://cran.rstudio.com/src/contrib/tibble_1.3.3.tar.gz'
Content type 'application/x-gzip' length 91511 bytes (89 KB)
==================================================
downloaded 89 KB

Installing tibble
Running command /usr/lib/R/bin/R 
Arguments:
CMD
INSTALL
/tmp/RtmpKfBAAK/devtools1eb5331f1cfb/tibble
--library=/home/jouni/R/x86_64-pc-linux-gnu-library/3.4
--install-tests

* installing *source* package ‘tibble’ ...
** package ‘tibble’ successfully unpacked and MD5 sums checked
g++  -I/usr/share/R/include -DNDEBUG  -I"/home/jouni/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include"    -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-8kDblV/r-base-3.4.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c RcppExports.cpp -o RcppExports.o
** libs
g++  -I/usr/share/R/include -DNDEBUG  -I"/home/jouni/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include"    -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-8kDblV/r-base-3.4.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c matrixToDataFrame.cpp -o matrixToDataFrame.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o tibble.so RcppExports.o matrixToDataFrame.o -L/usr/lib/R/lib -lR
installing to /home/jouni/R/x86_64-pc-linux-gnu-library/3.4/tibble/libs
** R
** inst
** tests
** preparing package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (tibble)
Running command /usr/lib/R/bin/R 
Arguments:
CMD
INSTALL
/tmp/RtmpKfBAAK/devtools1eb548db7832/stemangiola-ARMET_tc-f2652c6
--library=/home/jouni/R/x86_64-pc-linux-gnu-library/3.4
--install-tests

* installing *source* package ‘ARMET.tc’ ...
** R
** data
*** moving datasets to lazyload DB
** preparing package for lazy loading
Error in rstan_options(auto_write = TRUE) : 
  could not find function "rstan_options"
Error : unable to load R code in package ‘ARMET.tc’
ERROR: lazy loading failed for package ‘ARMET.tc’
* removing ‘/home/jouni/R/x86_64-pc-linux-gnu-library/3.4/ARMET.tc’
 
Installation failed: run(bin, args = real_cmdargs, stdout_line_callback = real_callback(stdout),      stderr_line_callback = real_callback(stderr), stdout_callback = real_block_callback,      stderr_callback = real_block_callback, echo_cmd = echo, echo = show,      spinner = spinner, error_on_status = fail_on_status, timeout = timeout) : System command error

Thanks actually, my version of install_github was giving problem before that. Now is working fine.

Thanks

1 Like