R markdown table loop NULL outcome - html

My code is producing a lot of NULLs at the end in the html output.
Could you please help me to prevent it?
---
title: "Test"
output: html_document
---
```{r warning=FALSE, message=FALSE, results = 'asis',echo=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
# nest all data except the cut column and create html tables
diamonds_tab <- diamonds %>%
nest(-cut) %>%
mutate(tab = map2(cut,data,function(cut,data){
writeLines(landscape(kable_styling(kable(as.data.frame(head(data)),
caption =cut,
format = "html",align = "c",row.names = FALSE),
latex_options = c("striped"), full_width = T)))
}))
# print tab column, which contains the html tables
invisible(walk(diamonds_tab$tab, print))
```

Instead of using invisible, wrap the print command with capture.output.
---
title: "Test"
output: html_document
---
```{r warning=FALSE, message=FALSE, results = 'asis',echo=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
# nest all data except the cut column and create html tables
diamonds_tab <- diamonds %>%
nest(-cut) %>%
mutate(tab = map2(cut,data,function(cut,data){
writeLines(landscape(kable_styling(kable(as.data.frame(head(data)),
caption =cut,
format = "html",align = "c",row.names = FALSE),
latex_options = c("striped"), full_width = T)))
}))
# print tab column, which contains the html tables
walk(diamonds_tab$tab, ~ capture.output(print(.x)))
```

I found out, that it is only enough to remove the walk.

Related

Rmarkdown HTML rendering issue

I am trying to print a list of HTML tables, but for some reason when I knit the document, I get the raw HTML code for output instead of the rendered table. Example:
---
title: "html-render-issue"
output: html_document
---
library(tidyverse)
library(gtsummary)
# this table renders correctly:
tbl_summary(iris)
# but this table does not!!
tables <- list(tbl_summary(iris), tbl_summary(cars))
print(tables)
I don't understand why this is happening, I tried indexing into the list with a for loop
for (i in 1:2) {
print(tables[[i]])
}
but this doesn't seem to work either! Short of doing tables[[1]]; tables[[2]] etc. (which does work), is there a way to iterate over the list and get the output I want?
Consider using results = "asis" in the r chunk and then instead of print use knitr::knit_print
```{r, results = "asis", echo = FALSE}
library(gtsummary)
# this table renders correctly:
tbl_summary(iris)
# but this table does not!!
tables <- list(tbl_summary(iris), tbl_summary(cars))
for (i in 1:2) {
cat(knitr::knit_print(tables[[i]]))
}
```
-output
Try with adding %>% as_gt() within the list!
---
title: "html-render-issue"
output: html_document
---
```{r loop_print, results = 'asis'}
library(tidyverse)
library(gtsummary)
tables <- list(tbl_summary(iris), tbl_summary(cars) %>% as_gt())
walk(tables, print) # walk from purrr package to avoid [[1]] [[2]]
```

How to produce a table with icons in an html doc. created with rmarkdown

I want to create an html report with a table with icons (arrows) but while it works for shiny or flexdashboard it does not seem to wotk when I knit to html format. Is there a way to produce such a table in html output?
---
title: "Leads Report"
output: html_document
---
```{r global, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,fig.width = 14)
library(shiny)
library(shinydashboard)
FCB<-c(5,6,4,6,8)
TWI<-c(3,5,2,3,5)
IN<-c(2,1,1,1,1)
DF1<-data.frame(FCB,TWI,IN)
FCB<-c(0,0,1,2,4)
TWI<-c(1,2,3,4,5)
IN<-c(1,3,4,5,6)
DF2<-data.frame(FCB,TWI,IN)
DF1$direction <- ifelse(DF2$FCB-DF1$FCB > 0,
paste("<p>↑</p>"),
ifelse(DF2$FCB-DF1$FCB < 0,
paste("<p>↓</p>"),
paste("<p>⇆</p>")))
```
#### Table
```{r plot1, echo=FALSE,results='hide', message=FALSE, warning=FALSE }
knitr::kable({DF1}, sanitize.text.function = function(x) x,escape = F)
```

Errors when knitting RMarkdown to HTML (no graphs showing up in the HTML)

Question about knitting RMarkdown.
I am having issues when knitting a Rmarkdown file to HTML/pdf. When I run my chunks of code in the Rmarkdown file everything runs smoothly (and I get my graphs made with ggplot) but when knitting I get an output with no graphs and errors (error in eval, error in ggplot, error in print).
Does anyone have experience with this?
The errors:
Error in eval(lhs, parent): object ‘iso3166’ not found
Error in ggplot(inci_100k, aes(long, lat, map.id=mapname,fill=inci)): object ‘inci_100k’ not found
Error in print(INCIPLOT): object ‘INCIPLOT’ not found
The code:
---
title: "R Markdown MAP"
author: "Alexandra V"
date: "1/4/2020"
output:
html_document: default
pdf_document: default
word_document: default
---
```{r,echo = FALSE, warning = FALSE, message=FALSE, error=TRUE}
knitr::opts_chunk$set(cache=TRUE)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(error = TRUE)
```
Loading the packages we will need for the following analysis.
```{r echo=FALSE, warning=FALSE}
library(tidyverse)
library(lubridate)
library(ggmap)
library(countrycode)
library(grid)
library(gridExtra)
```
To only keep the data needed to make a worldmap of TB incidences, only the relevant data will be taken from the TB_burden_countries_2020-01-04.csv file. Column 1: country names, column 3: iso3 (country codes), column 6: years, column 8: e_inc_100k (estimated incidence all TB forms per 100.000). To make the file easier to work with the names of the columns will be changed to: country, code, year and inci respectively.
```{r, message=FALSE}
TB.burden <- read.csv("TB_burden_countries_2020-01-04.csv")
TBworldINC.map <- as.data.frame(TB.burden[,c(1,3,6,8)], drop=false)
write.csv(TBworldINC.map, 'TBworldINC.map.csv', row.names = FALSE)
tb.INC <- read_csv("TBworldINC.map.csv") %>%
setNames(c("country", "code", "year", "inci"))
```
```{r}
world <- map_data("world")
tb_some_years <- tb.INC %>%
filter(year %in% c(2005, 2010, 2015, 2018))
inci_100k <- tb_some_years %>%
inner_join(iso3166 %>% select(a3, mapname), by = c(code = "a3")) %>%
left_join(world, by = c(country = "region"))
INCIPLOT <- ggplot(inci_100k, aes(long, lat, map_id = mapname,
fill = inci)) +
geom_map(map = world) +
scale_fill_gradient(low = "blue", high = "yellow") +
theme_void() +
coord_map(xlim = c(-180, 180)) +
labs(fill = "Incidence per year") +
facet_wrap(~ year, ncol = 2)
print(INCIPLOT)
```
picture of the output I get in Rstudio
I have had similar issues when making maps in R. One work around is to create your graphs and to save it locally and including the images. The syntax for adding images in R Markdown is ![alt text](path to image)

Icons do not show up at all in R flexdashboard

Im creating a flexdashboard which displays a table. In the last column icons are included but they are not displayed at all because of flexdashboard. That works normally in shinydashboard. Any workaround?
---
title: "Single Column (Fill)"
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
---
```{r global, include=FALSE}
library(shiny)
library(shinydashboard)
FCB<-c(5,6,4,6,8)
TWI<-c(3,5,2,3,5)
IN<-c(2,1,1,1,1)
DF1<-data.frame(FCB,TWI,IN)
FCB<-c(0,0,1,2,4)
TWI<-c(1,2,3,4,5)
IN<-c(1,3,4,5,6)
DF2<-data.frame(FCB,TWI,IN)
DF1$direction <- ifelse(
DF1$FCB < DF2$FCB,
as.character(icon("angle-up")),
as.character(icon("angle-down"))
)
```
### Chart 1
```{r}
renderTable(DF1, sanitize.text.function = function(x) x)
```
You are using Shiny content rendered to a static file.
I added runtime: shiny to the YAML header.
If you just need arrows would using simpe UTF-8 arrows like these be okay?
If you want to render HTML in a table in flexdashboard you should use a datatable from the DT package. Note that the rendering of the HTML is escaped by default. To render the HTML in your table you have to set escape = FALSE.
Here is an option:
---
title: "Single Column (Fill)"
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
runtime: shiny
---
```{r global, include=FALSE}
library(DT)
library(shiny)
library(shinydashboard)
FCB<-c(5,6,4,6,8)
TWI<-c(3,5,2,3,5)
IN<-c(2,1,1,1,1)
DF1<-data.frame(FCB,TWI,IN)
FCB<-c(0,0,1,2,4)
TWI<-c(1,2,3,4,5)
IN<-c(1,3,4,5,6)
DF2<-data.frame(FCB,TWI,IN)
DF1$direction <- ifelse(
DF1$FCB < DF2$FCB,
"<p>↑</p>",
"<p>↓</p>"
)
DF1.table <- datatable(DF1, escape = FALSE)
```
### Chart 1
```{r}
DT::renderDataTable(DF1.table)
```

How to trim a picture with rmarkdown/html?

I want to include a picture form the web in my markdown document, but I just want the left part of the picture. I searched on how to trim picture with rmarkdown but I found nothing...
Here is an example
---
title: "How to trim?"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE, fig.align = 'center')
```
Include picture
```{r pic}
include_graphics("http://ggplot2.tidyverse.org/README-example-1.png")
```
which gave me this HTML output.
If I want to trim the legend (the ~20% right part), how can I do?
I accept any type of answer: relative or absolute specifiation, rmarkdown or html solution, ...
Thanks!
You can use:
library(magick)
crop <- function(im, left = 0, top = 0, right = 0, bottom = 0) {
d <- dim(im[[1]]); w <- d[2]; h <- d[3]
image_crop(im, glue::glue("{w-left-right}x{h-top-bottom}+{left}+{top}"))
}
"http://ggplot2.tidyverse.org/README-example-1.png" %>%
image_read() %>%
crop(right = 210)
Thanks to #hrbrmstr comment, I found a solution.
library(magick)
library(magrittr)
image_read("http://ggplot2.tidyverse.org/README-example-1.png") %>%
image_flop() %>%
image_crop("1344x960+250") %>%
image_flop()
I'm not sure it's the most efficient with the two image_flop() functions and I don't understand precisly the "1344x960+250" but it works :)