1

I'm currently working on a Windows computer, and having some issues interaction with knitr and Rcpp.

My code works fine when I run it on Rstudio, but when I try to use knitr to create the html file, I get the error:enter image description here

Creating the html file works when I remove that line, so it seems it's not a problem in neither Rcpp or knitr packages.

On another note, I never used the sourceCPP function in the code, but cppFunction(variable with C code as string).

As required, I created a simplified example, script "cppcode.R" below:

library(Rcpp)
library(RcppArmadillo)

ccode = "
NumericMatrix rand_mat(NumericVector dim){
  IntegerVector v = seq_len(2) - 1;
  int N = dim[0];
  int M = dim[1];
  NumericMatrix Y(N,M);
  for(int i=0;i<N;i++){
    for(int j=0;j<M;j++){
      Y(i,j) = sample(v,1,true)[0];
    }
  }
  return Y;
}
"

cppFunction(code=ccode,depends="RcppArmadillo")

Then the Rmd file:

#This is an example
```{r}
source("cppcode.R")
rand_mat(c(3,3))
```

Works if just run the lines on Rstudio, but gives me the same error when I press the knit button.

VFreguglia
  • 2,129
  • 4
  • 14
  • 35

1 Answers1

1

Well, after looking here, I've found out it might be something with Rtools. It seems like the R session run in knitr is less flexible to "finding Rtools", so it wouldn't work. I'm not sure if this makes sense at all.

In case someone else has this problem, one simple workaround I found is to add the lines below to the Rmd file before calling cppFunction (make sure the location RBuildTools is on PATH). It'll find Rtools before trying to compile.

library(devtools)
find_rtools()

If someone else has a more technical explanation of what's going on and how to fix it so I don't have to add these lines to every file, I'd appreciate.

VFreguglia
  • 2,129
  • 4
  • 14
  • 35
  • 1
    Did you read the post you linked? I provided the technical explanation you desire within it. (See the first and second paragraphs along with the necessary permanent fix.) In short, the _Rtools_ installation was not done properly. Please follow the instructions at: http://thecoatlessprofessor.com/programming/rcpp/install-rtools-for-rcpp/ – coatless Jul 02 '17 at 22:06
  • Sorry, I didn't read it carefully at first, but I think it clarifies what was the issue. Just adding the variables to Path didn't fix it at first, but I've deleted the RBuildTools folder (I assume this is uninstalling Rtools), deleted the Path entries for Rtools and installed again following your guide and it's fixed now. Thank you. – VFreguglia Jul 02 '17 at 23:17