Is there an option I can provide to code chunks in RMarkdown so that it will have a cell number attached to the HTML output. Much like Jupyter has cell numbers.
I've seen some example with line numbering which is not what I want.
Using cell numbers is helpful when I'm discussing an RMarkdown HTML file over the phone with someone. I can ask him/her to see cell 23. I have a lot of R code, so providing section titles, while possible, is tedious.
Here's a solution using only CSS. It relies on CSS counters: each new R chunk increments the counter (named counter-rchunks).
You can knit the following minimal Rmd file and get this result:
---
title: "Counter for chunks"
author: "Romain Lesur"
output: html_document
---
```{css, echo=FALSE}
body {
counter-reset: counter-rchunks;
}
div.main-container {
padding-left: 5em;
}
pre.r {
counter-increment: counter-rchunks;
position: relative;
overflow: visible;
}
pre.r::before {
content: 'In [' counter(counter-rchunks) ']: ';
display: inline-block;
position: absolute;
left: -5em;
color: rgb(48, 63, 159);
}
```
```{r cars}
summary(cars)
```
```{r head-cars}
head(cars)
```
You may have to adapt this solution to your HTML template.
You also can insert these CSS rules to a .css file and includes it in your html_document.
Related
I use xaringan to create a presentation (with a footer).
The footer should include the title, and probably also the author of the presentation automatically.
To get the current slide number, I can use %current%, but %title% or %author$ fails. I know I can access the metadata like so in R rmarkdown::metadata$title, but I fail to construct the slideNumberFormat using R. I.e., r cat('<div class = "remark-slide-number-left">', rmarkdown::metadata$title, '</div>') does not work.
Any ideas?
MWE:
---
title: "Minimal Presenation"
author: "The Author"
output:
xaringan::moon_reader:
nature:
slideNumberFormat: '<div class = "remark-slide-number-left"> %title% - %current%</div>'
---
# Hello World
here %title% does not work, the intended output format would be "Minimal Presentation".
The alternative (which also does not work) would be to construct the footer using R similar to this
slideNumberFormat: "`r cat('<div class = \"remark-slide-number-left\">', rmarkdown::metadata$title, ' - %current% </div>')`"
With the help of some non-SO people, I was able to find this answer.
The only drawback is, that it does not construct the footer in one element, but internally constructs two footers... Nonetheless, it allows the reuse of the author and title and other YAML elements.
The css style of the footer can (and should be) written to a css file similar to what #persephone did.
---
title: "Minimal Presentation"
author: "The Author"
output:
xaringan::moon_reader:
css: ["default"]
nature:
slideNumberFormat: "%current%"
---
layout: true
<div style="position: absolute;left:60px;bottom:11px;color:gray;">`r rmarkdown::metadata$author` - `r rmarkdown::metadata$title`</div>
---
# Hello World
First content slide
---
class: inverse, center, middle
# Section Header
---
# Foo Bar
Second content slide
In general, I am not a huge fan of footers in presentations (they are mostly useless IMO/add unnecessary visual cluttering). However, some co-authors prefer to have them in conference presentations.
I also fiddled around with automatization but didn't find a perfect solution without putting much work into it.
Thus, my approach was to leave the slide numbering as is and just hardcode an extra footer with Authors/etc.
Add to a custom .css/style file/tweak to your needs:
div.footer {
background-color: #5c6266;
position: absolute;
bottom: 10px;
left: 0px;
height: 25px;
width: 20%;
}
div.footer span {
font-size: 13pt; /* */
color: #f6f3f2;
position: absolute;
left: 15px;
bottom: 1px;
}
Source it in your YAML:
css: ["default", custom.css"]
Add/Tweak this just before the 1st slide you want to include your footer in (e.g. after your title slide):
---
layout:true
<div class="footer"><span>John Doe et al.</span></div>
---
This is hacky but does not break the slide numbering and was the fastest solution I came up with in my context.
Is there a way to change the font and color in the YAML title in a R markdown flexdashboard?
Here is the code for the YAML header I am trying to change:
---
title: "Greenhouse gases and weather data"
fontsize: 30
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
social: menu
source_code: embed
theme: readable
---
The other option would be to add a CSS code chunk anywhere in the dashboard
```{css}
body > div.navbar.navbar-inverse.navbar-fixed-top > div > div.navbar-header > span.navbar-brand {
font-size: 26px;
color: red;
}
```
as Kat said, the color is set by CSS , therefore you can change the behaviour in the .rmd file itself, or in the underlying theme template .css file.
somewhere located at:
/home/user/R/x86_64-pc-linux-gnu-library/4.0/flexdashboard/rmarkdown/templates/flex_dashboard/resources
add (to the rmd) or look for and change (the .css) to :
<style>
.navbar {
background-color:white;
border-color:blue;
}
.navbar-brand {
color:blue!important;
}
</style>
This will revert the default color scheme of the top navbar
at the moment i dont know a simple YAML - argument solution for this
( but looking into css will gain you much more
versatility along the way, than relying on the YAML options)
I have a big problem since a long time and I could not find any solution. Daily, I waste a lot of time because of it.
I use knitr_1.29 to generate HTML reports (R version 4.0.0, R Studio 1.3.959). Sometimes the output of one or several chunks is not printed whitout any reason.
I mainly meet this problem with the "pheatmap" and "kable" functions.
An example: I have very similar chunks (same options, same kind of output) in my Rmd file and... some outputs are printed, some others not !
Or I will execute knitr on the same .RMD file twice: the first time, the output will be printed, the second one, it will not. This happens even if I changed nothing in my .Rmd file.
I have this problem when I use "render" or when I "knitr" directly the document...
Thank you very much for your help... I'm desesperate...
EDIT : a sort of example... But actually, with this kind of simple file, I will not meet any problem. The problem seems to occur with long and complicated files...
---
title: "Title"
subtitle: 'Subtitle'
author: "me"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
toc: true
toc_depth: 4
toc_float: true
code_folding: hide
theme: flatly
highlight: tango
---
<style type="text/css">
body{ /* Normal */
font-size: 16px;
}
td { /* Table */
font-size: 14px;
}
h1.title {
font-size: 60px;
color: Tomato;
}
h1 { /* Header 1 */
font-size: 28px;
color: SteelBlue;
}
h2 { /* Header 2 */
font-size: 22px;
color: SteelBlue;
}
h3 { /* Header 3 */
font-size: 18px;
color: SteelBlue;
}
code.r{ /* Code block */
font-size: 12px;
}
pre { /* Code block - determines code spacing between lines */
font-size: 14px;
}
</style>
```{r global_options,message=FALSE,results="hide"}
knitr::opts_chunk$set(fig.align="center",eval.after="fig.cap",
fig.width=12,fig.height=5,
warning=FALSE,
results="asis",
message=FALSE)
```
```{r setup, results="hide",fig.keep="none"}
library(DESeq2)
library(edgeR)
library(ade4)
library(factoextra)
library(kableExtra)
library(RColorBrewer)
library(vsn)
library(ggVennDiagram)
library(wesanderson)
library(DescTools)
library(pheatmap)
library(WGCNA)
library(readxl)
library(gage)
library(sva)
library(gageData)
library(AnnotationDbi)
library(org.Hs.eg.db)
library(SummarizedExperiment)
library(ggrepel)
library(openxlsx)
library(ggrastr)
library(viridis)
```
This chunk will print nothing
```{r,fig.height=7}
matrix1 = matrix(rnorm(200), 20, 10)
pheatmap(matrix1, color=inferno(100))
```
This chunk will print something
```{r heatmap C2 in C1 TEST,fig.height=10}
matrix2 = matrix(rnorm(200), 20, 10)
pheatmap(matrix2, color=inferno(100))
```
This chunk will print nothing
```{r model 1}
t<-sample(1:2,1)
if(t==1) {
df<-data.frame(replicate(3,sample(0:1,5,rep=TRUE)))
kable(df) %>%kable_styling("striped")
}
```
This chunk will print a table.
```{r model 2}
if(t==1) {
df<-data.frame(replicate(3,sample(0:1,5,rep=TRUE)))
kable(df) %>%kable_styling("striped")
}
```
While we can automate number section by adding the option number_sections: true to in the YAML header, I wonder if we can change the headings style in r markdown. For example, can we customise it with letters like following?
A. Section A
A.1 Subsection
A.1.1 Sub subsection 1
A.1.2 Sub subsection 1
B. Section B
B.1 Subsection
B.1.1 Sub subsection
Answer for pdf_document
Save this line in an external file (e.g. inheader.tex):
\renewcommand{\thesection}{\Alph{section}}
and insert the file in document's header with:
---
title: "Lettered sections"
output:
pdf_document:
number_sections: true
includes:
in_header: inheader.tex
---
Answer for html_document format
tl;dr
Knit this Rmd file:
---
title: "Lettered sections"
output: html_document
---
```{css, echo=FALSE}
.header-section-number {
display: none;
}
body {
counter-reset: counter-level-1;
}
h1:not(.title) {
counter-increment: counter-level-1;
counter-reset: counter-level-2;
}
h1:not(.title)::before{
content: counter(counter-level-1, upper-alpha) ". ";
}
h2 {
counter-increment: counter-level-2;
counter-reset: counter-level-3;
}
h2::before {
content: counter(counter-level-1, upper-alpha) "." counter(counter-level-2) " ";
}
h3 {
counter-increment: counter-level-3;
}
h3::before {
content: counter(counter-level-1, upper-alpha) "." counter(counter-level-2) "." counter(counter-level-3) " ";
}
```
# Section
## Subsection
### Sub subsection
### Sub subsection
## Subsection
# Section
## Subsection
### Sub subsection
## Subsection
Explanations
Number sections is a native pandoc's option. It seems that pandoc does not provide any support for hierarchical headings customisation.
So, there are three options with HTML output:
deeply dive into pandoc and develop a new writer, because hierarchical headings are declared as integers see here, line #105. Note there's a relevant recent issue in order to facilitate headings customisation.
modify HTML rendering using CSS.
modify HTML elements with Javascript. This may be necessary for toc: true.
This answer provides an example of hierarchical headings customisation with CSS. It is recommended to save all the CSS code (i.e. lines 7 to 39) to an external file with .css extension and include it in HTML report using this YAML header:
---
title: "Lettered sections"
output:
html_document:
css: "filename.css"
---
Additional note
One can use other counters than numeric or alpha, see here for a list.
You also can define your own set of counter with #counter-style.
I have an RMarkdown document outputting to HTML of the same form as the below example. What do I add where to apply unique CSS ids or classes to each plot output?
---
title: "RMarkdown"
author: "Me"
date: "Friday, March 27, 2015"
output:
html_document:
theme: null
css: style.css
---
```{r plot1, echo=FALSE, warning=FALSE, message=FALSE}
library(ggplot2)
x <- ggplot(some_r_code)
print(x)
```
```{r plot2, echo=FALSE, warning=FALSE, message=FALSE}
y <- ggplot(some_more_r_code)
print(y)
```
I've read the info page at http://rmarkdown.rstudio.com/html_document_format.html that went a ways to answering this question but didn't get me there. I have a similar question referencing the material in that page in it's comment section, and would appreciate an answer on either.
Thanks!
You can tell knitr (which is used under the hood) with results="asis" to embed a chunk's output directly into the html. Within the chunk you can use cat to simply write a style tag including your css definitions:
```{r results="asis"}
cat("
<style>
h1 {
color: red;
}
</style>
")
```
See http://yihui.name/knitr/options/#chunk_options for details.
Declaring custom css in RMarkdown
Add css between <style> and </style> tags in the regular body of the RMarkdown (i.e. not in R code area), like so:
<style>
.pad {
padding-top: 200px;
}
</style>
# This heading will be padded {.pad}
Another option is to declare css: "style.css" in yaml and store styles in a separate file (style sheet) in the same directory
Or css can be generated and applied via R code (excellent example here)
Open the resultant HTML in a browser with a Developer Tools option and look at the generated HTML. Then apply you styling to the appropriate tags/classes. For example, put the following into style.css, knit the file and you should see a red border on the plots:
img {
background-color: red;
padding: 2px;
border: 1px solid red;
border-radius: 3px;
margin: 0 5px;
max-width: 100%;
}