Error when trying to create a package with stan model

I’m having some issues creating a package (internal proprietary) that includes a Stan model. I’m not sure if this is a Stan issue or if this is just an issue with my computer setup. Any help is much appreciated, thanks!

Output from running check package

* using log directory 'C:/Users/spinkney/Documents/slam_research/survlift.Rcheck'
* using R version 3.5.1 (2018-07-02)
* using platform: x86_64-w64-mingw32 (64-bit)
* using session charset: ISO8859-1
* using options '--no-manual --as-cran'
* checking for file 'survlift/DESCRIPTION' ... OK
* this is package 'survlift' version '0.0.0.9000'
* package encoding: UTF-8
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking serialization versions ... OK
* checking whether package 'survlift' can be installed ... ERROR
Installation failed.
See 'C:/Users/spinkney/Documents/slam_research/survlift.Rcheck/00install.out' for details.
* DONE
Status: 1 ERROR
* installing *source* package 'survlift' ...
** libs
no DLL was created
ERROR: compilation failed for package 'survlift'
* removing 'C:/Users/spinkney/Documents/slam_research/survlift.Rcheck/survlift'
In R CMD INSTALL
Package: survlift
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R: 
    person(given = "First",
           family = "Last",
           role = c("aut", "cre"),
           email = "first.last@example.com")
Description: What the package does (one paragraph).
License: What license it uses
Encoding: UTF-8
LazyData: true
Depends: 
    R (>= 3.4.0),
    Rcpp,
    methods
Imports: 
    data.table,
    rstan (>= 2.17.3),
    rstantools (>= 1.5.0)
LinkingTo: 
    BH (>= 1.66.0-1),
    Rcpp,
    RcppEigen (>= 0.3.3.4.0),
    StanHeaders (>= 2.17.2),
    rstan (>= 2.17.3)
NeedsCompilation: yes
SystemRequirements: GNU make
RoxygenNote: 6.1.1

Contents of my .R/Makevars.win file

CXX14FLAGS=-O3 -Wno-unused-variable -Wno-unused-function
CXX14 = $(BINPREF)g++ -m$(WIN) -std=c++1y
CXX11FLAGS=-O3 -Wno-unused-variable -Wno-unused-function

Can you tell us what is in that file?

That is the part above

* installing *source* package 'survlift' ...
** libs
no DLL was created
ERROR: compilation failed for package 'survlift'
* removing 'C:/Users/spinkney/Documents/slam_research/survlift.Rcheck/survlift'
In R CMD INSTALL

Did you specify the stan_files argument when calling rstantools::rstan_package_skeleton()? Or otherwise, are there any Stan files in stc/stan_files?

Yes the Stan file is saved in src/stan_files

You do have to make sure that the name of the Stan program starts with a letter and does not have any spaces or odd characters in it. Possibly no periods either, except for the one before the stan extension. If that isn’t it. Try

R CMD INSTALL --preclean survlift

to see if there is a better error message buried in there.

Thanks for the help late on a Monday, I had to leave the office for the evening. I’ll report back in the morning.

Unfortunately, nothing additional:

spinkney@CSVASPINKNEYWIN10  ~/Documents/slam_research (master)
$ R CMD INSTALL --preclean survlift
In R CMD INSTALL
* installing to library 'C:/Program Files/R/R-3.5.1/library'
* installing *source* package 'survlift' ...
** libs
no DLL was created
ERROR: compilation failed for package 'survlift'
* removing 'C:/Program Files/R/R-3.5.1/library/survlift'

The package compiles fine when removing the src/stan_files folder. The only file in that folder is titled survlift.stan. In case there was a possible error in the stan program, I tried removing that stan program and putting in the 8-schools stan program (retitled as survlift.stan) but receive the same error.

What happens if you do

R -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_models/survlift.stan

from src directory?

Is this the intended way to do what you asked?

spinkney@CSVASPINKNEYWIN10  ~/Documents/slam_research/survlift/src (master)
$ R -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_models/survlift.stan
ARGUMENT 'stan_models/survlift.stan' __ignored__


R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))
Error in file(filename, "r", encoding = encoding) :
  cannot open the connection
Calls: source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file '../tools/make_cc.R': No such file or directory
Execution halted

Do you have a directory called tools with a make_cc.R file inside of it?

I did not have that directory or file. I added them and got the make_cc.R from github rstanarm. In the src directory I also added init.cpp, Makevars, Makevars.win.

At least I’m now getting a different error. Running devtools::install(local=TRUE) in R now produces the following error:

√  checking for file 'C:\Users\spinkney\Documents\slam_research\survlift/DESCRIPTION' ...
-  preparing 'survlift':
√  checking DESCRIPTION meta-information ...
-  cleaning src
-  checking for LF line-endings in source and make files and shell scripts (688ms)
-  checking for empty or unneeded directories
-  building 'survlift_0.0.0.9000.tar.gz'
   
Running "C:/PROGRA~1/R/R-35~1.1/bin/x64/Rcmd.exe" INSTALL \
  "C:\Users\spinkney\AppData\Local\Temp\RtmpKQjK8l/survlift_0.0.0.9000.tar.gz" --install-tests 
* installing to library 'C:/Program Files/R/R-3.5.1/library'
* installing *source* package 'survlift' ...
** libs
"C:/PROGRA~1/R/R-35~1.1/bin/x64/Rscript" -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_files/survlift.stan
Error in file(con, "r") : invalid 'description' argument
Calls: make_cc -> cat -> readLines -> file
Execution halted
make: *** [Makevars.win:17: stan_files/survlift.cc] Error 1
ERROR: compilation failed for package 'survlift'
* removing 'C:/Program Files/R/R-3.5.1/library/survlift'
* restoring previous 'C:/Program Files/R/R-3.5.1/library/survlift'
In R CMD INSTALL

When I run the code snippet you requested to run in src I get:

spinkney@CSVASPINKNEYWIN10  ~/Documents/slam_research/survlift/src (master)
$ R -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" stan_models/survlift.stan
ARGUMENT 'stan_models/survlift.stan' __ignored__


R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))
Error in file(fname, "rt") : invalid 'description' argument
Error in get_model_strcode(file, model_code) : cannot open model file ""
Calls: make_cc -> <Anonymous> -> get_model_strcode
Execution halted

It is in a weird state. Did you create the survlift directory with rstantools::rstan_package.skeleton(stan_files = file.path("to", "survlift.stan"))? It should have set everything up for your correctly.

Yep, this is due to my ignorance on rstantools. I ran a devtools create package prior to rstantools skeleton. Looks like it just skipped creating a bunch of necessary files. I started over using what you have above and everything is working now.