Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing character lat/long columns in hypertibble() #128 #129

Merged
merged 5 commits into from
Oct 15, 2024

Conversation

pvanlaake
Copy link
Contributor

This PR fixes issue #128 where hyper_tibble() would produce character columns for latitude and longitude axes.

Description

In the previous modification of the method, use was made of dimnames(), which are indeed character labels. This has now been reverted to the original use of information from the transforms attribute of the tidync object.

Code

This is the new code for hyper_tibble() (with added <<< here):

hyper_tibble.tidync<- function(x, ..., na.rm = TRUE, force = FALSE) {
  
  slabs <- hyper_array(x, ...,  force = force)
  if (na.rm) all_na <- Reduce(`&`, lapply(slabs, 
                                          function(a) is.na(as.vector(a))))
  total_prod <- prod(dim(slabs[[1]]))
  out <- tibble::as_tibble(lapply(slabs, as.vector))
  
  prod_dims <- 1
  trans <- attr(slabs, "transforms")                <<< Access the transforms again

  for (i in seq_along(trans)) {
    nm <- names(trans)[i]
    nr <- sum(trans[[i]]$selected)
    
    out[[nm]] <- if ("timestamp" %in% colnames(trans[[i]]))   <<< Specific behaviour for CFtime timestamps
      rep(dplyr::filter(trans[[nm]], .data$selected)[["timestamp"]],
          each = prod_dims, length.out = total_prod)
    else
      rep(dplyr::filter(trans[[nm]], .data$selected)[[nm]],   <<< Original code, for other axes
          each = prod_dims, length.out = total_prod)

    prod_dims <- prod_dims * nr
  }
  if (na.rm) out <- dplyr::filter(out, !all_na)
 out
}

Resulting in:

> hyper_tibble(nc)
# A tibble: 9,034,094 × 4
   tempanomaly   lon   lat time      
         <dbl> <dbl> <dbl> <chr>     
 1       0.180  -179   -47 1880-01-15
 2       0.180  -177   -47 1880-01-15
 3       0.180  -175   -47 1880-01-15
 4      -0.110   171   -47 1880-01-15
 5      -0.110   173   -47 1880-01-15
 6      -0.110   175   -47 1880-01-15
 7       0.180  -179   -45 1880-01-15
...

@mdsumner mdsumner merged commit 66ee83c into ropensci:main Oct 15, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants