Skip to content

srcref attribute is not handled as expected in chunks #2402

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

Open
bundfussr opened this issue May 13, 2025 · 0 comments
Open

srcref attribute is not handled as expected in chunks #2402

bundfussr opened this issue May 13, 2025 · 0 comments

Comments

@bundfussr
Copy link

bundfussr commented May 13, 2025

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().

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

1 participant