Rmarkdown in Stan

551 views
Skip to first unread message

jt...@st-andrews.ac.uk

unread,
Nov 23, 2016, 5:01:37 PM11/23/16
to Stan users mailing list
Rmarkdown in Stan seems to be another headache inside of my life. 
When inputting this as a code chunk. I get an error

```{R, engine="stan",engine.opts=list(x="mod")}
data {
    int N; //the number of observations
    //int N2; //the size of the new_X matrix
    int K; //the number of columns in the model matrix
    real y[N]; //the response
    matrix[N,K] X; //the model matrix
    //matrix[N2,K] new_X; //the matrix for the predicted values
  }
  
parameters {
  vector[K] beta; //the regression parameters
  real sigma; //the standard deviation
}

transformed parameters {
  vector[N] linpred;
  linpred <- X*beta;
}

model {  
  beta[1] ~ uniform(-10,10); //prior for the intercept following Gelman 2008
  
  for(i in 2:K)
    beta[i] ~ uniform(-10,10);//prior for the slopes following Gelman 2008
  
  y ~ normal(linpred,sigma);
}

//generated quantities {
//  vector[N2] y_pred;
//  y_pred <- new_X*beta; //the y values predicted by the model
//}
```
the error is

Error in if (nchar(CXX) > 0) return(TRUE) : argument is of length zero
Calls: <Anonymous> ... engine -> assign -> do.call -> <Anonymous> -> find_rtools
In addition: Warning messages:
1: In engine(options) :
  the option engine.opts$x is deprecated; use the chunk option output.var instead
2: running command '"C:/PROGRA~1/R/R-33~1.1/bin//R" CMD config CXX' had status 2 
Execution halted

When using another method 

ie ```{stan, output.var="ex1"}
data {
    int N; //the number of observations
    //int N2; //the size of the new_X matrix
    int K; //the number of columns in the model matrix
    real y[N]; //the response
    matrix[N,K] X; //the model matrix
    //matrix[N2,K] new_X; //the matrix for the predicted values
  } .... and so on 
  
I get a similar error

Quitting from lines 78-110 (main.Rmd) 
Error in if (nchar(CXX) > 0) return(TRUE) : argument is of length zero
Calls: <Anonymous> ... engine -> assign -> do.call -> <Anonymous> -> find_rtools
In addition: Warning message:
running command '"C:/PROGRA~1/R/R-33~1.1/bin//R" CMD config CXX' had status 2 
Execution halted

What does these error mean? Does it have to do with my Rtools?and How do I fix it?

Ben Goodrich

unread,
Nov 23, 2016, 5:18:49 PM11/23/16
to Stan users mailing list
On Wednesday, November 23, 2016 at 5:01:37 PM UTC-5, jt...@st-andrews.ac.uk wrote:
Rmarkdown in Stan seems to be another headache inside of my life. 

Does this happen outside or Rmarkdown too? As in, if you just execute stan_model() on some .stan file?

Vincent Dorie

unread,
Nov 23, 2016, 5:23:38 PM11/23/16
to stan-...@googlegroups.com
I've been successful using a block of the sort:

```{r engine = 'cat', engine.opts = list(file = "MODEL_NAME.stan", lang = "stan")}
// stan code
```

and then later

```{r}
fit <- stan("MODEL_NAME.stan")
```

This has the benefit of being able to cache the compilation step between renders. Unfortunately, the displayed Stan code has some typesetting issues in HTML, but I think that's an issue with the knitr engine for Stan.

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jt...@st-andrews.ac.uk

unread,
Nov 23, 2016, 6:17:07 PM11/23/16
to Stan users mailing list
No. I can properly load a stan file and run the model without R markdown.

jt...@st-andrews.ac.uk

unread,
Nov 23, 2016, 6:56:53 PM11/23/16
to Stan users mailing list
So, I've tried Vincent's method. And it worked great for defining the model. 

But, when I included the 2nd code chunk I obtained the error message 
Error in if (nchar(CXX) > 0) return(TRUE) : argument is of length zero
Calls: <Anonymous> ... withVisible -> eval -> eval -> stan -> stan_model -> find_rtools
Execution halted


This was my code
```{r engine = 'cat', engine.opts = list(file = "example.stan", lang = "stan")}
data {
    int N; //the number of observations
    //int N2; //the size of the new_X matrix
    int K; //the number of columns in the model matrix
    real y[N]; //the response
    matrix[N,K] X; //the model matrix
    //matrix[N2,K] new_X; //the matrix for the predicted values
  }
  
parameters {
  vector[K] beta; //the regression parameters
  real sigma; //the standard deviation
}

transformed parameters {
  vector[N] linpred;
  linpred <- X*beta;
}

model {  
  beta[1] ~ uniform(-10,10); //prior for the intercept following Gelman 2008
  
  for(i in 2:K)
    beta[i] ~ uniform(-10,10);//prior for the slopes following Gelman 2008
  
  y ~ normal(linpred,sigma);
}

//generated quantities {
//  vector[N2] y_pred;
//  y_pred <- new_X*beta; //the y values predicted by the model
//}
```


```{r}
fit <- stan(file="example.stan")
```
So I have a feeling it has something to do with using a loaded libary(rstan) inside Rmarkdown. Any ideas?  
For your information

Of course I also included this code chunk to load the library 


```{r ,include=FALSE }
#load libraries
library(R2OpenBUGS)
library(MASS) # need to mvrnorm
library(MCMCpack) # need for rwish
library(rstan)
```

To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

Vincent Dorie

unread,
Nov 23, 2016, 7:47:18 PM11/23/16
to stan-...@googlegroups.com
Assuming your installation is up-to-date with the github version, it looks like a Windows bug in rstan:::find_rtools and/or a configuration error. For some reason, the following code is returning either a 0-length vector or NULL:

system2(file.path(Sys.getenv("R_HOME"), "bin",
                  Sys.getenv("R_ARCH_BIN"), "R"),
                  args = "CMD config CXX", stdout = TRUE)

A good way to debug would be to execute (from the console, or ideally directly into the Rmd):

print(execPath <- file.path(Sys.getenv("R_HOME"), "bin", Sys.getenv("R_ARCH_BIN"), "R"))
print(CXX <- system2(execPath, args = "CMD config CXX", stdout = TRUE))

In my Windows R session R_ARCH_BIN isn't defined, but it looks like it maybe should refer to R_ARCH. A workaround would be to define those environment variables:

if (nchar(Sys.getenv("R_HOME")) == 0) Sys.setenv(R_HOME = R.home())
if (nchar(Sys.getenv("R_ARCH_BIN")) == 0) Sys.setenv(R_ARCH_BIN = .Platform$r_arch)

Presumably, an upstream fix is to check for the NULL result and instead find R by using file.path(R.home("bin"), "R").

To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

jt...@st-andrews.ac.uk

unread,
Nov 23, 2016, 8:24:53 PM11/23/16
to Stan users mailing list
Hi I am not the most tech savvy person out there but i tried what you said.

It appears either inside console or or RMD I get the returns.

print(execPath <- file.path(Sys.getenv("R_HOME"), "bin", Sys.getenv("R_ARCH_BIN"), "R"))
[1] "C:/PROGRA~1/R/R-33~1.1/bin/x64/R"

print(CXX <- system2(execPath, args = "CMD config CXX", stdout = TRUE))
Warning message:
running command '"C:/PROGRA~1/R/R-33~1.1/bin/x64/R" CMD config CXX' had status 2 

file.path(R.home("bin"), "R")
"C:/PROGRA~1/R/R-33~1.1/bin/x64/R"

I do not know what you mean by upstream check. Based on what I get above, does it mean that I made a faulty installation of Rstan?

Jeffrey Arnold

unread,
Nov 23, 2016, 8:47:40 PM11/23/16
to Stan users mailing list
I wrote the stan engine and I can't figure out what's going on here (yet).  Here's the code for the stan engine. I just execute stan_model with the code in the chunk as the model_code argument and saves the stan model to a variable with the name specified in output.var. 

I think Vincent is right that the bug is with Windows and somewhere in find_rtools. I didn't have a problem running it in OSX. But I can't figure out why stan_model would work with it in one case and not the other. Does the model work if you pass it as a character vector to stan_model? 

To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

jt...@st-andrews.ac.uk

unread,
Nov 23, 2016, 8:59:33 PM11/23/16
to Stan users mailing list
Hi Jeff, 

```{r pressure, echo=FALSE}
scode <- "
parameters {
  real y[2]; 
} 
model {
  y[1] ~ normal(0, 1);
  y[2] ~ double_exponential(0, 2);
} 
"
fit1 <- stan(model_code = scode, iter = 10, verbose = FALSE) 
print(fit1)
```

I tried the code u had on github. it returns me this error
Error in if (nchar(CXX) > 0) return(TRUE) : argument is of length zero
Calls: <Anonymous> ... withVisible -> eval -> eval -> stan -> stan_model -> find_rtools
Execution halted

It works on a normal R setup(I usually put all my stan code in another file and call it). just doesnt work on rmarkdown

Jeffrey Arnold

unread,
Nov 23, 2016, 9:18:23 PM11/23/16
to Stan users mailing list
What about just running the following inside and outside Rmarkdown? 

```{r}
devtools::find_rtools()
```

This seems to be some sort of bug in the find_rtools function on Windows, and that should confirm it. 

I also generally run the 

What were the highlighting problems with the Stan code? 

To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Vincent Dorie

unread,
Nov 23, 2016, 11:54:26 PM11/23/16
to stan-...@googlegroups.com
The real culprit looks like the call to system2. Normally, the output from system2 should be a character string even if an error is generated. Since the result appears to be empty, it might be worth trying to executing the call from the Windows command line (Windows menu -> type "cmd" -> copy/paste "C:/PROGRA~1/R/R-33~1.1/bin/x64/R" CMD config CXX). From what I can see, exit status 2 means that the child R process crashed horribly, which I guess might make sense for it to have a completely empty message.

By "upstream", I meant that it might make sense to modify Rstan's find_rtools function to catch the error and print an informative error message, although so far this seems a little obscure.

jt...@st-andrews.ac.uk

unread,
Nov 24, 2016, 6:23:01 AM11/24/16
to Stan users mailing list
  • So I tried using the command line and it returns, "no such file or directory" 
  • When I run devtools::find_rtools() I get  both in console and Rmarkdown I get 
Rmarkdown
Error in if (!file.exists(path)) { : argument is of length zero
Calls: <Anonymous> ... eval -> <Anonymous> -> scan_path_for_rtools -> find_gcc49
Execution halted

Console
Error: running command '"C:/PROGRA~1/R/R-33~1.1/bin/x64/R" --no-site-file --no-environ --no-save --no-restore --quiet CMD config CC' had status 2

jt...@st-andrews.ac.uk

unread,
Nov 24, 2016, 12:00:31 PM11/24/16
to Stan users mailing list
Somehow I managed to get the error on R now(not just R markdown) when I  call stan() procedure

1: running command '"C:/PROGRA~1/R/R-33~1.1/bin//R" CMD config CXX' had status 2 
2: In if (nchar(CXX) > 0) return(TRUE) :
  the condition has length > 1 and only the first element will be used


Am I better off uninstalling and re-installing?

Vincent Dorie

unread,
Nov 24, 2016, 12:05:18 PM11/24/16
to stan-...@googlegroups.com
For mysterious reasons, it seems as if R cannot find itself. Reinstalling is probably a good idea, as is confirming that R actually exists in that directory. I'm not a Windows expert, but it sounds like there is some incorrect information in the registry somewhere.

To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Ben Goodrich

unread,
Nov 24, 2016, 12:12:10 PM11/24/16
to Stan users mailing list
On Thursday, November 24, 2016 at 12:00:31 PM UTC-5, jt...@st-andrews.ac.uk wrote:
Somehow I managed to get the error on R now(not just R markdown) when I  call stan() procedure

1: running command '"C:/PROGRA~1/R/R-33~1.1/bin//R" CMD config CXX' had status 2 
2: In if (nchar(CXX) > 0) return(TRUE) :
  the condition has length > 1 and only the first element will be used


Am I better off uninstalling and re-installing?

No. This is a problem with your C++ compiler configuration. Does

fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
    return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
' )
fx
( 2L, 5 ) # should be 10

return 10?

Ben

jt...@st-andrews.ac.uk

unread,
Nov 24, 2016, 2:05:37 PM11/24/16
to Stan users mailing list
Ben, it does return 10.

Vincent Dorie

unread,
Nov 24, 2016, 2:27:56 PM11/24/16
to stan-...@googlegroups.com
R does not seem to exist where R thinks it does, so whether or not the C++ toolchain is installed correctly R cannot be queried to find it. The inline package performs a similar check (using system and R.home("bin") instead of system2 and environment variables) but directly has R compile the code using R CMD SHLIB. This possibly implies that it is only R CMD config that is failing. It is worth trying to execute from the windows command line "C:/PROGRA~1/R/R-33~1.1/bin/x64/R" and seeing if R opens in the console. If I recall, the various CMDs are implemented by links, so it is possible that only the config is missing. 

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Mike Lawrence

unread,
Nov 24, 2016, 2:38:05 PM11/24/16
to stan-...@googlegroups.com
A student of mine experienced this last week after updating her OS. Worked after re-installing R and rstan.

To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

jt...@st-andrews.ac.uk

unread,
Nov 24, 2016, 7:05:59 PM11/24/16
to Stan users mailing list
Thanks for the help everyone. I think I am going to switch to using a MAC. ahhh another pain installing openbugs. 
Reply all
Reply to author
Forward
0 new messages