Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notebooks do not handle custom knitr engines correctly #1662

Closed
thk686 opened this issue Oct 25, 2017 · 5 comments
Closed

Notebooks do not handle custom knitr engines correctly #1662

thk686 opened this issue Oct 25, 2017 · 5 comments
Assignees

Comments

@thk686
Copy link

thk686 commented Oct 25, 2017

I believe this is an issue with R Studio. The following code knits fine, but generates an error when run in R Studio (ie run-all in the notebook interface). It appears R Studio tries to pipe the code through a binary named after the custom engine. That makes no sense if the engine is not meant for calling an external program. Try the code below with knitr and in R Studio.

I am doing some teaching with Rcpp and it is really a problem that I cannot split code across separate chunks. Hope this will get some attention. Thanks.

---
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()))
```

@thk686
Copy link
Author

thk686 commented Oct 25, 2017

cpp_header_engine.txt

@thk686
Copy link
Author

thk686 commented Oct 25, 2017

This example also exposes a segfault in RStudio proper. Rcpp compile failures in notebooks can trigger a crash.

@dfalty
Copy link

dfalty commented Oct 25, 2017

I'm able to reproduce this on Mac with version 1.2.134.

@kevinushey kevinushey self-assigned this Oct 25, 2017
@kevinushey
Copy link
Contributor

I'm now tracking the segfault as a separate issue here: #1668

@jmcphers
Copy link
Member

jmcphers commented Nov 7, 2019

Fixed with #2401.

@jmcphers jmcphers closed this as completed Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants