ropensci / rnoaa

R interface to many NOAA data APIs
https://docs.ropensci.org/rnoaa
Other
328 stars 84 forks source link

Class modification when using lcd #369

Closed TaniaAlarcon closed 4 years ago

TaniaAlarcon commented 4 years ago

The lcd package adds the word "lcd" to the object class, which causes problems when working with other R packages.

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
}

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 converting RawData back to tibbles.

What is the reason for having the lcd function modify the object class?

Here is my session information:

Session Info ```r setting value version R version 4.0.2 (2020-06-22) os Windows 10 x64 system i386, mingw32 ui RStudio language (EN) collate English_United States.1252 ctype English_United States.1252 tz America/New_York date 2020-08-07 - Packages --------------------------------------------------------------------------------------------------------------------------------------------------------- package * version date lib source assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.2) backports 1.1.8 2020-06-17 [1] CRAN (R 4.0.2) blob 1.2.1 2020-01-20 [1] CRAN (R 4.0.2) broom 0.7.0 2020-07-09 [1] CRAN (R 4.0.2) callr 3.4.3 2020-03-28 [1] CRAN (R 4.0.2) cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.0.2) cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.2) colorspace 1.4-1 2019-03-18 [1] CRAN (R 4.0.2) crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.2) crul 1.0.0 2020-07-30 [1] CRAN (R 4.0.2) curl 4.3 2019-12-02 [1] CRAN (R 4.0.2) DBI 1.1.0 2019-12-15 [1] CRAN (R 4.0.2) dbplyr 1.4.4 2020-05-27 [1] CRAN (R 4.0.2) desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.2) devtools 2.3.1 2020-07-21 [1] CRAN (R 4.0.2) digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.2) dplyr * 1.0.1 2020-07-31 [1] CRAN (R 4.0.2) ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.2) fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.2) forcats * 0.5.0 2020-03-01 [1] CRAN (R 4.0.2) fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2) generics 0.0.2 2018-11-29 [1] CRAN (R 4.0.2) ggplot2 * 3.3.2 2020-06-19 [1] CRAN (R 4.0.2) glue 1.4.1 2020-05-13 [1] CRAN (R 4.0.2) gridExtra 2.3 2017-09-09 [1] CRAN (R 4.0.2) gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.2) haven 2.3.1 2020-06-01 [1] CRAN (R 4.0.2) hms 0.5.3 2020-01-08 [1] CRAN (R 4.0.2) hoardr 0.5.2 2018-12-02 [1] CRAN (R 4.0.2) httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.0.2) httr 1.4.2 2020-07-20 [1] CRAN (R 4.0.2) jsonlite 1.7.0 2020-06-25 [1] CRAN (R 4.0.2) lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.2) lubridate 1.7.9 2020-06-08 [1] CRAN (R 4.0.2) magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.2) memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.2) modelr 0.1.8 2020-05-19 [1] CRAN (R 4.0.2) munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.2) pillar 1.4.6 2020-07-10 [1] CRAN (R 4.0.2) pkgbuild 1.1.0 2020-07-13 [1] CRAN (R 4.0.2) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.2) pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.2) prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.2) processx 3.4.3 2020-07-05 [1] CRAN (R 4.0.2) ps 1.3.3 2020-05-08 [1] CRAN (R 4.0.2) purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.0.2) R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.2) rappdirs 0.3.1 2016-03-28 [1] CRAN (R 4.0.2) Rcpp 1.0.5 2020-07-06 [1] CRAN (R 4.0.2) readr * 1.3.1 2018-12-21 [1] CRAN (R 4.0.2) readxl 1.3.1 2019-03-13 [1] CRAN (R 4.0.2) remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.2) reprex 0.3.0 2019-05-16 [1] CRAN (R 4.0.2) rlang 0.4.7 2020-07-09 [1] CRAN (R 4.0.2) rnoaa * 1.1.0 2020-07-08 [1] CRAN (R 4.0.2) rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.2) rstudioapi 0.11 2020-02-07 [1] CRAN (R 4.0.2) rvest 0.3.6 2020-07-25 [1] CRAN (R 4.0.2) scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.2) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.2) stringi 1.4.6 2020-02-17 [1] CRAN (R 4.0.0) stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.0.2) testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.2) tibble * 3.0.3 2020-07-10 [1] CRAN (R 4.0.2) tidyr * 1.1.1 2020-07-31 [1] CRAN (R 4.0.2) tidyselect 1.1.0 2020-05-11 [1] CRAN (R 4.0.2) tidyverse * 1.3.0 2019-11-21 [1] CRAN (R 4.0.2) usethis 1.6.1 2020-04-29 [1] CRAN (R 4.0.2) vctrs 0.3.2 2020-07-15 [1] CRAN (R 4.0.2) withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.2) XML 3.99-0.5 2020-07-23 [1] CRAN (R 4.0.2) xml2 1.3.2 2020-04-23 [1] CRAN (R 4.0.2) ```
sckott commented 4 years ago

Are you sure the problem is with lcd as part of the class info? I can't replicate your code (i dont know where crossing comes from). You're calling as.character() on a data.frame and the error is saying mutate expects a vector, which suggests to me it's expecting a vector of items, not a data.frame.

TaniaAlarcon commented 4 years ago

Crossing is a tidyverse function: https://tidyr.tidyverse.org/reference/expand.html I can use a more straight forward example:

dat <- lcd(72658014922, 2000)

class(dat)

[1] "tbl_df"     "tbl"        "data.frame" "lcd" 

The problem appears to be the modification of the class with the word "lcd". The source code for the lcd function shows that this modification happens in the last line of the lcd function:

lcd <- 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"))
}
sckott commented 4 years ago

fixed now