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

Not working with markdown code chunk with language name #472

Closed
renkun-ken opened this issue Mar 29, 2020 · 4 comments
Closed

Not working with markdown code chunk with language name #472

renkun-ken opened this issue Mar 29, 2020 · 4 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@renkun-ken
Copy link
Collaborator

renkun-ken commented Mar 29, 2020

lintr does not seem to work with Rmd file with markdown code chunk with language name specified. Following are examples that show the problem.

  • doc1.Rmd
# hello

Hello, world!

```{r}
x<-rnorm(100)
y<-rnorm(100)
m <- lm(y~x)
```
> lintr::lint("doc1.Rmd")                                                       
/Users/ken/Workspaces/test/vscode-r/doc1.Rmd:6:2: style: Put spaces around all infix operators.
x<-rnorm(100)
~^~~
/Users/ken/Workspaces/test/vscode-r/doc1.Rmd:7:2: style: Put spaces around all infix operators.
y<-rnorm(100)
~^~~
  • doc2.Rmd
# hello

Hello, world!

```{r}
x<-rnorm(100)
y<-rnorm(100)
m <- lm(y~x)
```

```
x <- rnorm(100)
y <- rnorm(100)
m <- lm(y ~ x)
```
> lintr::lint("doc2.Rmd")                                                       
/Users/ken/Workspaces/test/vscode-r/doc2.Rmd:6:2: style: Put spaces around all infix operators.
x<-rnorm(100)
~^~~
/Users/ken/Workspaces/test/vscode-r/doc2.Rmd:7:2: style: Put spaces around all infix operators.
y<-rnorm(100)
~^~~
  • doc3.Rmd
# hello

Hello, world!

```{r}
x<-rnorm(100)
y<-rnorm(100)
m <- lm(y~x)
```

```r
x <- rnorm(100)
y <- rnorm(100)
m <- lm(y ~ x)
```
> lintr::lint("doc3.Rmd")                                                       
Error: Malformed file!
Backtrace:
1: stop("Malformed file!", call. = FALSE)
2: filter_chunk_end_positions(starts = starts, ends = grep(pattern$chunk.end, 
    lines, perl = TRUE))
3: extract_r_source(source_file$filename, source_file$lines)
4: get_source_expressions(filename)
5: lintr::lint("doc1.Rmd")
@russHyde
Copy link
Collaborator

I modified some code related to this a year or so ago. It might be worth having a look in https://github.com/jimhester/lintr/blob/master/R/extract.R - specifically at the regular expression in defines_knitr_engine - to see if this can be generalised to handle both ```{r block-name} and ```r block-name forms

@russHyde russHyde added the bug an unexpected problem or unintended behavior label Mar 30, 2020
@russHyde
Copy link
Collaborator

russHyde commented May 4, 2020

Thanks for reporting this.

I just tried fixing this and ended up on a wild-goose chase. It did seem like a strange bug, because lintr identifies R code blocks using knitr's regexes for identifying R code blocks.

If we make a slight change to your doc3.Rmd:

---
title: "temp"
output: html_document
---

# hello

Hello, world!

```{r}
x<-rnorm(100)
y<-rnorm(100)
m <- lm(y~x)
m
```

```r
x <- rnorm(100)
y <- rnorm(100)
m <- lm(y ~ x)
m
```

... and then knit that, the second code chunk does not get passed to the R engine by knitr: Although the contents of the model object m are printed out at the end of the first chunk, they are not printed out at the end of the second chunk. knitr treats the second chunk as "code-formatted" text, not as R syntax.

In short, lintr is telling you that the file is malformed, because, according to knitr's rules for identifying R-code blocks, it is malformed: the ``` r line should have braces in it.

Closing

@russHyde russHyde closed this as completed May 4, 2020
@renkun-ken
Copy link
Collaborator Author

Thanks for investigating in this.

Then it would mean that the Rmd cannot be processed if it includes any text code chunk with specified language that is not intended for running but code demonstration?

For example, the following Rmd should be perfectly valid for knitr::knit but it cannot be processed by lintr:

---
title: "temp"
output: html_document
---

# hello

Hello, world!

```{r}
x<-rnorm(100)
y<-rnorm(100)
m <- lm(y~x)
m
```

```cpp
void test(int x) {
  return x + 1;
}
```

@russHyde
Copy link
Collaborator

russHyde commented May 4, 2020

Sorry, I mustn't have understood the original issue. Does the ```cpp line tell knitr to use a formatting engine?

Looked into this a bit further, so ```some_language is the chunk-start for markdown-formatting of a non-evaluated code chunk (https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code-and-syntax-highlighting). Thanks.

@russHyde russHyde reopened this May 4, 2020
russHyde added a commit to russHyde/lintr that referenced this issue May 5, 2020
…uage in Rmd

lintr was complaining that files were malformed when containing
unevaluated code-blocks of the following form (cpp is a format specifier
for use by markdown).
~~~~
```cpp
blah
```
~~~~

"```" matches the end-block pattern for Rmd-code blocks; the change
allows the start pattern for unevaluated blocks to be "```" or
"```some_language"

A couple of such blocks were added to the .Rmd used in the knitr-format
test.

`extract_r_source` has been refactored: selection of start / end
positions for each code block is handled by `get_chunk_positions`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants