Problem running devtools::install() on simple rstan-based package


#1

Hi.

I’m trying to get started writing an rstan-based package, initially just for personal use to see how to do it.

I encountered the problem discussed here, so uninstalled rstantools and installed it from github:

devtools::install_github("stan-dev/rstantools", ref = "dd1009c")

I then used

rstantools::rstan_create_package('junk', rstudio = FALSE)

to create a directory structure.

I put a stan file into junk/inst/stan, and tried to run:

devtools::install(local = FALSE)

to check that compilation was working, but got an error:

* installing to library ‘/Library/Frameworks/R.framework/Versions/3.5/Resources/library’
* installing *source* package ‘junk’ ...
** libs
Error: unexpected symbol in:
"cat(' ', sep = '
cat(system.file('include"
Execution halted

I suspected I had something wrong in my tool chain, so I followed the instructions in this blog post to make sure I was up to date, and reinstalled rstan but I’m still getting the same error.

Is my example too minimal to install, or is there something wrong in my setup?

  • Operating System: MacOS 10.14.2
  • RStan Version 2.18.2
  • Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))

Following reinstallation of toolchain and rstan, my ~/.R/Makevars is blank. But the issue was present before this.

  • Output of devtools::session_info("rstan")

    ─ Session info ───────────────────────────────────────────────────────────────
    setting value
    version R version 3.5.0 (2018-04-23)
    os macOS 10.14.2
    system x86_64, darwin15.6.0
    ui X11
    language (EN)
    collate en_GB.UTF-8
    ctype en_GB.UTF-8
    tz Europe/London
    date 2018-12-21

    ─ Packages ───────────────────────────────────────────────────────────────────
    package * version date lib source
    assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.5.0)
    backports 1.1.3 2018-12-14 [1] CRAN (R 3.5.0)
    BH 1.66.0-1 2018-02-13 [1] CRAN (R 3.5.0)
    callr 3.1.0 2018-12-10 [1] CRAN (R 3.5.0)
    cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.0)
    colorspace 1.3-2 2016-12-14 [1] CRAN (R 3.5.0)
    crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.0)
    desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.0)
    digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.0)
    fansi 0.4.0 2018-10-05 [1] CRAN (R 3.5.0)
    ggplot2 3.1.0 2018-10-25 [1] CRAN (R 3.5.0)
    glue 1.3.0 2018-07-17 [1] CRAN (R 3.5.0)
    gridExtra 2.3 2017-09-09 [1] CRAN (R 3.5.0)
    gtable 0.2.0 2016-02-26 [1] CRAN (R 3.5.0)
    inline 0.3.15 2018-05-18 [1] CRAN (R 3.5.0)
    labeling 0.3 2014-08-23 [1] CRAN (R 3.5.0)
    lattice 0.20-38 2018-11-04 [1] CRAN (R 3.5.0)
    lazyeval 0.2.1 2017-10-29 [1] CRAN (R 3.5.0)
    loo 2.0.0 2018-04-11 [1] CRAN (R 3.5.0)
    magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.0)
    MASS 7.3-51.1 2018-11-01 [1] CRAN (R 3.5.0)
    Matrix 1.2-15 2018-11-01 [1] CRAN (R 3.5.0)
    matrixStats 0.54.0 2018-07-23 [1] CRAN (R 3.5.0)
    mgcv 1.8-26 2018-11-21 [1] CRAN (R 3.5.0)
    munsell 0.5.0 2018-06-12 [1] CRAN (R 3.5.0)
    nlme 3.1-137 2018-04-07 [1] CRAN (R 3.5.0)
    pillar 1.3.1 2018-12-15 [1] CRAN (R 3.5.0)
    pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.5.0)
    plyr 1.8.4 2016-06-08 [1] CRAN (R 3.5.0)
    prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.0)
    processx 3.2.1 2018-12-05 [1] CRAN (R 3.5.0)
    ps 1.2.1 2018-11-06 [1] CRAN (R 3.5.0)
    R6 2.3.0 2018-10-04 [1] CRAN (R 3.5.0)
    RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 3.5.0)
    Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.0)
    RcppEigen 0.3.3.5.0 2018-11-24 [1] CRAN (R 3.5.0)
    reshape2 1.4.3 2017-12-11 [1] CRAN (R 3.5.0)
    rlang 0.3.0.1 2018-10-25 [1] CRAN (R 3.5.0)
    rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.0)
    rstan 2.18.2 2018-11-07 [1] CRAN (R 3.5.0)
    scales 1.0.0 2018-08-09 [1] CRAN (R 3.5.0)
    StanHeaders 2.18.0-1 2018-12-13 [1] CRAN (R 3.5.0)
    stringi 1.2.4 2018-07-20 [1] CRAN (R 3.5.0)
    stringr 1.3.1 2018-05-10 [1] CRAN (R 3.5.0)
    tibble 1.4.2 2018-01-22 [1] CRAN (R 3.5.0)
    utf8 1.1.4 2018-05-24 [1] CRAN (R 3.5.0)
    viridisLite 0.3.0 2018-02-01 [1] CRAN (R 3.5.0)
    withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.0)

    [1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library


#2

A little extra info.

If I download the rstanarm code from github and run devtools::install( local = FALSE) in the base directory of that package I get the same error.


#3

I would pass the paths to your Stan program(s) as part of this.


#4

Thanks for the suggestion.

I deleted the package and tried using rstantools::rstan_create_package('junk', stan_files = '~/8schools.stan', rstudio = FALSE) to create the directory structure, but the error is the same.


#5

Back from my Christmas/New Year break and I’ve been trying to get back into this.

As I couldn’t get it working and there was a new version of R, I decided to go for a clean install. Removed R and old packages, installed the new version and then installed packages from scratch.

I still needed the github version of rstantools as above.

The problem persists. So I also tried following the minimal rstan package example vignette, with the modifications that I used:

  • rstantools::rstan_create_package() and
  • put lm.stan in inst/stan (as per the Read-and-delete-me file).

Running:

rstantools::rstan_create_package(path = "rstanlm", rstudio = FALSE)
setwd('rstanlm')
devtools::install(local = FALSE)

Gave the same error as previously:

devtools::install(local=FALSE)
✔  checking for file ‘/Users/xxxxxx/src/R/rstanlm/DESCRIPTION’ ...
─  preparing ‘rstanlm’:
✔  checking DESCRIPTION meta-information ...
─  cleaning src
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
   Removed empty directory ‘rstanlm/man’
   Removed empty directory ‘rstanlm/src’
─  building ‘rstanlm_0.0.0.9000.tar.gz’
   Warning: invalid uid value replaced by that for user 'nobody'
   Warning: invalid gid value replaced by that for user 'nobody'

Running /Library/Frameworks/R.framework/Resources/bin/R CMD INSTALL \
  /var/folders/m9/lpvpvw_53qgg5tdwxvgdcylmj6fvfv/T//RtmprnfCv5/rstanlm_0.0.0.9000.tar.gz \
  --install-tests
* installing to library ‘/Users/xxxxxx/Library/R/library/3.5’
* installing *source* package ‘rstanlm’ ...
** libs
Error: unexpected symbol in:
"cat(' ', sep = '
cat(system.file('include"
Execution halted
clang++ -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -I"/Users/xxxxxx/Library/R/library/3.5/BH/include" -I"/Users/xxxxxx/Library/R/library/3.5/Rcpp/include" -I"/Users/xxxxxx/Library/R/library/3.5/RcppEigen/include" -I"/Users/xxxxxx/Library/R/library/3.5/rstan/include" -I"/Users/xxxxxx/Library/R/library/3.5/StanHeaders/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c RcppExports.cpp -o RcppExports.o
In file included from RcppExports.cpp:4:
In file included from /Users/xxxxxx/Library/R/library/3.5/RcppEigen/include/RcppEigen.h:25:
In file included from /Users/xxxxxx/Library/R/library/3.5/RcppEigen/include/RcppEigenForward.h:25:
In file included from /usr/local/clang6/include/c++/v1/iterator:416:
In file included from /usr/local/clang6/include/c++/v1/iosfwd:90:
/usr/local/clang6/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
              ^~~~~~~~~
1 error generated.
make: *** [RcppExports.o] Error 1
ERROR: compilation failed for package ‘rstanlm’
* removing ‘/Users/xxxxxx/Library/R/library/3.5/rstanlm’

Trying to run roxygen2::roxygenise(clean = TRUE) before the install gives the error which had resulted in installing rstantools from github.

Not sure what to try next. Anybody have any more suggestions?


#6

I think the problem may be in rstantools/include/sys/Makevars, found using grep -rF "cat(' '," * in my R library directory.

In a terminal window, I can type

Rscript -e "cat(' ', sep = '\n')"

and get:

ARGUMENT '')' __ignored__

Error: unexpected end of input
Execution halted

Whereas Rscript -e "cat(' ')" runs without error.


#7

Try with devtools::install_github("stan-dev/rstantools", ref = "dd1009cdfc95a0eab90988faea48e9a1f426d441")


#8

Thanks @bgoodri, I ran:

remove.packages("rstantools")
devtools::install_github("stan-dev/rstantools", ref = "dd1009cdfc95a0eab90988faea48e9a1f426d441")
rstantools::rstan_create_package(path = "rstanlm", rstudio = FALSE, stan_files = 'rstanlm_code/lm.stan')
setwd('rstanlm')
file.copy('../../R/rstanlm_code/lm_stan.R','R')
devtools::install(local = FALSE)

and got the same error as previously:

devtools::install(local = FALSE)
✔  checking for file ‘/Users/xxxxxx/src/R/rstanlm/DESCRIPTION’ ...
─  preparing ‘rstanlm’:
✔  checking DESCRIPTION meta-information ...
─  cleaning src
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
   Removed empty directory ‘rstanlm/man’
─  building ‘rstanlm_0.0.0.9000.tar.gz’
   Warning: invalid uid value replaced by that for user 'nobody'
   Warning: invalid gid value replaced by that for user 'nobody'

Running /Library/Frameworks/R.framework/Resources/bin/R CMD INSTALL \
  /var/folders/m9/lpvpvw_53qgg5tdwxvgdcylmj6fvfv/T//RtmpqQe7Sh/rstanlm_0.0.0.9000.tar.gz \
  --install-tests
* installing to library ‘/Users/xxxxxx/Library/R/library/3.5’
* installing *source* package ‘rstanlm’ ...
** libs
Error: unexpected symbol in:
"cat(' ', sep = '
cat(system.file('include"
Execution halted
clang++ -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error -I"/Users/xxxxxx/Library/R/library/3.5/BH/include" -I"/Users/xxxxxx/Library/R/library/3.5/Rcpp/include" -I"/Users/xxxxxx/Library/R/library/3.5/RcppEigen/include" -I"/Users/xxxxxx/Library/R/library/3.5/rstan/include" -I"/Users/xxxxxx/Library/R/library/3.5/StanHeaders/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c RcppExports.cpp -o RcppExports.o
In file included from RcppExports.cpp:4:
In file included from /Users/xxxxxx/Library/R/library/3.5/RcppEigen/include/RcppEigen.h:25:
In file included from /Users/xxxxxx/Library/R/library/3.5/RcppEigen/include/RcppEigenForward.h:25:
In file included from /usr/local/clang6/include/c++/v1/iterator:416:
In file included from /usr/local/clang6/include/c++/v1/iosfwd:90:
/usr/local/clang6/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
              ^~~~~~~~~
1 error generated.
make: *** [RcppExports.o] Error 1
ERROR: compilation failed for package ‘rstanlm’
* removing ‘/Users/xxxxxx/Library/R/library/3.5/rstanlm’

#9

Hi,

I am also having the same problem while trying to install a rstan interfacing package I wrote on my Mac.

Mac operating system: 10.13.6
RStan Version 2.18.2

Thanks!


#10

Try after doing devtools::install_github("stan-dev/rstantools", ref = " c3c59fb")


#11

Hi bgoodri,

Thanks for your quick response! Unfortunately after uninstalling and reinstalling rstantools I get the same error.

p.s. I think you meant devtools::install_github("stan-dev/rstantools", ref = "c3c59fb") with the space caused an odd github error.


#12

I’ve just been looking at c3c59fb on Github - I had tried that fix already. It lead to the "cat(' ', sep = ' problem going away but instead led to a lot of weird compiler warnings… Odd that it has different effect when in rstantools itself.


#13

Hi bgoodri,

Sorry for the amount of replies. Ater quitting R and installing the rstantools from Github again I do not get the error message.

However what rstantools::rstan_package_skeleton does is very different - it doesn’t seem to create an .Rproj file for example.

Thanks!


#14

OK, I’ll look into the missing .Rproj. It might have been intentional.


#15

I’m now playing around with that branch - it seems that rstan_create_package uses usethis and so does create an R.proj file but rstan_package_skeleton uses the base package.skeleton and so doesn’t.

I don’t really understand the differences between these functions…

Thanks!


#16

The version using usethis is definitely more modern. I think we’re keeping rstan_package_skeleton mostly for backwards compatibility.


#17

Okay great - I’ll use rstan_create_package and report back if there are more issues


#18

Sorry to bug you but when I create a rstan project with rstan_create_package I get a Read-and-delete-me-file containing the following:

Stan-specific notes:

* All '.stan' files containing stanmodel definitions must be placed in 'inst/stan'.
* Additional files to be included by stanmodel definition files
  (via e.g., #include "mylib.stan") must be placed in any subfolder of 'inst/stan'.
* Additional C++ files needed by any '.stan' file must be placed in 'inst/include',
  and can only interact with the Stan C++ library via '#include' directives
  placed in the file 'inst/include/stan_meta_header.hpp'.
* The precompiled stanmodel objects will appear in a named list called 'stanmodels',
  and you can call them with e.g., 'rstan::sampling(stanmodels$foo, ...)'

This contradicts the information in the Step by step guide for creating a package that depends on RStan vignette. Which should I follow? (Happy to fix this with a pull request if that would be helpful)


#19

Just to confirm that using the above (minus the space before the c3c59fb) allowed me to create and install the rstanlm package from the vignette without error.

There were a lot of compiler warnings though, and I think all of them report a -W<something> compiler setting. Are these avoidable through .R/Makevars? And would this be recommended?


#20

I can confirm that I can also build the package in the vignette - following the instructions in the Read-and-delete-me this branch created. I also get, however, all the nasty compiler warnings. A sample:

/mat/fun/autocorrelation.hpp:18:8: warning: function 'fft_next_good_size' is not needed and will not be emitted [-Wunneeded-internal-declaration]
size_t fft_next_good_size(size_t N) {