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!')
```
Related
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.
I have the followin markdown:
---
title: "My report"
output: html_document
---
```{r setup, include=FALSE}
library(DT)
my_func <- function(x) {
#DT::datatable(x)
print(DT::datatable(x))
nrow(x)
}
```
```{r}
x <- my_func(mtcars)
print(x)
```
I want to display a DT from inside the function, but this function performs some calculations that I'm interested in the output of. So far it doesn't display the table. How can I force markdown to generate a table without returning it?
What about something like this:
---
title: "My report"
output: html_document
---
```{r setup, include=FALSE}
library(DT)
my_func <- function(x) {
res <- list(dt = DT::datatable(x),
nr = nrow(x))
class(res) <- "myfun"
invisible(res)
}
```
```{r}
x <- my_func(mtcars)
x$nr
```
```{r}
x$dt
```
I want to create dynamic sections in my R markdown document. For this, I use R chunks with the asis output type. The chunks contain tables created by the reactable package.
I cannot get the tables to print when I create them in a for-loop. I know that one usually must wrap plots or the like within print() in loops, but that had no effect in my case.
How can I get the tables to print?
---
title: "Test"
author: "Test"
date: "29 11 2021"
output: html_document
---
```{r include=FALSE}
library(reactable)
```
```{r results='asis', echo=FALSE}
cat("\n\n## My header 1 \n\n")
reactable(data.frame(test = rnorm(3))) ## This works
```
```{r results='asis', echo=FALSE}
for (i in 1:3) {
cat("\n\n## My header ", i+1, "\n\n")
print(reactable(data.frame(test = rnorm(3)))) ## shows nothing
}
```
I just found out, that reactable uses htmlwidgets under the hood. So one can wrap the result in shiny::tagList() to display it in a loop.
---
title: "Test"
author: "Test"
date: "29 11 2021"
output: html_document
---
```{r include=FALSE}
library(reactable)
```
```{r results='asis', echo=FALSE}
cat("\n\n## My header 1 \n\n")
reactable(data.frame(test = rnorm(3))) ## This works
```
```{r results='asis', echo=FALSE}
for (i in 1:3) {
cat("\n\n## My header ", i+1, "\n\n")
print(shiny::tagList(reactable(data.frame(test = rnorm(3))))) ## now it works
}
```
You could try to export your react-tables as temporary html-files that you then import as text and delete afterwards.
Here is a solution that worked for me:
---
title: "Test"
author: "Test"
date: "29 11 2021"
output: html_document
---
```{r include=FALSE}
library(reactable)
```
```{r results='asis', echo=FALSE}
cat("\n\n## My header 1 \n\n")
reactable(data.frame(test = rnorm(3))) ## This works
```
```{r results='asis', echo=FALSE}
for (i in 1:3) {
cat("\n\n## My header ", i+1, "\n\n")
htmlwidgets::saveWidget(reactable(data.frame(test = rnorm(3))),
file = 'temp.html')
cat(readr::read_lines('temp.html')[-1])
file.remove('temp.html')
}
```
When I want to print some tables in html report from knitr, sometime I use knitr::kable(), and other times I use htmltable::htmltable.
When I use bookdown::html_document2 in the YAML numbering of tables from kable is automatic. However, it is not for htmltable, and I need to use options(table_counter = TRUE), which generates another numbering mechanism. Is there a way to unify it?
Example:
---
title: "Untitled"
author: "Guilherme"
date: "10/26/2020"
output:
bookdown::html_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(table_counter = TRUE)
```
```{r cars}
library(htmlTable)
htmlTable(mtcars[1:5,1:5],
caption = "XX")
```
```{r}
htmlTable(mtcars[1:5,1:5],
caption = "XX")
```
```{r}
library(knitr)
kable(mtcars[1:5,1:5],
caption = "XX")
```
Outputs:
Thanks!
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
---