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

Ignore {knitr} chunks that use Rcpp engine? #928

Open
IndrajeetPatil opened this issue Mar 8, 2022 · 12 comments
Open

Ignore {knitr} chunks that use Rcpp engine? #928

IndrajeetPatil opened this issue Mar 8, 2022 · 12 comments

Comments

@IndrajeetPatil
Copy link
Collaborator

IndrajeetPatil commented Mar 8, 2022

In a way, a follow-up to #312.

Should {styler} ignore {knitr} R code chunk with Rcpp engine?

For example, if I try to style an .Rmd file containing the following chunk,

```{r, engine='Rcpp'}
#include <Rcpp.h>
using namespace Rcpp;
```

it will (understandably) produce the following error:

Error in `parse_safely()`:
! <text>:2:7: unexpected symbol
1: #include <Rcpp.h>
2: using namespace
         ^

But this file might also contain a number of other R code chunks with engine = 'R', and none of them will be styled due to the offending Rcpp engine chunk.

@lorenzwalthert
Copy link
Collaborator

Thanks. The quick fix I beliebe is just not to use r at the beginning, as the first unnamed argument of a code chunk header seems to get matched with the engine. In particular for Rcpp:

```{Rcpp}
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
  return x * 2;
}

However, to solve this more generically, we should probably rely on {knitr} to parse the code chunk header. This would also allow for #839, since we'd have keys and values we can easily format. Since we already import {knitr} in Suggests and everyone working with R Markdown documents already has it, I would be happy to depend on it for this functionality. Not sure {knitr} exports the functionality for parsing the headers, probably not. But maybe we can convince them of that use case. First step would anyways be to find the relevant code in {knitr} that parses the headers. Are you interested in contributing this?

@lorenzwalthert
Copy link
Collaborator

Also note other issues that have been filed with similar requests #832, #312.

@lorenzwalthert lorenzwalthert changed the title Ignore {knitr} chunks that use Rcpp engine? Ignore {knitr} chunks that use Rcpp engine? Mar 10, 2022
@IndrajeetPatil

This comment was marked as off-topic.

@IndrajeetPatil

This comment was marked as off-topic.

@IndrajeetPatil
Copy link
Collaborator Author

The parameters from chunk header are parsed using an internal function called parse_params():

knitr:::parse_params("a=1, engine='Rcpp'")
#> $a
#> [1] 1
#> 
#> $engine
#> [1] "Rcpp"
#> 
#> $label
#> [1] "unnamed-chunk-3"

knitr:::parse_params("Rcpp, foo, a=1,")
#> $label
#> [1] "Rcpp, foo"
#> 
#> $a
#> [1] 1

Created on 2022-03-10 by the reprex package (v2.0.1.9000)

@lorenzwalthert

This comment was marked as off-topic.

@IndrajeetPatil

This comment was marked as off-topic.

@lorenzwalthert

This comment was marked as off-topic.

@lorenzwalthert
Copy link
Collaborator

Here's all we need to know going forward: yihui/knitr#2112

@MichaelChirico
Copy link
Contributor

MichaelChirico commented Oct 11, 2022 via email

@IndrajeetPatil
Copy link
Collaborator Author

I also get the e-mails btw

@MichaelChirico Do you still get emails when you comment?

This should have been resolved in #1008.

@MichaelChirico
Copy link
Contributor

testing...

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

3 participants