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 made a presentation with a scrollable code chunk output using the xaringan package in R before like the photo shown below.
I want to make the same scrollable code chunk output in quarto revealjs presentation. Anyone knows how to do it in quarto presentation?
scrollable code chunk output:
If it helps, here is the css code I used before when making a presentation in xaringan.
Thank you in advance!
/* scrollable code chunk output */
.remark-code {
display: block;
overflow-x: auto;
max-height: 100%;
padding: .5em;
color: #fff;
background: rgb(131, 139, 139);
}
You just need two steps to do the same in Quarto revealjs. At first, define a css class with overflow-x: auto and then pass the class to the chunk option class-output so that output of that will have horizontal scrolling.
---
title: Output Horizontal scrolling
format: revealjs
engine: knitr
---
## Quarto
```{r}
#| class-output: hscroll
library(gapminder)
df <- dplyr::bind_cols(gapminder, gapminder, .name_repair = "minimal")
head(df)
```
```{css, echo=FALSE}
.hscroll {
overflow-x: auto;
white-space: nowrap;
}
```
And if you want to do this for code chunks, instead of passing the .hscroll class as a chunk option to a specific chunk, use the knitr opts_chunk key in the yaml section.
---
title: Output Horizontal scrolling
format: revealjs
engine: knitr
knitr:
opts_chunk:
class-output: hscroll
---
## Quarto
```{r}
library(gapminder)
df <- dplyr::bind_cols(gapminder, gapminder, .name_repair = "minimal")
head(df)
```
```{css, echo=FALSE}
.hscroll {
overflow-x: auto;
white-space: nowrap;
}
```
I made a presentation in which I can change the text size of the slides. But if there is a math equation in the text, it doesn't render anymore. Is there a way that I could create a text class that would change the size of the text and be able to render math as well? Also, is there a way I can use the .tiny class on text, math, code input and output, in 1 CSS class definition or I would need to create 4 different .tiny classes (see the .tiny-code code.r and the .tiny-code code[class="remark-code"])?
---
title: Check this out
output:
xaringan::moon_reader:
seal: false
---
```{css, echo=FALSE}
.tiny {
font-size: .6rem;
font-weight: 200;
}
.huge {
font-size: 2.6rem;
font-weight: 200;
}
/* Tiny R block of code */
.tiny-code code.r {
font-weight: bold;
font-size: .3rem;
}
/* Tiny ouput */
.tiny-code code[class="remark-code"] {
font-size: .3rem;
display: block;
border: 0px solid red;
}
```
# Check this slide out
.tiny[get the **best estimation** of the parameters ( $\beta$ s), become a heRo]
.huge[this is huge]
.tiny-code[
```{r}
mtcars[1:5, "mpg"]
```
]
I found that this:
<span class="tiny">get the **best estimation** of the parameters ( $\beta$ s), become a heRo.</span>
works, but is there a way not to use html and write Markdown only?
Ok, found it...
.tiny[get the **best estimation** of the parameters ( \(\beta\) s), become a heRo]
Source here
.tiny[get the best estimation of the parameters ( \\(\beta\\) s), become a heRo]
I am trying to display some regression results in an rmarkdown html file. However, the output from the code chunks is too narrow and therefore it is very hard to read the p-values, etc, particularly when I include a floating table of contents. I have tried adjusting options(width = 9999) but this does not seem to fix the issue. I have also tried:
<style>
pre {
overflow-x: auto;
}
pre code {
word-wrap: normal;
white-space: pre;
}
</style>
which allows me to scroll horizontally if needed. However, given I am using regression results, I do not like having to scroll back and forth to read the coefficients and p-values. I have also tried:
<style>
.main-container { width: 1200px; max-width:2800px;}
</style>
adjusting the width and max-width values, to no avail. These solutions are suggested here.
Any idea how I can solve this issue?
Here is an example. Unfortunately I cannot post my regression results, so here is a matrix that essentially replicates the same issue:
---
title: "Untitled"
author: "me"
date: "11/10/2021"
output:
html_document:
toc: true
tow_float: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(width = 9999)
matrix = matrix(rnorm(100,5,2), ncol = 20)
matrix
However, when I knit the document, I get something like this:
How can I make the output wider?
I found out, where is your problem.
The floating toc is guilty.
Look, I took a code from that post and remove toc_float.
---
title: "R Notebook"
author: "me"
date: "11/10/2021"
output:
html_document:
toc: true
---
<style>
.main-container { width: 1800px; max-width:2800px;}
</style>
## R Markdown
```{r}
options(width = 1500)
dataM <- matrix(rnorm(100, 5, 2), ncol = 20)
dataM
```
And it works:
I think, that this space under toc is reserved. But can't say it with 100% confidence.
An addition
You should make a custom TOC with CSS.
I'm not a pro with CSS, so, I have compiled something for you. You can modify it...
CSS-file:
#TOC {
font-family: Arial, Helvetica, sans-serif;
color:black;
background-color: white;
position: fixed;
top: 0;
left: 0;
width: 150px;
border: 1px solid Black;
padding: 10px;
margin: 30px;
overflow:auto;
}
body {
max-width: 800px;
margin-left:210px;
line-height: 20px;
}
Your header:
---
title: "R Notebook"
author: "me"
date: "11/10/2021"
output:
html_document:
css: TOC.css
toc: true
---
I have a DT data table in my RMarkdown document that allows users to download to a csv. How can I shift this table to the left in my HTML file
in order to get the whole document to shift left, you can add inline CSS code in your Rmarkdown, like this
---
title: "DT table"
author: "Daniel"
date: "5/22/2021"
output: html_document
---
<style type="text/css">
.main-container {
max-width: 1800px;
margin-left: auto;
margin-right: auto;
}
</style>
```{r, message=FALSE, warning=FALSE}
library(DT)
datatable(iris)
```
Which renders in the web browser all the way to the left.
If you know more CSS than me, then you can control your document with Divs and CSS selectors to get a even more specific layout
I recently opened a standard Rmd file without editing anything. The default file looks like this:
Untitled.rmd
---
title: "myfile"
author: "Me"
date: "May 25, 2015"
output: html_document
fontsize: 12pt
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
I wanted to create an html file corresponding to the above file so in a separate R script I did the following:
knit('Untitled.Rmd', 'doc.md')
markdownToHTML('doc.md', 'testing.html',header = TRUE)
For some reason the font size doesn't work and the header information that I was hoping for doesn't appear in my testing.html. Anyone know why this is happening?
This is what I used to control font size and color in an R-markdown file. It basically overrides the CSS style sheets without having to create a new file. The example changes the sizes of the headers and titles, as well as the inline text and the R-code text, and sets some colors as well.
In my case I needed to pack more information into a document that had a specified number of pages so I made everything smaller.
---
title: "This is a title"
date: 25 May 2015
output:
html_document:
theme: cerulean
---
<style type="text/css">
body{ /* Normal */
font-size: 12px;
}
td { /* Table */
font-size: 8px;
}
h1.title {
font-size: 38px;
color: DarkRed;
}
h1 { /* Header 1 */
font-size: 28px;
color: DarkBlue;
}
h2 { /* Header 2 */
font-size: 22px;
color: DarkBlue;
}
h3 { /* Header 3 */
font-size: 18px;
font-family: "Times New Roman", Times, serif;
color: DarkBlue;
}
code.r{ /* Code block */
font-size: 12px;
}
pre { /* Code block - determines code spacing between lines */
font-size: 14px;
}
</style>
# H1 Header
Some body text
## H2 Header
More body text
### H3 Header
blah blah blah
```{r echo=T}
n <- 100
df <- data.frame(x=rnorm(n),y=rnorm(n))
```
### Another H3
Update:
Added more more styles, comments, and a bit of color to make this answer more useful. And a screen shot: