2

How can I modify my code so that I can pass a list of variables to select within tq_mutate when called from within a function?

The following code works when called outside a function, but not when called inside a function:

(Edited to add reproducible example):

library(tidyquant)

# WORKS
x <- data_frame(date=seq(as.Date('2018-01-01'),as.Date('2018-01-10'),by=1), a=seq(1,10,by=1), b=seq(11,20,by=1))
k=c(1,2)

lag_cols <- c('a', 'b')

temp_names <- crossing(lag_cols, k)
temp_names <- temp_names %>% arrange(k, lag_cols)
col_names <- paste0(temp_names$lag_cols, '_lag', temp_names$k)

result <- x %>% tq_mutate(
    select = c('a', 'b'),
    mutate_fun = lag.xts,
    k = k,
    col_rename=col_names)

result

# DOESN'T WORK
lag_data <- function(df, k) {
  lag_cols_in <- c('a', 'b')

  temp_names <- crossing(lag_cols_in, k)
  temp_names <- temp_names %>% arrange(k, lag_cols_in)
  col_names <- paste0(temp_names$lag_cols_in, '_lag', temp_names$k)

  df %>% tq_mutate(
      select = lag_cols_in,
      mutate_fun = lag.xts,
      k = k,
      col_rename=col_names)
}

My reading indicates that tq_mutate uses select_ when called inside a function, and so I need to pass the .dots=c('a','b','c') call to select.

How can I modify my code so that I can pass a list of variables? Note that putting select=c('a', 'b') inside the tq_mutate call works in both cases, so I'm guessing this has something to do with environments and variable scoping.

Here's the error message:

Error in .f(.x[[i]], ...) : object 'lag_cols_in' not found
23.
.f(.x[[i]], ...)
22.
map(.x[sel], .f, ...)
21.
map_if(quos, !is_helper, eval_tidy, mask)
20.
vars_select_eval(.vars, quos)
19.
tidyselect::vars_select(names(.data), !!!quos(...))
18.
select.data.frame(.data, !!!dots)
17.
select(.data, !!!dots)
16.
select_.data.frame(data, select)
15.
dplyr::select_(data, select)
14.
tq_transmute_.tbl_df(data = data, select = select, mutate_fun = mutate_fun, col_rename = col_rename, ... = ...)
13.
tq_transmute_(data = data, select = select, mutate_fun = mutate_fun, col_rename = col_rename, ... = ...)
12.
tq_mutate_.tbl_df(data = data, select = lazyeval::expr_text(select), mutate_fun = lazyeval::expr_text(mutate_fun), col_rename = col_rename, ... = ...)
11.
tq_mutate_(data = data, select = lazyeval::expr_text(select), mutate_fun = lazyeval::expr_text(mutate_fun), col_rename = col_rename, ... = ...)
10.
tq_mutate(., select = lag_cols_in, mutate_fun = lag.xts, k = k, col_rename = col_names)
9.
function_list[[k]](value)
8.
withVisible(function_list[[k]](value))
7.
freduce(value, `_function_list`)
6.
`_fseq`(`_lhs`)
5.
eval(quote(`_fseq`(`_lhs`)), env, env)
4.
eval(quote(`_fseq`(`_lhs`)), env, env)
3.
withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
2.
df %>% tq_mutate(select = lag_cols_in, mutate_fun = lag.xts, k = k, col_rename = col_names)
1.
lag_data(x, k)
Jesse E.
  • 261
  • 1
  • 4
  • Please show a small reproducible example – akrun Dec 19 '18 at 20:44
  • 1
    Use [edit] facilities to address comment. (Do NOT use comments.) And do add `library` calls to load any needed packages. – IRTFM Dec 19 '18 at 20:46
  • I believe the answer might be in using `tq_mutate_` instead of `tq_mutate`, but when using the SE version it seems like it ignores multiple columns. I'm not sure if I should file an issue in GitHub or not. – Jesse E. Dec 20 '18 at 19:10

1 Answers1

0

There is probably a better way but this appears to work...

library(tidyquant)
x <- data_frame(date=seq(as.Date('2018-01-01'),as.Date('2018-01-10'),by=1), a=seq(1,10,by=1), b=seq(11,20,by=1))
k <- c(1,2)
lag_data <- function(df, k) {
  lag_cols_in <- c('a', 'b')
  temp_names <- crossing(lag_cols_in, k)
  temp_names <- temp_names %>% arrange(k, lag_cols_in)
  col_names <- paste0(temp_names$lag_cols_in, '_lag', temp_names$k)

  df %>% tq_mutate(
      select = lag_cols_in[1:length(lag_cols_in)],
      mutate_fun = lag.xts,
      k = k,
      col_rename = col_names)
}

lag_data(x,k)
GeeGee
  • 58
  • 9
  • Are you certain? I tried and received the same error as earlier (lag_cols_in not found). Is it possible you have lag_cols_in defined in the global scope? If you have lag_cols_in defined globally the function will work, but not if it's only defined within the function. – Jesse E. Dec 20 '18 at 00:25
  • Ah sorry, you are correct. I had used <<- at one point when testing – GeeGee Dec 20 '18 at 00:47