Insert image with kableExtra function::spec_image( )

Asked

Viewed 110 times

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:

inserir a descrição da imagem aqui

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

1 answer

5


I solved the problem, but perhaps not the answer sought.

(reread the images of the medals. as they were not provided in the original post, I searched others on the internet)

The trick I used was to create the object link outside the pipe, so as not to confuse the dplyr and the kable.

library(knitr)
library(kableExtra)
library(tidyverse)

games <- tibble(logo = "",
                nome = c("Joao", "Pedro", "Geovanna"),
                medalha = c("Gold", "Silver","Bronze"))

medalha <- c("Gold", "Silver", "Bronze")

link <- sprintf("img/1024px-%s_medal.png", medalha)

kable(games) %>% 
  kable_paper(full_width = T) %>% 
  column_spec(1, image = spec_image(link, 200, 200))

inserir a descrição da imagem aqui

I understand that, although it is working, this may not be the solution sought by the AP. However, in a post on the Rstudio forum entitled Conditional formatting with column_spec Within a dplyr chain, Hao Zhu, the author of the package kableExtra, solves the problem that is put there in a similar way. He even comments the following:

The Solution is quite simple: save the final data before you create your table. Well, I understand the Joy of Piping from top to bottom. However, in cases like this one, it’s just easier and Cleaner to break them into two Pieces.

In a free translation,

The solution is quite simple: save the final dataset before creating your table. Well, I understand the satisfaction of creating a pipe from beginning to end of your code. However, in cases like this, it’s just easier and cleaner to break them into two parts.

  • 1

    Very nice Marcus. Thank you. Just one question: this is the first time I offer a reward. How do you "pay" the reward? Is it automatic or do I just need to accept your answer as the correct one?

  • 24 hours after the start of the reward offer, just click on the bonus grant icon next to the answer to be rewarded. More details can be found at this link (although there are no images describing the procedure).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.