r-lib / styler

Non-invasive pretty printing of R code
https://styler.r-lib.org
Other
713 stars 70 forks source link

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

Open IndrajeetPatil opened 2 years ago

IndrajeetPatil commented 2 years ago

In a way, a follow-up to https://github.com/r-lib/styler/issues/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:

```r
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 commented 2 years ago

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 https://github.com/r-lib/styler/issues/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 commented 2 years ago

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

IndrajeetPatil commented 2 years ago

First step would anyways be to find the relevant code in {knitr} that parses the headers. Are you interested in contributing this?

Sure, I can try to find the relevant code.

IndrajeetPatil commented 2 years ago

Btw, every time I comment on styler issue, I get this GitHub notification and I have no idea what it's about:

image

IndrajeetPatil commented 2 years ago

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 commented 2 years ago

Thanks, I'll file an issue in {knitr} and ask about exporting this function. Really strange these emails you get...

IndrajeetPatil commented 2 years ago

Really strange these emails you get...

This traces to following GHA, which is triggered on issue comment:

https://github.com/r-lib/styler/blob/b70aebe2890956eb599d8c9cb47414a97250f68b/.github/workflows/pr-commands.yaml#L1-L3

lorenzwalthert commented 2 years ago

strange. Because I never get any e-mail...

lorenzwalthert commented 2 years ago

Here's all we need to know going forward: https://github.com/yihui/knitr/issues/2112

MichaelChirico commented 1 year ago

I also get the e-mails btw

On Thu, Mar 10, 2022 at 3:18 PM Lorenz Walthert @.***> wrote:

Thanks, I'll file an issue in {knitr} and ask about exporting this function. Really strange these emails you get...

— Reply to this email directly, view it on GitHub https://github.com/r-lib/styler/issues/928#issuecomment-1064612666, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB2BA5PRWRYZW4VVD3WLST3U7J7KVANCNFSM5QHJ5M2Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

IndrajeetPatil commented 1 year ago

I also get the e-mails btw

@MichaelChirico Do you still get emails when you comment?

This should have been resolved in https://github.com/r-lib/styler/issues/1008.

MichaelChirico commented 1 year ago

testing...