Skip to content

srcref attribute is not handled as expected in chunks #2402

@bundfussr

Description

@bundfussr

It seems that code executed in chunks sets the srcref attribute differently than code executed in the console.

Please consider the following .Rmd file.

---
title: "Srcref"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Srcref}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = ""
)
```

The following does not work as expected.
```{r}
options(keep.source = TRUE)
myfun <- function(x) {
  substitute(x)
}
x <- myfun({1 + 2})
x
attributes(x)
attributes(myfun)
```

It should produce
```
options(keep.source = TRUE)
myfun <- function(x) {
 substitute(x)
}
x <- myfun({1 + 2})
x
{
    1 + 2
}
attributes(x)
$srcref
$srcref[[1]]
{

$srcref[[2]]
1 + 2


$srcfile
 

$wholeSrcref
x <- myfun({1 + 2}

attributes(myfun)
$srcref
function(x) {
  substitute(x)
}
```

The result is:

Image

This may also be the reason why some examples in "Advanced R" are not displayed correctly. Consider for example the last chunk in 6.2.1 Function components or the first chunk in 20.2.3 Gotcha: function().

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBugsexternalrelated to other tools or other package

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions