rpurrrrnoaa

Strange tibble class when using lcd function from rnoaa package


I recently updated to the newest versions of R, RStudio, and tidyverse, and now I have an error when running the ldc function from the rnoaa package. This error only started after the updates.

R version 4.0.2 (2020-06-22)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.0   stringr_1.4.0   dplyr_1.0.1     purrr_0.3.4     readr_1.3.1     tidyr_1.1.1     tibble_3.0.3    ggplot2_3.3.2   tidyverse_1.3.0 rnoaa_1.1.0    

loaded via a namespace (and not attached):
 [1] tinytex_0.25     tidyselect_1.1.0 xfun_0.16        haven_2.3.1      colorspace_1.4-1 vctrs_0.3.2      generics_0.0.2   utf8_1.1.4       blob_1.2.1      
[10] XML_3.99-0.5     rlang_0.4.7      pillar_1.4.6     withr_2.2.0      httpcode_0.3.0   glue_1.4.1       DBI_1.1.0        rappdirs_0.3.1   dbplyr_1.4.4    
[19] modelr_0.1.8     readxl_1.3.1     lifecycle_0.2.0  munsell_0.5.0    gtable_0.3.0     cellranger_1.1.0 rvest_0.3.6      curl_4.3         fansi_0.4.1     
[28] hoardr_0.5.2     triebeard_0.3.0  urltools_1.7.3   broom_0.7.0      Rcpp_1.0.5       scales_1.1.1     backports_1.1.8  jsonlite_1.7.0   fs_1.5.0        
[37] gridExtra_2.3    hms_0.5.3        digest_0.6.25    stringi_1.4.6    grid_4.0.2       cli_2.0.2        tools_4.0.2      magrittr_1.5     crul_1.0.0      
[46] crayon_1.3.4     pkgconfig_2.0.3  ellipsis_0.3.1   xml2_1.3.2       reprex_0.3.0     lubridate_1.7.9  assertthat_0.2.1 httr_1.4.2       rstudioapi_0.11 
[55] R6_2.4.1         compiler_4.0.2  

Here is a sample data set:

Year    <- seq(from = 2000, to = 2003)
Station <- c(72658014922)
dat     <- crossing(Station, Year)

I use map to get NOAA's LCD records for each year in the data.

dat <- dat %>% 
  mutate(RawData = map2(Station, Year, lcd))

When I check class for the RawData, I get the following:

class(dat$RawData[[1]])

"tbl_df"     "tbl"        "data.frame" "lcd"   

Since there is this strange "lcd" included in the class, any call to RawData gives this error:

dat <- dat %>%
  mutate(RawData = map(RawData, ~mutate_all(., as.character)))

Error: Problem with `mutate()` input `RawData`.
x `x` must be a vector, not a `tbl_df/tbl/data.frame/lcd` object.
i Input `RawData` is `map(RawData, ~mutate_all(., as.character))`.

I can get around this error by running this code:

dat <- dat %>%
  mutate(RawData = lapply(RawData, as_tibble))

But I am very confused about why map2 with the lcd function gives an object of class tbl_df/tbl/data.frame/lcd.

I would really appreciate any help understanding this error!


Solution

  • The attribute lcd is creating the issue

    āœ– x must be a vector, not a tbl_df/tbl/data.frame/lcd object.

    , can be converted to tibble and it should work

    library(dplyr)
    library(purrr)
    dat %>% 
       mutate(RawData = map(RawData, ~
             .x %>% 
               as_tibble %>% 
                mutate(across(everything(), as.character))))
    

    If we check the function lcd, it adds the lcd class attribute at the end

    function (station, year, ...) 
    {
        assert(station, c("character", "numeric", "integer"))
        assert(year, c("character", "numeric", "integer"))
        assert_range(year, 1901:format(Sys.Date(), "%Y"))
        path <- lcd_get(station = station, year = year, ...)
        tmp <- safe_read_csv(path)
        names(tmp) <- tolower(names(tmp))
        df <- tibble::as_tibble(tmp)
        structure(df, class = c(class(df), "lcd")) # // adding the class attribute
    }