Show code in flexdashboard - r

I'm trying to produce a flexdashboard with R and want to show code in my presentation, but this seems not to work.
Here a little example:
---
title: "Test"
output:
flexdashboard::flex_dashboard
---
```{r setup, include=FALSE}
library(flexdashboard)
```
### Code
```{r, eval=FALSE, include=TRUE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
### Output
```{r, fig.align='center', echo = FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
Even other chuck options like fig.show = 'hide' will not work.
Is it possible to show code in a R-chuck in flexdashboard?
The code highlights would be a benefit instead of a plain text.

If you want both the code and plot to show set the chunk options to: echo = true
If you just want code set it to: echo=TRUE, eval=FALSE
---
title: "Test"
output:
flexdashboard::flex_dashboard
---
```{r setup, include=FALSE}
library(flexdashboard)
```
### Code
```{r, echo=TRUE, eval=FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
### Code and Plot
```{r, echo=TRUE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
### Plot
```{r, fig.align='center', echo = FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```

It won't show as a panel in your presentation, but to add a </> Source Code button to your dashboard, include source_code: embed in your YAML.
---
title: "Example"
output:
flexdashboard::flex_dashboard:
source_code: embed
---

Related

RMarkdown is not referencing tables

Somehow my RMarkdown document is not crossreferencing tables or figures. Here is a stripped down version of my document.
---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
var1<-sample(LETTERS)
tab1<-table(var1)
My table is in Table \#ref{tab:tab1}
library(knitr)
kable(tab1, caption="my table")
AS we see in Figure \#ref{fig:plot1}
plot(seq(1,10,1))
You should call your tab1 in the code chunk like this {r tab1}. And use () instead of {} for your #ref. In that case it reference to your figures and tables. You can use the following code:
---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---
My table is in Table \#ref(tab:tab1)
```{r tab1, echo =FALSE}
var1<-sample(LETTERS)
tab1<-table(var1)
library(knitr)
kable(tab1, caption="my table")
```
\newpage
AS we see in Figure \#ref(fig:plot1)
```{r plot1, fig.cap ="plot", echo=FALSE}
par(mar = c(4, 4, .2, .1))
plot(seq(1,10,1))
```
Output:
As you can see on the image, when you click on 1 in will go to your table.

About the hyperlink in RMARKDOWN

In rmarkdown, we can create a catalog using 'toc: yes',using the hyperlink, i can link to subtitle.
Now, i want to link subtitle to catalog ? (in the final html, i with to click subtitle,then back to top of the file)
---
title:'my markdown file'
output:
html_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## subtitle 1 :R Markdown
```{r cars}
summary(cars)
```
## subtitle 2:Including Plots
```{r pressure, echo=FALSE}
plot(pressure)
```
You may enclose subheader in an HTML tag <a>.
---
title: 'my markdown file'
output:
html_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## subtitle 1 :R Markdown
```{r cars}
summary(cars)
as.matrix(rnorm(100))
```
## subtitle 2:Including Plots
```{r pressure, echo=FALSE}
plot(pressure)
```

Rmarkdown not rendering raw HTML

After upgrading Rstudio to 1.4 version, when I render this rmarkdown doc
---
title: "Raw HTML"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
library(htmltools)
knitr::opts_chunk$set(echo = F)
```
```{r}
f <- function(text){
cat(asis_output(htmltools::htmlPreserve(paste("<span style=\"color:green\">", text, "</span>"))))
return(0)
}
```
```{r}
x <- f('Hello!')
```
I get this html doc
But before the update this code worked as expected, rendering this html doc
How can I get the second html doc?
cat removes the knit_asis class.
You can either avoid cat or use results='asis' chunck option :
---
title: "Raw HTML"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
library(htmltools)
knitr::opts_chunk$set(echo = F)
```
```{r}
side.effect <- function(text){
cat(asis_output(htmltools::htmlPreserve(paste("<span style=\"color:green\">", text, "</span>"))))
return(0)
}
direct <- function(text){
asis_output(htmltools::htmlPreserve(paste("<span style=\"color:green\">", text, "</span>")))
}
```
```{r}
direct('direct Hello!')
```
```{r,results ='asis'}
x <- side.effect('side effect Hello!')
```

Disable execution of chunk with R markdown eval variable

I am trying to define a set of variables to control on/off of the execution of chunks in my Rmarkdown. However, I noticed that with the variable1 = FALSE and {r chunk1, eval = variable1}, the chunk will be still executed (by hitting "Run All Chunks" button). For knitting, this approach works, and the chunk will not be evaluated. Is there anything that I have missed.
For example, the following Rmarkdown will execute pressure1 and plot the pressure with eval = varaible1, but eval =FALSE worked for pressure2 and disable the chunk.
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r switches, include=FALSE}
variable1 = FALSE
```
```{r cars}
summary(cars)
```
```{r pressure1, eval=variable1}
print("pressure1")
plot(pressure)
```
```{r pressure2, eval=FALSE}
print("pressure2")
plot(pressure)
```

Displaying data frame in r markdown

I want to have table in 'one line' instead of dividing it. In the second picture is what I get and in the first picture is what I want to get. Thanks for your help.
1) https://i.stack.imgur.com/uwfZ5.png
2) https://i.stack.imgur.com/6nUJ2.png
my code
title: "Bundesliga - raport"
author: "aa aak"
date: "3 stycznia 2018"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=FALSE, warning=FALSE, message=FALSE)
library(vcd)
```
## Bundesliga
a
```{r echo=FALSE}
head(Bundesliga)
```
Use the width option. This works on your example:
```{r echo=FALSE}
options(width = 100)
head(Bundesliga)
```

Resources