代码之家  ›  专栏  ›  技术社区  ›  Essi

R gt表-从文本转换中排除NA值

  •  0
  • Essi  · 技术社区  · 3 年前

    如何排除NA值转换为图标?有了下面的代码,你也需要有NA图标,我只是把它涂成了消失的背景色。有没有更好的方法来排除需要转换的NA?我专栏中的所有条目都是字符类。

    text_transform(
        locations = cells_body(columns = 2:ncol(df)),
        fn = function(x) {
          # loop over the elements of the column
          map_chr(x, ~ local_image(
            filename = paste0(.x, ".png"),
            height = 13))}) %>%
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Nir Graham    3 年前

    在这里,我从细节中抽象出来,但展示了一种可以用来解决自己案件的方法。

    library(purrr)
    library(dplyr)
    
    local_image <- function(f, h) {
      paste(f, h, sep = "_")
    }
    
    # spoil with an NA
    x <- c(letters[1:3], NA)
    # if else to dodge it from being in local_image
    (step_1 <- map_chr(
      x,
      ~ if_else(!is.na(.x),
        local_image(
          f = paste0(.x, ".png"),
          h = 13
        ), NA
      )
    ))
    
    # clear the NAs
    (step_2 <- step_1[!is.na(step_1)])