0

I had previously asked about inserting images in gt tables here and gotten a lot of help. But I am now encountering a new issue. The difficult part is that I am having trouble creating a minimal example.

Consider this code from the answer to my original post

library(gt)
library(magrittr)
library(purrr)
library(ggplot2)

# Let's make some pngs
mtcars %>% 
  split(.$cyl) %>% 
  map(~ ggplot(.x, aes(hp, mpg, color = factor(gear))) + geom_point()) %>% 
  set_names(c("CA", "UT", "OH")) %>% 
  iwalk(~ ggsave(paste0(.y, "_test.png"), .x))

column_one <- c("CA", "UT", "OH")
# Put the filenames in the column
column_two <- c("CA", "UT", "OH")

dashboard.data <- data.frame(column_one, column_two, stringsAsFactors = FALSE)

names(dashboard.data)[1] <- "State"
names(dashboard.data)[2] <- "IncidenceGauge"

dboard3 <- dashboard.data %>% 
  gt() %>%
  tab_header(
    title = md("**Big Title**"),
    subtitle = md("*Subtitle*")
  ) %>%
  text_transform(
    locations = cells_body(vars(IncidenceGauge)),
    fn = function(x) {
      # loop over the elements of the column
      map_chr(x, ~ local_image(
        filename = paste0(.x, "_test.png"),
        height = 100
      ))
    }) %>% 
  cols_label(IncidenceGauge = "Risk Level")

print(dboard3)

It runs fine and produces this image

enter image description here

Now, imagine that I want to create a header row, and in that row there is no image in column two. In my case I create an image called NA_test.png that is just a blank white square. For the example below, you'll have to image that's what NA_test.png is and not a plot from mtcars. You'll see that column two now begins with NA...

library(gt)
library(magrittr)
library(purrr)
library(ggplot2)

# Let's make some pngs
mtcars %>% 
  split(.$cyl) %>% 
  map(~ ggplot(.x, aes(hp, mpg, color = factor(gear))) + geom_point()) %>% 
  set_names(c("NA", "UT", "OH")) %>% 
  iwalk(~ ggsave(paste0(.y, "_test.png"), .x))

column_one <- c("Header", "UT", "OH")
# Put the filenames in the column
column_two <- c(NA, "UT", "OH")

dashboard.data <- data.frame(column_one, column_two, stringsAsFactors = FALSE)

names(dashboard.data)[1] <- "State"
names(dashboard.data)[2] <- "IncidenceGauge"

dboard3 <- dashboard.data %>% 
  gt() %>%
  tab_header(
    title = md("**Big Title**"),
    subtitle = md("*Subtitle*")
  ) %>%
  text_transform(
    locations = cells_body(vars(IncidenceGauge)),
    fn = function(x) {
      # loop over the elements of the column
      map_chr(x, ~ local_image(
        filename = paste0(.x, "_test.png"),
        height = 100
      ))
    }) %>% 
  cols_label(IncidenceGauge = "Risk Level")

print(dboard3)

This runs as well and produces this...again you'll just have to imagine that next to Header NA_test.png is just a blank white image.

enter image description here

My issue is that in my script (much longer, but literally copied in form from this example) when I encounter NA's it seems as if it's treating them like blanks rather then substituting NA. I get error messages that R can't find _test.png. Not NA_test.png as I would expect it to look for, but _test.png.

Here's the dataframe that is passed to gt()

enter image description here

There is no call to text_transform() for the Base column, but there is for the CommunityNewCases column and that's where the error occurs.

Can anyone suggest any reason as to why this is happening?

jerH
  • 1,085
  • 1
  • 12
  • 30
  • How is your actual code different from what you have shared here especially the classes of column in your data and the code with which you read the images. Do you use `map_chr(x, ~ local_image(filename = paste0(.x, "_test.png"),height = 100)` there as well? – Ronak Shah Feb 25 '21 at 03:16
  • The dataframe for the actual code is much lager, 11 columns x 10 rows, and there is some path information in the `paste0` call `map_chr(x, ~ local_image(filename = paste0("C:/Users/jerem/OneDrive/Desktop/Work/COVID_master/", plotDate,"/",.x,"_lifetime_base_cases_final.png"), height=50` I'll gather up the column classes... – jerH Feb 25 '21 at 03:26
  • the error I get is `In file(con, "rb") : cannot open file 'C:/Users/jerem/OneDrive/Desktop/Work/COVID_master/Feb24/_lifetime_base_cases_final.png': No such file or directory`. The "lifetime_base_cases" part tells me this is associated with column 8 of the dataframe, which is of class character. The first row in column 8 is NA and the remaining 9 are single words – jerH Feb 25 '21 at 03:46
  • Are you sure you data as `NA` in the file or you have empty values ? (`""`) and that is why you are getting `/_lifetime_base_cases_final.png` as filename. – Ronak Shah Feb 25 '21 at 08:47
  • Positive...edited the OP to add a screenshot of the dataframe passed to `gt()` – jerH Feb 25 '21 at 08:59
  • I am not really sure what is causing this error then. Can you try pasting the path outside of `map_chr` ? Something like this `map_chr(paste0(x, "_test.png"), ~ local_image(filename = .x, height = 100))`. See if this changes anything. – Ronak Shah Feb 25 '21 at 09:38

1 Answers1

0

How to handle NA fields is important to dashboard design. So the real question is - should a dashboard really aggregate a plot of the entries which have "NA" as location? NA is typically a data entry error, so isn't the plot misleading?

As a statistical language, R considers the logical NA as a blank or empty data field. This is by design. And it is powerful, because NA retains its meaning in both character and numeric fields.

To change R's standard behavior, simply reassign an alternative value to the logical NA. For example, your sample code can replace logical NA with the character "NA" to use it in a paste command.

  column_two <- c("NA", "UT", "OH")

Or if your data is more complex than this toy example, replace NA with "NA" using tidyr library.

  dboard3 <- dashboard.data %>% 
    tidyr::replace_na("NA") %>%
    gt() %>%
    ... 

Alternatively, you could retain the logical NA, but handle with ifelse and if.na() test as shown here...

dboard3 <- dashboard.data %>% 
 gt() %>%
 tab_header(
   title = md("**Big Title**"),
   subtitle = md("*Subtitle*")
 ) %>%
 text_transform(
   locations = cells_body(vars(IncidenceGauge)),
   fn = function(x) {
     # loop over the elements of the column
     map_chr(x, ~ local_image(
       filename = ifelse(is.na(.x),
             "NA_test.png",
             paste0(.x, "_test.png")),
       height = 100
     ))
   }) %>% 
 cols_label(IncidenceGauge = "Risk Level")
GGAnderson
  • 1,993
  • 1
  • 14
  • 25