1

I have this code:

eng_cppheader = function(options)
{
  opts = options$engine.opts
  code = paste(options$code, collapse = "\n")
  if (is.null(opts$path)) opts$path = tempdir()
  cat(code, file = file.path(opts$path, opts$filename))
  Sys.setenv(PKG_CPPFLAGS = paste0("-I", opts$path))
  options$engine = 'cpp'
  engine_output(options, code, "")
}
knitr::knit_engines$set(cppheader=eng_cppheader)

If I call it, even with eval=FALSE, I get:

/bin/sh: cppheader: command not found

I've tried

{r, engine="cppheader", ...} and {cppheader, ...}

It seems that registering a new engine automatically calls eng_interpreted. Is there a way to set it and not have a system call?

UPDATED:

I got it to knit, but it crashed RStudio when run as a notebook. Also, there is no "engine.opts" field in the "options" list. I had to change that.

Here is the Rmd. It knits fine.

---
title: "C++ Header Engine Demo"
output:
  html_document:
    df_print: paged
  html_notebook: default
  pdf_document: default
---

```{r}
eng_cppheader = function(options)
{
  code = paste(options$code, collapse = "\n")
  if (is.null(options$file) || nzchar(options$file) == 0)
    stop("file is a required chunk options")
  if (is.null(options$path)) options$path = tempdir()
  cat(code, file = file.path(options$path, options$file))
  Sys.setenv(PKG_CPPFLAGS = paste0("-I\"", options$path, "\""))
  options$engine = 'cpp'
  knitr::engine_output(options, code, "")
}
knitr::knit_engines$set(cppheader=eng_cppheader)
```

```{r}
names(knitr::knit_engines$get())
```

```{r}
knitr::knit_engines$get()$cppheader(
  list(code="Hello world!",
       echo=T, prompt=T, highlight=T,
       file = "test.h"))
```

```{r}
system2('cat', file.path(tempdir(), "test.h"), stdout = TRUE)
```

```{r}
unlink(file.path(tempdir(), "test.h"))
```

```{cppheader, file="test.h"}
#ifndef __test_h__
#define __test_h__

#define test_string "Hello world!"

#include <string>

using std::string;

#endif // __test_h__
```

```{r}
header_file = file.path(tempdir(), "test.h")
ifelse(file.exists(header_file), "I found it!", "I did not find it!")
```
```{r}
Sys.getenv("PKG_CPPFLAGS")
```

```{Rcpp}
#include "test.h"

// [[Rcpp::export]]
string test_it()
{
  return test_string;
}
```

```{r}
print(try(test_it()))
```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
THK
  • 486
  • 3
  • 11
  • Is this issue reproducible outside of RStudio? i.e. is it specific to RStudio? – Yihui Xie Oct 24 '17 at 02:17
  • It does appear to be an RStudio issue. – THK Oct 24 '17 at 16:39
  • I have seen it reported by different users multiple times, and I was also reasonably sure it was an RStudio issue. You may file it to https://github.com/rstudio/rstudio/issues if it has not been filed by other users. Thanks! – Yihui Xie Oct 24 '17 at 18:19

0 Answers0