ropensci / tidync

NetCDF exploration and data extraction
https://docs.ropensci.org/tidync
90 stars 12 forks source link

Hyper_filter() will accept string at second occurrence of dimension value but not the first? #122

Open zlehmann opened 1 year ago

zlehmann commented 1 year ago

I'm really confused hoping I'm doing something wrong on my end. I'm trying to use tidync in some of my own functions to help some co-workers working with some netCDF files. One of my functions basically just wraps up the below (ocean_time is the name of the dimension I'm trying to reference with str_dim): t <- tidync(nc_path) %>% hyper_filter(str_dim = !! sym(str_dim) == 1800) %>% hyper_tibble()

now if i type this out with literals in the console like below it works: t <- tidync(nc_path) %>% hyper_filter("ocean_time" = !! sym(str_dim) == 1800) %>% hyper_tibble()

but if I wrap this into a function and pass a string (str_dim = "ocean_time") as a parameter to it, I get this warning and the filter doesn't happen: Warning message: In hyper_filter.tidync(., str_dim = !!sym(str_dim) == 1800) : 'str_dim' not found in active grid, ignoring

I have tried adding the !! sym(str_dim) before the = too but then i get this error: Error: unexpected '=' in "t <- tnc %>% hyper_filter(!! sym(str_dim) ="

mdsumner commented 1 year ago

I see, i don't actually know how that works can you craft an example with bare dataframe and I'll try to see if it could work

zlehmann commented 1 year ago

Here is a reproducible example with some various testing I've done.

library(tidync)
library(rlang)

# Read in stock data file
ice_file <- system.file("extdata", "ifremer", "20171002.nc", package = "tidync", mustWork = TRUE)

# Store the name of a dimension as a string variable
str_var <- "nj"

# Attempt to hyper_filter using that string
#---------------------------------------------------
# Hard coded name values work
t <- tidync(ice_file) %>%
  hyper_filter(nj = nj >= 500) %>%
  hyper_tibble()

# Hard coded string values work
t <- tidync(ice_file) %>%
  hyper_filter("nj" = "nj" >= 500) %>%
  hyper_tibble()

# Mixed hard coded values like this work
t <- tidync(ice_file) %>%
  hyper_filter("nj" = str_var >= 500) %>%
  hyper_tibble()

# Mixed hard coded values like this does not work - 'str_var' not found in active grid, ignoring
t <- tidync(ice_file) %>%
  hyper_filter(str_var = "nj" >= 500) %>%
  hyper_tibble()

# This does not work - no applicable method for 'hyper_tibble' applied to an object of class "NULL"
t <- tidync(ice_file) %>%
  hyper_filter(!! sym(str_var) = str_var >= 500) %>%
  hyper_tibble()

# This returns a warning but doesn't perform the filter - 'str_var' not found in active grid, ignoring
t <- tidync(ice_file) %>%
  hyper_filter(str_var = !! sym(str_var) >= 500) %>%
  hyper_tibble()

# This returns a warning but doesn't perform the filter - 'str_var' not found in active grid, ignoring
t <- tidync(ice_file) %>%
  hyper_filter(str_var = str_var >= 500) %>%
  hyper_tibble()
mdsumner commented 1 year ago

apologies, I meant with tools that do it now.

library(dplyr)
d <- tibble(a = letters[1:10], nj = 1:10)
# Store the name of a dimension as a string variable
str_dim <- "nj"

d |> filter(str_dim = !! sym(str_dim) == 2)

that's not going to work, how does the idiom you're trying to use work in normal data frames?

zlehmann commented 1 year ago

that doesn't work for filter, but the hyper_filter needs to be framed like in the mutate function right?


# data frame version I would do this
library(dplyr)
d <- tibble(a = letters[1:10], nj = 1:10)
# Store the name of a dimension as a string variable
str_dim <- "nj"

d |> filter(str_dim == 2)

# but in the hyper_filter you need to change it to what you had posted earlier right?
mdsumner commented 1 year ago

oh I see, yes my question didn't make sense

the fact is I don't understand the tidy eval stuff enough to navigate this - the whole rhs and lhs thing here probably needs a rework and maybe can use filter without the lhs at all but I don't know