Skip to content

Commit

Permalink
use |> pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
huizezhang-sherry committed Nov 15, 2023
1 parent 99a88e6 commit 54b2f19
Show file tree
Hide file tree
Showing 30 changed files with 180 additions and 269 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ S3method(ungroup,spatial_cubble_df)
S3method(ungroup,temporal_cubble_df)
S3method(update_cubble,spatial_cubble_df)
S3method(update_cubble,temporal_cubble_df)
export("%>%")
export(as_cubble)
export(bind_cols.spatial_cubble_df)
export(bind_cols.temporal_cubble_df)
Expand Down Expand Up @@ -115,7 +114,6 @@ importFrom(rlang,as_name)
importFrom(rlang,quo_is_missing)
importFrom(rlang,sym)
importFrom(stats,setNames)
importFrom(tibble,"%>%")
importFrom(tibble,as_tibble)
importFrom(tibble,new_tibble)
importFrom(tibble,tbl_sum)
Expand Down
24 changes: 12 additions & 12 deletions R/as_cubble.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#' @export
#' @return a cubble object
#' @examples
#' climate_flat %>% as_cubble(key = id, index = date, coords = c(long, lat))
#' climate_flat |> as_cubble(key = id, index = date, coords = c(long, lat))
#'
#' # only need `coords` if create from a tsibble
#' dt <- climate_flat %>% tsibble::as_tsibble(key = id, index = date)
#' dt %>% as_cubble(coords = c(long, lat))
#' dt <- climate_flat |> tsibble::as_tsibble(key = id, index = date)
#' dt |> as_cubble(coords = c(long, lat))
#'
#' # netcdf
#' path <- system.file("ncdf/era5-pressure.nc", package = "cubble")
Expand All @@ -38,14 +38,14 @@
#'# stars - take a few seconds to run
#' tif <- system.file("tif/L7_ETMs.tif", package = "stars")
#' x <- stars::read_stars(tif)
#' x %>% as_cubble()
#' x |> as_cubble()
#'}
#'
#' # don't have to supply coords if create from a sftime
#' dt <- climate_flat %>%
#' sf::st_as_sf(coords = c("long", "lat"), crs = sf::st_crs("OGC:CRS84")) %>%
#' sftime::st_as_sftime()
#' dt %>% as_cubble(key = id, index = date)
#' dt |> as_cubble(key = id, index = date)
as_cubble <- function(data, key, index, coords, ...) {
UseMethod("as_cubble")
}
Expand Down Expand Up @@ -87,7 +87,7 @@ as_cubble.tbl_df <- function(data, key, index, coords, ...) {
listcol_var <- listcol_var[1]
if (listcol_var != "ts")
colnames(data)[colnames(data) == listcol_var] <- "ts"
chopped <- data %>% tidyr::unchop("ts")
chopped <- data |> tidyr::unchop("ts")
already <- as_name(index) %in% names(chopped[["ts"]])
if (!already) cli::cli_abort(
"Can't' find the index variable in the data. Please check."
Expand Down Expand Up @@ -125,7 +125,7 @@ as_cubble.ncdf4 <- function(data, key, index, coords, vars,
# extract variables
lat_raw <- extract_longlat(data)$lat
long_raw <- extract_longlat(data)$long
time_raw <- extract_time(data) %>% as.Date()
time_raw <- extract_time(data) |> as.Date()
var <- extract_var(data, vars)
lat_idx <- 1:length(lat_raw)
long_idx <- 1:length(long_raw)
Expand All @@ -139,7 +139,7 @@ as_cubble.ncdf4 <- function(data, key, index, coords, vars,
long_idx <- which(long_raw %in% long_range)
long_raw <- as.vector(long_raw[which(long_raw %in% long_range)])
}
raw_data <- var$var %>% map(~.x[long_idx, lat_idx,])
raw_data <- var$var |> map(~.x[long_idx, lat_idx,])

# define dimension and grid
dim_order <- c(length(long_raw), length(lat_raw) ,
Expand All @@ -161,7 +161,7 @@ as_cubble.ncdf4 <- function(data, key, index, coords, vars,

key <- "id"
all_vars <- find_invariant(data, !!key)
out <- data %>% tidyr::nest(ts = c(!!!all_vars$variant))
out <- data |> tidyr::nest(ts = c(!!!all_vars$variant))

new_spatial_cubble(
out, key = key, index = "time", coords = c("long", "lat")
Expand Down Expand Up @@ -244,12 +244,12 @@ as_cubble.sftime <- function(data, key, index, coords, ...){
coords <- as.list(quo_get_expr(coords))[-1]
coords <- unlist(map(coords, as_string))

all_vars <- data %>% find_invariant(!!key)
spatial <- data %>% select(all_vars$invariant, -!!index) %>% distinct()
all_vars <- data |> find_invariant(!!key)
spatial <- data |> select(all_vars$invariant, -!!index) |> distinct()
temporal <- as_tibble(data) %>%
select(!!key, all_vars$variant, !!index) %>%
nest(ts = all_vars$variant)
out <- spatial %>% left_join(temporal, by = as_name(key))
out <- spatial |> left_join(temporal, by = as_name(key))

new_spatial_cubble(
out, key = as_name(key), index = as_name(index), coords = coords
Expand Down
2 changes: 1 addition & 1 deletion R/cubble-accessors.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' climate_mel[c(1:3, 7)] # a nested cubble
#' make_spatial_sf(climate_mel)[1:3] # an sf
#'
#' long <- climate_mel %>% face_temporal()
#' long <- climate_mel |> face_temporal()
#' long[1:3] # a long cubble
#'
#' climate_mel[1:3] # tibble
Expand Down
2 changes: 1 addition & 1 deletion R/cubble-attrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ spatial <- function(data){
#' @export
spatial.spatial_cubble_df <- function(data){
class(data) <- setdiff(class(data), c("spatial_cubble_df","cubble_df"))
data %>% select(-"ts") %>% remove_attrs()
data |> select(-"ts") |> remove_attrs()
}

#' @rdname attr
Expand Down
10 changes: 5 additions & 5 deletions R/cubble-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ cubble <- function(..., key, index, coords) {
coords <- names(data)[tidyselect::eval_select(coords, data)]

all_vars <- find_invariant(data, !!key)
data <- data %>% tidyr::nest(ts = c(!!index, !!!all_vars$variant))
data <- data |> tidyr::nest(ts = c(!!index, !!!all_vars$variant))

new_spatial_cubble(
data, key = as_name(key), index = as_name(index), coords = coords)
Expand Down Expand Up @@ -162,12 +162,12 @@ make_cubble <- function(spatial, temporal, by = NULL, key, index, coords,
)

# only create when have both spatial & temporal info
spatial <- spatial %>% filter(!by %in% only_spatial)
spatial <- spatial |> filter(!by %in% only_spatial)

if (is_sf(spatial)){
# from discussion: https://github.com/r-spatial/sf/issues/951
# to ensure the sf is built from a tibble
spatial <- spatial %>% as_tibble() %>% sf::st_as_sf()
spatial <- spatial |> as_tibble() |> sf::st_as_sf()
}

if (is_tsibble(temporal)){
Expand All @@ -176,10 +176,10 @@ make_cubble <- function(spatial, temporal, by = NULL, key, index, coords,
index <- as_name(index)
}

temporal <- temporal %>% filter(!by %in% only_temporal) %>%
temporal <- temporal |> filter(!by %in% only_temporal) %>%
select(as_name(index), setdiff(colnames(temporal), as_name(index)))
out <- suppressMessages(
dplyr::inner_join(spatial, temporal %>% nest(ts = -by))
dplyr::inner_join(spatial, temporal |> nest(ts = -by))
)

new_spatial_cubble(
Expand Down
2 changes: 1 addition & 1 deletion R/cubble-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tbl_sum.spatial_cubble_df <- function(x){
x_is_sf <- is_sf(x)
if (!x_is_sf) {
coord_vars <- coords(x)
x <- as_tibble(x) %>% sf::st_as_sf(coords = coord_vars)
x <- as_tibble(x) |> sf::st_as_sf(coords = coord_vars)
}

line2 <- glue::glue("[", paste0(sf::st_bbox(x), collapse = ", "), "]")
Expand Down
4 changes: 2 additions & 2 deletions R/cubble-update.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ update_cubble.spatial_cubble_df <- function(data, key = NULL,
index <- index_var(data)
coords <- coords(data)

data %>% new_spatial_cubble(key = key, index = index, coords = coords)
data |> new_spatial_cubble(key = key, index = index, coords = coords)

}

Expand All @@ -33,6 +33,6 @@ update_cubble.temporal_cubble_df <- function(data, key = NULL,
coords <- coords(data)

spatial <- spatial(data)
data %>% new_temporal_cubble(
data |> new_temporal_cubble(
key = key, index = index, coords = coords, spatial = spatial)
}
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' }
#' @rdname climate-data
#' @examples
#' climate_aus %>% face_temporal() %>% face_spatial()
#' climate_aus |> face_temporal() |> face_spatial()
"climate_aus"

#' @rdname climate-data
Expand Down
92 changes: 46 additions & 46 deletions R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
#' cb_long <- face_temporal(climate_mel)
#'
#' # filter - currently filter.spatial_cubble_df, dply_row_slice
#' cb_nested %>% filter(elev > 40)
#' cb_long %>% filter(prcp > 0)
#' cb_nested |> filter(elev > 40)
#' cb_long |> filter(prcp > 0)
#'
#' # mutate - curerntly mutate.spatial_cubble_df, dply_col_modify
#' cb_nested %>% mutate(elev2 = elev + 10)
#' cb_long %>% mutate(prcp2 = prcp + 10)
#' cb_nested |> mutate(elev2 = elev + 10)
#' cb_long |> mutate(prcp2 = prcp + 10)
#'
#' # arrange - currently arrange.spatial_cubble_df, arrange.temporal_cubble_df
#' cb_nested %>% arrange(wmo_id)
#' cb_long %>% arrange(prcp)
#' cb_nested |> arrange(wmo_id)
#' cb_long |> arrange(prcp)
#'
#' # summarise - summarise.spatial_cubble_df, summarise.temporal_cubble_df
#' cb_long %>%
Expand All @@ -51,57 +51,57 @@
#' summarise(t = mean(tmax), .by = first_5)
#'
#' # select - select.spatial_cubble_df, select.temporal_cubble_df
#' cb_nested %>% select(name)
#' cb_nested %>% select(-id, -name)
#' cb_long %>% select(prcp)
#' cb_long %>% select(-prcp, -date)
#' cb_nested |> select(name)
#' cb_nested |> select(-id, -name)
#' cb_long |> select(prcp)
#' cb_long |> select(-prcp, -date)
#'
#' # rename - rename.spatial_cubble_df, rename.temporal_cubble_df
#' cb_nested %>% rename(elev2 = elev)
#' cb_long %>% rename(prcp2 = prcp)
#' cb_nested |> rename(elev2 = elev)
#' cb_long |> rename(prcp2 = prcp)
#' # rename on key attributes
#' cb_nested %>% rename(id2 = id)
#' cb_long %>% rename(date2 = date)
#' cb_nested |> rename(id2 = id)
#' cb_long |> rename(date2 = date)
#'
#' # join - mutate_join - dplyr_reconstruct()
#' # join - filter_join - dplyr_row_slice()
#' df1 <- cb_nested %>% as_tibble() %>% select(id, name) %>% head(2)
#' nested <- cb_nested %>% select(-name)
#' nested %>% left_join(df1, by = "id")
#' nested %>% right_join(df1, by = "id")
#' nested %>% inner_join(df1, by = "id")
#' nested %>% full_join(df1, by = "id")
#' nested %>% anti_join(df1, by = "id")
#' df1 <- cb_nested |> as_tibble() |> select(id, name) |> head(2)
#' nested <- cb_nested |> select(-name)
#' nested |> left_join(df1, by = "id")
#' nested |> right_join(df1, by = "id")
#' nested |> inner_join(df1, by = "id")
#' nested |> full_join(df1, by = "id")
#' nested |> anti_join(df1, by = "id")
#'
#' # bind_rows - dplyr_reconstruct, bind_rows.temporal_cubble_df
#' df1 <- cb_nested %>% head(1)
#' df2 <- cb_nested %>% tail(2)
#' df1 <- cb_nested |> head(1)
#' df2 <- cb_nested |> tail(2)
#' bind_rows(df1, df2)
#' df1 <- cb_long %>% head(10)
#' df2 <- cb_long %>% tail(20)
#' df1 <- cb_long |> head(10)
#' df2 <- cb_long |> tail(20)
#' bind_rows(df1, df2)
#'
#' # relocate - dplyr_col_select, dplyr_col_select
#' cb_nested %>% relocate(ts, .before = name)
#' cb_nested %>% face_temporal() %>% relocate(tmin)
#' cb_nested |> relocate(ts, .before = name)
#' cb_nested |> face_temporal() |> relocate(tmin)
#'
#' # slice - all the slice_* uses dplyr::slice(), which uses dplyr_row_slice()
#' cb_nested %>% slice_head(n = 2)
#' cb_nested %>% slice_tail(n = 2)
#' cb_nested %>% slice_max(elev)
#' cb_nested %>% slice_min(elev)
#' cb_nested %>% slice_sample(n = 2)
#' cb_nested |> slice_head(n = 2)
#' cb_nested |> slice_tail(n = 2)
#' cb_nested |> slice_max(elev)
#' cb_nested |> slice_min(elev)
#' cb_nested |> slice_sample(n = 2)
#'
#' # rowwise - rowwise.spatial_cubble_df, rowwise.temporal_cuble_df
#' cb_nested %>% rowwise()
#' cb_long %>% rowwise()
#' cb_nested |> rowwise()
#' cb_long |> rowwise()
#'
#' # group_by & ungroup -
#' (res <- cb_nested %>% mutate(group1 = c(1, 1, 2)) %>% group_by(group1))
#' res %>% ungroup()
#' (res2 <- res %>% face_temporal() %>% unfold(group1) %>% group_by(group1))
#' res2 %>% ungroup()
#' res2 %>% mutate(first_5 = ifelse(lubridate::day(date) <= 5, 1, 6)) %>%
#' (res <- cb_nested |> mutate(group1 = c(1, 1, 2)) |> group_by(group1))
#' res |> ungroup()
#' (res2 <- res |> face_temporal() |> unfold(group1) |> group_by(group1))
#' res2 |> ungroup()
#' res2 |> mutate(first_5 = ifelse(lubridate::day(date) <= 5, 1, 6)) %>%
#' group_by(first_5) %>%
#' ungroup(group1)
arrange.temporal_cubble_df <- function(.data, ...){
Expand Down Expand Up @@ -237,14 +237,14 @@ summarise.temporal_cubble_df <- function(.data, ..., .by = key_vars(.data),
class(.data) <- setdiff(class(.data), cb_temporal_cls)
if (inherits(.data, "grouped_df")){
gv <- c(group_vars(.data), .by)
.data <- .data %>% group_by(!!!syms(gv))
.data <- .data |> group_by(!!!syms(gv))
out <- summarise(.data, ..., .groups = .groups)
} else{
out <- summarise(.data, ..., .by = .by, .groups = .groups)
}

if (!index %in% colnames(out) && "groups" %in% names(attributes(out))){
potential_index <- .data %@% groups %>% colnames() %>% utils::head(-1)
potential_index <- .data %@% groups |> colnames() |> utils::head(-1)
index <- setdiff(potential_index, key)
}

Expand Down Expand Up @@ -300,9 +300,9 @@ bind_rows.temporal_cubble_df <- function(..., .id = NULL){

dots <- list2(...)
all_temporal_cubble <- all(map_lgl(dots, is_cubble_temporal))
same_key <- map_chr(dots, key_vars) %>% reduce(identical)
same_index <- map_chr(dots, index_var) %>% reduce(identical)
same_coords <- map_chr(dots, coords) %>% reduce(identical)
same_key <- map_chr(dots, key_vars) |> reduce(identical)
same_index <- map_chr(dots, index_var) |> reduce(identical)
same_coords <- map_chr(dots, coords) |> reduce(identical)
if (!all_temporal_cubble)
cli::cli_abort("All the objects needs to be temporal cubbles to bind.")

Expand All @@ -315,7 +315,7 @@ bind_rows.temporal_cubble_df <- function(..., .id = NULL){

class(.data) <- setdiff(class(.data), cb_temporal_cls)
res <- NextMethod()
spatial <- map(dots, spatial) %>% reduce(bind_rows)
spatial <- map(dots, spatial) |> reduce(bind_rows)
new_temporal_cubble(
res, key = same_key, index = same_index, coords = same_coords,
spatial = spatial)
Expand Down Expand Up @@ -394,7 +394,7 @@ dplyr_reconstruct.temporal_cubble_df <- function(data, template) {
key_var <- key_vars(template)
key_vals <- unique(data[[key_var]])
index_var <- index_var(template)
spatial <- spatial(template) %>% filter(!!sym(key_var) %in% key_vals)
spatial <- spatial(template) |> filter(!!sym(key_var) %in% key_vals)

if (is_tsibble(template)){
suppressWarnings(
Expand Down
10 changes: 5 additions & 5 deletions R/face-spatial-temporal.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#' @rdname face
#' @export
#' @examples
#' cb_long <- climate_mel %>% face_temporal()
#' cb_back <- cb_long %>% face_spatial()
#' cb_long <- climate_mel |> face_temporal()
#' cb_back <- cb_long |> face_spatial()
#' identical(climate_mel, cb_back)
face_temporal <- function(data, col) {
UseMethod("face_temporal")
Expand Down Expand Up @@ -56,7 +56,7 @@ face_temporal.spatial_cubble_df <- function(data, col){
class(data) <- class(data)[class(data) != "cubble_df"]

if (is_tsibble){
out <- out %>% tsibble::as_tsibble(key = !!cur_key, index = index)
out <- out |> tsibble::as_tsibble(key = !!cur_key, index = index)
tsibble_attr <- attributes(out)
index <- out %@% "index"
}
Expand Down Expand Up @@ -97,8 +97,8 @@ face_spatial.temporal_cubble_df <- function(data) {
setdiff(key_name)

class(data) <- setdiff(class(data), cb_temporal_cls)
temporal <- data %>% remove_attrs() %>% tidyr::nest(ts = -key_name)
out <- spatial %>% dplyr::left_join(temporal, by = key_name)
temporal <- data |> remove_attrs() |> tidyr::nest(ts = -key_name)
out <- spatial |> dplyr::left_join(temporal, by = key_name)

new_spatial_cubble(
out, key = key_name, index = index, coords = coords
Expand Down
Loading

0 comments on commit 54b2f19

Please sign in to comment.