Home > OS >  Apply gt::html(glue::glue(()) to columns with NA values
Apply gt::html(glue::glue(()) to columns with NA values

Time:10-19

For a dataset df as follows:

df <- structure(list(type = c("v1", "v2", "v3"), value = c(1.2, 1.6, 
-0.3), direction1 = c(0.1, NA, 0), direction2 = c(0.0, -0.1, 1.2
)), class = "data.frame", row.names = c(NA, -3L))

With code below, I'm able to customize column direction2 by conditional coloring and adding arrows:

df %>%
  gt() %>%
  fmt_missing(
    columns = c('direction1', 'direction2'),
    missing_text = "-"
  ) %>%
  text_transform(
    locations = cells_body(columns = c(`direction2`)),
    fn = function(x){
      `direction1` <- as.numeric(x)
      `direction2` <- as.numeric(x)
      choose_logo <-function(x){
        if (x == 0){
          gt::html(glue::glue("<span style='color:#bfbfbf;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-right", fill = gray))
      } else if (x > 0){
         gt::html(glue::glue("<span style='color:#CF000F;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-up", fill = red))
      } else if (x < 0) {
        gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-down", fill = blue))
      }
        else if (x == '-') {
        gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("", fill = tjd_blue))
      }
      } 
      map(`direction1`, choose_logo)
      map(`direction2`, choose_logo)
    }
  )

Out:

enter image description here

Now I hope to apply it to direction1 as well, but since it has a NA value which I transformed to - with fmt_missing function, it raises an error: Error in if (x == 0) { : missing value where TRUE/FALSE needed.

Please note I have created an else if (x == '-') condition, but it's not working. Anyone could to deal with issue? Thanks a lot at advance.

CodePudding user response:

fmt_missing is only for formatting and does not change the value of x which will be feed into choose_logo. So it is still NA. You must catch that situation first, because e.g. 5 == NA is neither TRUE nor FALSE:

library(tidyverse)
library(gt)

df <- structure(list(type = c("v1", "v2", "v3"), value = c(
  1.2, 1.6,
  -0.3
), direction1 = c(0.1, NA, 0), direction2 = c(0.0, -0.1, 1.2)), class = "data.frame", row.names = c(NA, -3L))
df

df %>%
  gt() %>%
  fmt_missing(
    columns = c("direction1", "direction2"),
    missing_text = "-"
  ) %>%
  text_transform(
    locations = cells_body(columns = c(`direction2`, `direction1`)),
    fn = function(x) {
      `direction1` <- as.numeric(x)
      `direction2` <- as.numeric(x)
      choose_logo <- function(x) {
        if(is.na(x)) {
          return(
            gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"))
          )
        }
        
        if (x == 0) {
          gt::html(glue::glue("<span style='color:#bfbfbf;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-right", fill = 'gray'))
        } else if (x > 0) {
          gt::html(glue::glue("<span style='color:#CF000F;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-up", fill = 'red'))
        } else if (x < 0) {
          gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-down", fill = 'blue'))
        }
      }
      map(`direction1`, choose_logo)
      map(`direction2`, choose_logo)
    }
  )

enter image description here

  •  Tags:  
  • r gt
  • Related