I want to write a stan file which includes another prior.stan
and run it by cmdstan_model
function in R. It works well locally, but the #include prior.stan
will fail while running in a docker image.
My ~/test_docker/
folder contains
- an empty file named
prior.stan
and - an R script
test.R
# test_docker/test.R
library(cmdstanr)
f <- write_stan_file("functions {
#include prior.stan
}")
model <- cmdstan_model(f, include_path = getwd())
I run it on a docker image I built. The cmdstan_model()
function would cause an error of not finding the include path.
docker pull xinranmiao/test_stan:01
docker run -v ~/test_docker/:/test_docker -it --rm=true xinranmiao/test_stan:01 /bin/bash
cd test_docker
Rscript test.R
Error message:
This is cmdstanr version 0.5.3
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
- CmdStan path: /root/.cmdstan/cmdstan-2.31.0
- CmdStan version: 2.31.0
Syntax error in '/tmp/Rtmpiln4CL/model-10200ceee1.stan', line 2, column 4, include error:
-------------------------------------------------
1: functions {
2: #include prior.stan
^
3: }
-------------------------------------------------
Could not find include file prior.stan in specified include paths.
make: *** [make/program:50: /tmp/Rtmpiln4CL/model-10200ceee1.hpp] Error 1
Error: An error occured during compilation! See the message above for more information.
Execution halted
Notes:
- Setting the
include_path
argument to be an absolute path or relative path (e.g.,.
or~
) won’t solve this problem. - I think the problem relates to the
write_stan_file
function. Alternatively if we have atest.stan
file that includesprior.stan
, then thecmdstan_model
function will work.
# test_docker/test.stan
functions {
#include prior.stan
}
# this will work in R
model <- cmdstan_model("./test.stan", include_path = ".", compile = TRUE)
- The error only occurs when I test it on docker. A local test will be fine. Here is my
Dockerfile
.
FROM rocker/tidyverse
RUN apt-get update
RUN apt-get install -y libudunits2-dev vim
RUN Rscript -e "install.packages('cmdstanr', repos = c('https://mc-stan.org/r-packages/', getOption('repos')))"
RUN Rscript -e "cmdstanr::install_cmdstan()"
RUN chmod -R 777 /usr/local/lib/ /root/ /tmp/
ENV HOME /root/
ENV USER root
Any help will be appreciated!! Thank you!