1

I have a table that I made with the kableExtra package in R, using kbl(). Each row has a state abbreviation, like 'NJ', 'ME', etc, and the columns have info about each state.

Is there an easy way to add a column that just has the shape pictures of the states? This would make it easy to quickly identify states. like the row with NJ would have the shape of NJ next to it.

Edit: I know how to add an image to the table, as discussed here: Add an image to a table-like output in R, but I'm specifically looking for a way to get the US state shapes.

FG120
  • 61
  • 1
  • 4
  • 1
    Does this answer your question? [Add an image to a table-like output in R](https://stackoverflow.com/questions/25106481/add-an-image-to-a-table-like-output-in-r) – Limey Mar 23 '21 at 14:51
  • Welcome to SO, @FG120. I posted an answer with a Html output. Please let me know if this is what you are looking for. Please do not hesitate to ask any question regarding this answer. – bttomio Mar 23 '21 at 15:11

1 Answers1

0

This is the new edit following this answer. You could use this example to put shapes next to the state abbreviation:

Html ouput

---
title: "Shapes"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(kableExtra)
```

```{r, echo=FALSE}
tbl_img <-data.frame(
  name = c("alabama", "alaska"),
  abbr = c("AL", "AK"))

tbl_img$shape <- sprintf('![](https://suncatcherstudio.com/uploads/patterns/us-states/map-outlines/svg/%s-map-outline-dddddd.png){width=25px}', tbl_img$name)

tbl_img %>%
  select(-name) %>%
  kbl(booktabs = T) %>% 
  kable_paper(full_width = F)
```

-output

enter image description here

bttomio
  • 2,206
  • 1
  • 6
  • 17
  • Thanks! This is helpful, but I was looking for state shapes, not flags. Also, I'd use the flags if it were easier to get them for all 50 states, rather than manually grabbing all the links. – FG120 Mar 23 '21 at 16:32
  • It's fine, you're welcome. Just edited my answer. – bttomio Mar 23 '21 at 20:50
  • This is great! Works for most states, but doesn't work for states with more than one name, like New Jersey. If we change to `tbl_img <-data.frame(name = gsub(" ", "-", tolower(state.name)), abbr = state.abb)`, then it works for all states. – FG120 Mar 24 '21 at 09:50
  • Nice. Please feel free to edit my answer! If it was helpful, could you please confirm/upvote it? Thanks. – bttomio Mar 24 '21 at 09:52
  • 1
    I tried. Didn't work. It said "edit queue is full". Nonetheless, I accepted – FG120 Mar 24 '21 at 12:25