-
-
Notifications
You must be signed in to change notification settings - Fork 877
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
Difficulty in using class.output
option with collapse=T
#2172
Comments
The purpose of
Usually when setting knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
) See for example R4DS book. A prompt could be added for R code line but this is not good for highlighting
You could also look at tweaking the CSS for leaving less space between the source block and result block maybe. I don't really know what we could do to change this. Do you have suggestion based on the current structure of HTML produce when |
Ideally the default would be that this:
Would produce something like:
This way you could style the code and output differently. The problem with having a prefix for results or messages (e.g. If you also then set output classes it would be possible to have:
Would produce something like:
|
Just to add, an example here: https://codepen.io/benwhalley/pen/jOxLEoo I realised that spans within pre tags wouldn't allow a background color, so divs might be better. Either would be better than current behaviour thought I think. |
One thing is that knitr does not produce the HTML structure of the code. knitr will produce markdown content, to be converted to HTML or PDF or else, by Pandoc. https://bookdown.org/yihui/rmarkdown-cookbook/rmarkdown-process.html So when setting
will produce this markdown in the intermediate
(by default, It will be converted to HTML by Pandoc to So there is no control over what you expect unfortunately. Regarding Even if we could come up with a new way to change the structure (which downlit R package is doing), we could also need to come up or tweak the syntax highlighter. Overall it is not that obvious, and we won't probably change this in knitr at this is not the correct level. It would need to be in one of the syntax highligther but they usually are static analysis only and don't know results or it would need to be a specific R tools (like downlit) for this specific usage, that would use special knitr hooks to output directly a structure like you want. Problem of syntax highlighting would need to be handled specifically too.
The comment part is here by default so that when copying the code chunk content from the collapse block, this is still valid R code. R expression does get evaluated when paster, while results being commented they are ignored.
Just to shere, there are some tools to add a copy button in chunks like This exists in bookdown too but we still need to make it clever to ignore prompt and results rstudio/bookdown#1072 So we could probably add to rmarkdown as default feature to add a clever copy button that would keep only the code to avoid copy issues. I hope this clarify the topic and challenge about this. |
I understand that the processing is done by pandoc etc, although I guess that doesn't preclude using markdown syntax to add the classes to multiple elements. However I see that what I'm suggesting is actually different from the One way to do this would be to amend the
This would be backwards compatible, but avoid the need to set collapse=T if you want all of the output after all of the code . |
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. |
This reopens this issue: #1906
These screenshots illustrate the problem. The only thing that changes is the value for
collapse
chunk option:With
collapse = F
With
collapse = T
Minimal example is here: https://gist.github.com/benwhalley/99b2ebf4b58605e0cec35727793190f6
I can see there was some previous discussion about expected behaviour, but the problem problem is that if collapse=T then no distinction is made between the R code being echoed and the resulting output (and no different css is applied, so results can't be styled differently to commands).
I can see that for the second code chunk it's not obvious which classes to output, but at the very least allowing the author to know that something is a result rather than the command that produced it would be very useful.
In my case, I could just set collapse=F but this results in very fragmented commands and output which is less good for explanatory purposes.
The text was updated successfully, but these errors were encountered: