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

Lint fails with Rmd chunks using {lang} engine format #322

Closed
cnolanminich opened this issue May 31, 2018 · 1 comment
Closed

Lint fails with Rmd chunks using {lang} engine format #322

cnolanminich opened this issue May 31, 2018 · 1 comment

Comments

@cnolanminich
Copy link

This issue is related to #32, where lintr should ignore non-R chunks as it lints. lintr currently only ignores chunks that have the following chunk header: {r engine = "LANG"}, vs. the newer and now more common {LANG} Refer here, and the discussion on knitr issue #963 here.

Currently, if you use the {LANG} language engine syntax, lintr will do 2 things:

  1. attempt to lint the chunk and throw an error for the user
  2. not lint any R code after that chunk.

Below is a minimal reprex, using the .Rmd test case in lintr as a base and adding a chunk with {python}.

text <- c('# Test #

          Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
          tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
          vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
          no sea takimata sanctus est Lorem ipsum dolor sit amet.
          
          ```{r}
          a = 1
```

          Test
          ====
          
          Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
          tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
          vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
          no sea takimata sanctus est Lorem ipsum dolor sit amet.
          
          ```{r}
          b <- function(x) {
          d = 1
          }
          
          ```
          
          ```{r engine="python"}
          a=[]
          
          a[0]=1
          ```
          
          ```{python}
          a=[]
          
          a[0]=1
          ```


​          
          ## Same code chunk as above, but lintr will not ouput this
          ```{r}
          
          b <- function(x) {
          d = 1
          }
          
          ```
          ')
  tmp <- tempfile()

  writeLines(text, tmp)

  lintr::lint(tmp)

The results of the lint are below:

image

The part of the code that would require an edit is here.

I think the fix to this would involve adding another && condition that checked that the chunk began with r and does NOT have engine following it. Or perhaps more simply, found any chunk that began with anything other than r and ignored it. Happy to try a fix on this and submit a PR if that would be useful.

@russHyde
Copy link
Collaborator

+1

russHyde added a commit to russHyde/lintr that referenced this issue Sep 18, 2018
Rmarkdown format allows the use of code blocks in the following format

```{some_engine}
# not necessarily R syntax
foo = bar
```

These blocks should be filtered out before linting the remaining R-code blocks.

A couple of python blocks were added to test.Rmd: one with R-like syntax and one with syntax that can't be parsed as R.

The function `defines_knitr_engine` was extracted from `extract_r_source` and extended. This tests for the presence of {r engine = some_engine} and {some_engine} in the start line of a code chunk. The names of valid engines are obtained from `knitr::knit_engines`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants