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

Option to show chunk options in output #1688

Closed
ulyngs opened this issue Mar 24, 2019 · 10 comments
Closed

Option to show chunk options in output #1688

ulyngs opened this issue Mar 24, 2019 · 10 comments
Labels
feature Feature requests next Issues/PRs considered for the next release

Comments

@ulyngs
Copy link

@ulyngs ulyngs commented Mar 24, 2019

It's often important to be able to display a code chunk in the output where the reader is also able to see the chunk options.

At the moment, one has to do this rather awkwardly with something like:

````markdown
`r ''````{r setup, echo=TRUE}
library(tidyverse)
```
````

Which outputs

```{r setup, echo=TRUE}
library(tidyverse)
```

It would be fantastically useful if something like

```{r setup, echo_opts=TRUE}
library(tidyverse)
```

had the same effect.

@yihui
Copy link
Owner

@yihui yihui commented Mar 25, 2019

Yes, I admit it is awkward, and it has been bothering myself (but only mildly). Do you have time to improve https://github.com/yihui/knitr-examples/blob/master/062-chunk-wrapper.Rmd?

@ulyngs
Copy link
Author

@ulyngs ulyngs commented Mar 26, 2019

Not sure this is an improvement on the solution you showed, but here's an alternative approach:

# option 2: chunk option 'echo_opts'
hook_chunk = knit_hooks$get('chunk')
knit_hooks$set(chunk = function(x, options) {
  if (!is.null(options$echo_opts)) {
    return(paste0("```` ```{r ", options$params.src, "} ````", x, "```` ``` ````"))
  } else {
    return(hook_chunk(x, options))  # pass to default hook
  }
})

Output is identical in PDF, but slightly different in HTML:
Screenshot 2019-03-26 at 10 09 49

image

@yihui yihui added this to the v1.23 milestone Mar 26, 2019
@yihui
Copy link
Owner

@yihui yihui commented Mar 26, 2019

Unless you want to actually evaluate the code chunk, I would put everything in a single code block: the chunk header, the code, and footer (all inside four backticks). Anyway, it seems you have understood the method. I'm not particularly interested in providing built-in support for this (it will require more thinking, e.g. code chunks may be indented, and I don't have time for this task).

@yihui yihui closed this as completed May 16, 2019
@yihui yihui reopened this Jun 20, 2019
@cderv cderv added the feature Feature requests label Jan 28, 2021
@cderv cderv removed this from the v1.23 milestone Jan 28, 2021
@cderv
Copy link
Collaborator

@cderv cderv commented Jan 28, 2021

I believe this is related to #1931 and #1275

@yihui yihui added the next Issues/PRs considered for the next release label Aug 27, 2021
@apreshill
Copy link

@apreshill apreshill commented Aug 30, 2021

Hi- wanted to share the Quarto syntax for this:
https://quarto.org/docs/computations/running-code.html#escaping

So instead of:

````r
```{r setup, echo=TRUE}`r ''`
library(tidyverse)
```
````

You would use:

````{.r}
```{{r setup, echo=TRUE}}
library(tidyverse)
```
````

@cderv
Copy link
Collaborator

@cderv cderv commented Aug 31, 2021

Thanks @apreshill ! This is interesting, I wasn't aware

I guess this works too

````r
```{{r setup, echo=TRUE}}
library(tidyverse)
```
````

This is done in Quarto by a Lua filter. So we could bring this syntax to rmarkdown pretty easily. The filter would just need to be added to all type of formats. It works this way:

  • knitr won't parse this part of the Rmd source
  • Pandoc will create a Codeblock element with the content
  • The Lua filter will replace the text content inside this CodeBlock ```{{r setup, echo=TRUE}} by ```{r setup, echo=TRUE} using a regex + gsub. This will create a code block of the language class set in the external backticks to show as-is a knitr code chunk with engine an option.

I guess you find it to be an easier syntax, aren't you ? It is quite nice.
@yihui what do you think of this syntax ?
I can work on a PR for this if we want it in R Markdown.

Side note on the result:
I am always puzzled by what the highlighting should be. So I either don't set any language or set markdown

````{.markdown}
```{{r setup, echo=TRUE}}
library(tidyverse)
```
````

@yihui
Copy link
Owner

@yihui yihui commented Aug 31, 2021

what do you think of this syntax ?
I can work on a PR for this if we want it in R Markdown.

It looks good to me. A PR will be great.

I am always puzzled by what the highlighting should be. So I either don't set any language or set markdown

I don't have a strong preference, but I always set the language to markdown.

@cderv
Copy link
Collaborator

@cderv cderv commented Nov 17, 2021

@ulyngs current dev version of knitr supports a new verbatim engine. See https://github.com/yihui/knitr/blob/master/NEWS.md

You can write

````{verbatim, lang="markdown"}
```{r setup, echo=TRUE}
library(tidyverse)
```
````

to output the content of the verbatim chunk in a fenced code block

````markdown
```{r setup, echo=TRUE}
library(tidyverse)
```
````

I believe this solves you initially need, isn't it ?

@ulyngs
Copy link
Author

@ulyngs ulyngs commented Nov 17, 2021

Hip hooray!!!!! :D

@ulyngs ulyngs closed this as completed Nov 17, 2021
@github-actions
Copy link

@github-actions github-actions bot commented May 18, 2022

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 May 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature requests next Issues/PRs considered for the next release
Projects
None yet
Development

No branches or pull requests

4 participants