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

Allowing tq_transmute to programmatically set aggregation functions #198

Open
kaybenleroll opened this issue Apr 26, 2021 · 0 comments
Open

Comments

@kaybenleroll
Copy link

I am trying to create a function that takes a list of aggregation functions and applies them in sequence.

Here is a short reprex:

library(tidyquant)
library(glue)


calculate_period_volume <- function(label, data) {
  apply_func <- glue("apply.{labe}") %>% get()
  
  agg_tbl <- data %>%
    tq_transmute(
      mutate_fun = apply_func,
      FUN        = sum,
      na.rm      = TRUE,
      col_rename = "amount"
      )

  return(agg_tbl)
}

agg_vol_tbl <- c("daily", "weekly", "monthly") %>%
  enframe(name = NULL, value = "period") %>%
  mutate(
    data = map(label, calculate_period_volume, data = FANG)
  )

The error message for this is

Error: Problem with `mutate()` input `data`.
x fun = apply_func not a valid option.
i Input `data` is `map(label, calculate_period_volume, data = FANG)`.

I think the issue is that tq_transmute/tq_mutate is expecting the name of the function, but would it be possible to change this so that if mutate_fun is a string it just uses the function named in the string? I guess for validity you would also need to ensure that the value of the string is on the approved list if you want to preserve that feature?

Happy to help out in fixing this if I can as I think it will be useful.

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

No branches or pull requests

1 participant