Why some outputs are not printed in knitr? - r

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")
}
```

Related

Why `code_folding: hide` doesnt give the option to hide/show code in Rmarkdown?

This is my Rmarkdown code:
---
title: "Tutorial"
output:learnr::tutorial:
code_folding: hide
runtime: shiny_prerendered
---
```{r setup}
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
```
## Topic 1
### Exercise
*Here's a simple exercise with an empty code chunk provided for entering the answer.*
Write the R code required to add two plus two:
```{r two-plus-two, exercise=TRUE}
library(tidyverse)
mtcars %>% select(cyl, mpg)
```
### Exercise with Code
*Here's an exercise with some prepopulated code as well as `exercise.lines = 5` to provide a bit more initial room to work.*
Now write a function that adds any two numbers and then call it:
```{r add-function, exercise=TRUE, exercise.lines = 5}
add <- function() {
}
```
I already tried all the identation options , but its not working. Any help?
As seems, code-folding option doesn't work for output "learnr".
For html_doc - yes. F.e.:
output:
html_document:
code_folding: hide
But you can try next solution with details:
CSS:
<style>
details {
border: 1px solid;
border-radius: 10px;
padding: .1em .5em 0;
text-align: right;
}
</style>
Using:
<details>
<summary>Show/hide code</summary>
```{r two-plus-two, exercise=TRUE}
library(tidyverse)
mtcars %>% select(cyl, mpg)
```
</details>
Output:
P.S. If you need a support to IE/Edge - look there

In R Bookdown, how to set font family and size for different level text?

In R Bookdown, how to set font family and size for different level text ? (I want apply this setting for the whole book)
Here is the wished setting parameter as attached image .Thanks!
(Additional, i want to change the code font also as 'family:Arial size=9')
---
title: "A Minimal Book Example"
author: "John Doe"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib, packages.bib]
link-citations: yes
github-repo: rstudio/bookdown-demo
---
# About
## Usage
1. Find the **Build** pane in the RStudio IDE, and
Or build the book from the R console:
```{r, eval=FALSE}
bookdown::render_book()
```
Then don't think twice and use CSS.
If you will have more rules, make a separate CSS-file for the clarity of your .Rmd-file.
Your example:
<style>
h1 {
font-family: 'Arial';
font-size: 20pt;
}
h2 {
font-family: 'Arial Black';
font-size: 15pt;
}
pg {
font-family: 'MS UI Gothic';
font-size: 6pt;
}
</style>
# About
## Usage
<pg> 1. Find the **Build** pane in the RStudio IDE, and
Or build the book from the R console: </pg>

how to number custom blocks in R markdown [duplicate]

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.

R markdown Error: cannot change value of locked binding for '.GlobalEnv' Execution halted

I'm running RStudio Version 1.2.1335 and am having difficulty knitting an R markdown file to HTML. I don't know what this error message means or what a solution might be. I/ve gone through and run each chunk to ensure there are no coding errors. I was unable to find a similar problem/question online. Thanks for any help!
Here is an example start to my markdown file:
---
title: "Linking environmental contaminants with hepatic and biliary disease in White perch from Choptank and Severn Rivers"
author:
- Mark Matsche
- Cooperative Oxford Laboratory
- mark.matsche#maryland.gov
date: '`r Sys.Date()`'
output:
html_document:
df_print: paged
fig_caption: yes
theme: united
toc: yes
toc_float: yes
indent: yes
header-includes: \setlength\parindent{24pt}
---
<style type="text/css">
body, td {
font-size: 12pt;
}
code.r{
font-size: 16pt;
}
pre {
font-size: 16pt
}
caption {
color: black;
font-weight: bold;
font-size: 1.0em;
}
</style>
```{r setup, include = FALSE}
options(knitr.table.format = "html")
knitr::opts_chunk$set(echo = TRUE)
```
```{r echo=FALSE, include= FALSE, message= FALSE, warning= FALSE}
# This function will load packages.
library(tidyverse)
library(readxl)
library(kableExtra)
library(knitr)
library(ggplot2)
library(png)
library(tinytex)
library(scales)
library(grid)
library(svglite) # needed to save vector graphics
library(broom)
library(lemon)
#library(plyr)
#library(rms)
#library(GGally)
#library(car) #need for calculating vifs
#library(leaps) #stepwise regression
#library(DAAG) #need for doing cross validation
#library(rms) #check structural form
#library(ROCR) #compute sensitivity and specificity
```
```{r readxl, echo=FALSE}
Alldata<-read_excel("Data12Aug.xlsx")
```

Adding custom CSS tags to an RMarkdown html document

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%;
}

Resources