I am using inline code in RMarkdown and I would like all the text that is a result of inline code to be a different color in the document. In this example, I would like heat.colors to be red all over the document. Is there a way to do this?
Or you can use text_spec in kableExtra. It literarily does the same thing but just a tiny bit more literal. See more here
---
title: ''
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
## R Markdown
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>.
This is inline code: `r text_spec(colnames(mtcars)[1], color = "red")`.
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 cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
### This is more inline code `r text_spec(colnames(mtcars)[2], color = "red")`.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
You can do something like:
---
title: ''
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{css echo=FALSE}
.custom-inline {
color: red;
font-weight: 700
}
```
## R Markdown
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>.
This is inline code: `r sprintf("<span class='custom-inline'>%s</span>", colnames(mtcars)[1])`.
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 cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
### This is more inline code `r sprintf("<span class='custom-inline'>%s</span>", colnames(mtcars)[2])`.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
to get:
The default templates do not wrap inline chunks in a classed <span> tag so you have to do it manually. You can make a function to do it, too.
Related
There is currently a thread for how to Wrap text around plots in Markdown but this is only for knitting to HTML. I need to be able to place text next to a plot and output to a word document.
---
title: "Untitled"
author: "Your Name"
date: "May 27, 2020"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r pressure, echo=FALSE, out.width= "65%", out.extra='style="float:right; padding:10px"'}
plot(pressure)
```
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:
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing
of the R code that generated the plot.
Please advise.
I'm trying to knit an HTML document from RMarkdown, and hide the section from both the Table of Contents (ToC) and body of the output (HTML file), while while still knitting (executing) all of the code and contents.
I'm not looking to hide just the code chunk - this can be done through echo = FALSE.
I tried using the {.hidden} option meant for flexdashboards but it only partially solves the problem, i.e. the whole section (called # The section I want to hide in the example below) disappears from the HMTL output itself, but not the table of contents.
Code example:
---
output:
html_document:
df_print: kable
fig_height: 2
fig_width: 2
number_sections: yes
toc: yes
toc_float: yes
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
warning = FALSE,
message = FALSE,
cache = FALSE,
dpi = 75,
comment = '')
```
# R Markdown
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 cars}
summary(cars)
```
# Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
# The section I want to hide {.hidden}
Super-secret text (section content I don't want to show).
```{r}
sumCars <- summary(cars)
```
# This the section I want to show again
```{r}
sumCars[1,]
```
Output:
HTML output
How can I hide the document section while still knitting?
You can use arbitrary css in markdown when exporting to html. Here is an example:
---
title: "Untitled"
output: html_document
---
# One
Hello!
# Two
<style>
#two {
display: none;
}
</style>
```{r}
x <- 1:10
```
# Three
```{r}
plot(x)
```
The unlisted class ensures that a heading is not included in the table of contents, and hidden will hide a section in HTML output. Therefore, the following should work:
# The section I want to hide {.hidden .unlisted}
I tried to create a RMarkdown file, with the default template by RStudio like below:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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 cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
When I clicked knit, it only rendered a raw html file like below, as if no css is applied to the html.
How to fix it?
Does anybody know why R markdown generates excessive white space above each plot and how can I fix that ? Is there a knitr option to be included ? Or any chunk option maybe ?
I've provided 3 images at the bottom of this post so that you can see what I mean.
Haven't used any other chunk option than echo, warning and message and about the plot it is a basic ggplot.
Let me know in the comments if I need to provide any code example of my chunks for a better view.
Edit: here's a simple rmarkdown file generating the same excessive white space above ggplots.
---
title: "Untitled"
author: "Razvan Cretu"
date: "January 9, 2019"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r import, echo=FALSE}
library('ggplot2')
```
## R Markdown
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 cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
ggplot(cars, aes(speed, dist))+
geom_line()
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
I have been trying to use a two-column layout in an Rmarkdown document which include a table rendered using Pander. I would like the table to render to the width of the column but all the options that I have tried do not seem to work. Here is a simple example.
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<div class = "row">
<div class = "col-md-6">
## R Markdown
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 cars, fig}
plot(pressure)
```
</div>
<div class = "col-md-6">
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
library(pander)
pander(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
</div>
</div>
Not using Pander but learned that kable may be better in this case. I just need to change the chunk option to results='asis' and use kable. I addition formatting of the table can be added easily with the table.attr parameter and use of Bootstrap table classes (http://v4-alpha.getbootstrap.com/content/tables/).
```{r pressure, echo=FALSE, results='asis'}
library(knitr)
#pander(pressure, convert = "html")
kable(pressure, format = "html", table.attr='class="table table-hover"')
```