5
library(tidyverse)
library(kableExtra)
Suppose the following dataset:
games <- tibble(logo = "",
nome = c("Joao", "Pedro", "Geovanna"),
medalha = c("Gold", "Silver","Bronze"))
I want to add pictures (.png) of the corresponding medals. I created a PROJECT (File > New Project...) to work with this data so as not to worry about setting the complete path to the files.
So I have a folder called "img" where the following files are:
- 1024px-Bronze_medal.png
- 1024px-Gold_medal.png
- 1024px-Silver_medal.png
To insert the images of the medals, I did the following:
games %>%
kable() %>%
kable_paper(full_width = T) %>%
column_spec(1, image = spec_image(c("img/1024px-Gold_medal.png",
"img/1024px-Silver_medal.png",
"img/1024px-Bronze_medal.png"), 200,200))
When I did the Knit the result was as expected:
The drawback was that I needed to inform each of the paths in the function spec_image()
.
I’m trying to optimize this process. For this, I imagined the following: use the function sprintf()
to create a new column in the dataset (which I called "link". I worried about inserting the quotation marks) with the paths and pass this newly created column as an argument to the function spec_image()
games %>%
mutate(link = sprintf("'img/1024px-%s_medal.png'", medalha)) %>%
kable() %>%
kable_paper(full_width = T) %>%
column_spec(1, image = spec_image("link", 200,200))
However, when doing Knit the following error message appears:
File link not found in resource path
Erro: pandoc document conversion failed with error 99
I thought when referencing the "link" column in the function spec_image()
would be the same thing spec_image(c("img/1024px-Gold_medal.png", "img/1024px-Silver_medal.png", "img/1024px-Bronze_medal.png"))
If you reference the absolute position, it does not resolve? /home/user/img/1024px-Silver_medal.png
– Daniel Ikenaga