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

When file= chunk option, duplication of chunk label is not checked. #2126

Closed
cderv opened this issue May 12, 2022 · 5 comments · Fixed by #2127
Closed

When file= chunk option, duplication of chunk label is not checked. #2126

cderv opened this issue May 12, 2022 · 5 comments · Fixed by #2127
Assignees

Comments

@cderv
Copy link
Collaborator

cderv commented May 12, 2022

Reported by @mine-cetinkaya-rundel

Reprex:

  • Will error
```{r abcd}
1 + 1
```


```{r abcd}
2 + 2
```
> knitr::knit("test.Rmd")


processing file: test.Rmd
Error in parse_block(g[-1], g[1], params.src, markdown_mode) : 
  Duplicate chunk label 'abcd', which has been used for the chunk:
1 + 1
  • Will not error - no check duplication ?
```{cat, engine.opts = list(file = "_common.R")}
1 + 1
```

```{r abcd, code = xfun::read_utf8("_common.R")}
```


```{r abcd}
2 + 2
```

```{r}
unlink("_common.R")
```
> knitr::knit("test.Rmd")


processing file: test.Rmd
  |...........                                                                                      |  11%
  ordinary text without R code

  |......................                                                                           |  22%
label: unnamed-chunk-1 (with options) 
List of 2
 $ engine.opts:List of 1
  ..$ file: chr "_common.R"
 $ engine     : chr "cat"

  |................................                                                                 |  33%
  ordinary text without R code

  |...........................................                                                      |  44%
label: abcd (with options) 
List of 1
 $ code: chr "1 + 1"

  |......................................................                                           |  56%
  ordinary text without R code

  |.................................................................                                |  67%
label: abcd
  |...........................................................................                      |  78%
  ordinary text without R code

  |......................................................................................           |  89%
label: unnamed-chunk-2
  |.................................................................................................| 100%
  ordinary text without R code


output file: test.md

[1] "test.md"

Same with

```{r abcd, file = "_common.R"}
```

We probably want the duplication chunk to still be done, right ?

@cderv
Copy link
Collaborator Author

cderv commented May 12, 2022

Currently the check is done in parse_block()

knitr/R/parser.R

Lines 108 to 115 in 925b9b3

if (length(code)) {
if (label %in% names(knit_code$get())) {
if (identical(getOption('knitr.duplicate.label'), 'allow')) {
params$label = label = unnamed_chunk(label)
} else stop(
"Duplicate chunk label '", label, "', which has been used for the chunk:\n",
one_string(knit_code$get(label))
)

Maybe we should also trigger if options$code or options$file is provided ?

Or we could regroup in the same all the process and checks regarding code chunk content at the parsing time, or the process time

knitr/R/block.R

Lines 33 to 40 in 925b9b3

# if chunk option 'file' is provided, read the file(s) as the chunk body;
# otherwise if 'code' is provided, use it; if neither 'file' nor 'code' is
# provided, use the chunk body
params[["code"]] = if (is.null(code_file <- params[['file']])) {
params[["code"]] %n% unlist(knit_code$get(ref.label), use.names = FALSE)
} else {
in_input_dir(xfun::read_all(code_file))
}

@yihui
Copy link
Owner

yihui commented May 12, 2022

Maybe we should also trigger if options$code or options$file is provided ?

Yes, let's do that. Sounds like a simple fix (change length(code) to length(code) || length(params$file)). Please feel free to submit a PR and merge by yourself, or just push to master directly.

Or we could regroup in the same all the process and checks regarding code chunk content at the parsing time, or the process time

I'm not sure if that's a good idea.

@cderv
Copy link
Collaborator Author

cderv commented May 12, 2022

Yes, let's do that. Sounds like a simple fix (change length(code) to length(code) || length(params$file)). Please feel free to submit a PR and merge by yourself, or just push to master directly.

it would be length(code) || length(params$file) || length(params$code) instead, right ? I'll test it

@yihui
Copy link
Owner

yihui commented May 12, 2022

Yes.

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants