1

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!

1 Answers1

0

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
}
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Yes, that is why I used lapply to convert the list elements to tibbles. What I'm trying to understand is why "lcd " is being added to the object class. It wasn't happening before I updated tidyverse. – Tania Alarcon Aug 05 '20 at 21:33
  • @TaniaAlarcon you applied the function `lcd` and it is creating that class. May be either of the packages updated to create the attribute – akrun Aug 05 '20 at 21:41
  • Oh so it is the lcd function that is creating that class and it likely has nothing to do with me updating tidyverse. I will contact the rnoaa package creators to ask about this annoying class modification. Thank you! – Tania Alarcon Aug 06 '20 at 17:08
  • @TaniaAlarcon yes, it is the modification of class. You can create a copy of that function and remove the last line (as it is a small function) – akrun Aug 06 '20 at 17:10