I am trying to use the aggregate_key
function in the fable
package to create a hierarchical time series in a shiny flexdashboard. The below code works fine as long as I can hard code in the column name 'value'.
library(tidyverse)
library(tsibble)
library(fable)
library(fpp2)
agg_key <- cbind.data.frame(visnights, year=1900:1975) %>%
pivot_longer(NSWMetro:OTHNoMet, names_to=c("State", "Region"), names_sep=c(3)) %>%
as_tsibble(index=year, key=c(State, Region)) %>%
aggregate_key(State / Region, value=sum(value))
The problem comes because I'm using a flexdashboard input to get the column name so it comes in as the character string "value". I've tried to following to no avail.
#only repeating the last line in the pipe for brevity
aggregate_key(State / Region, value=sum(!!"value"))
aggregate_key(State / Region, value=sum(!!!"value"))
aggregate_key(State / Region, value=sum(as.name("value")))
aggregate_key(State / Region, value=sum(as_label("value")))
Please help me figure out how to pass a character string to this function.
The
aggregate_key()
function hassummarise()
semantics, so anything that works with non-standard evaluation (NSE) andsummarise()
should also work here.To convert a string to a symbol (the name of the column), you can use
as.name("value")
,as.symbol("value")
, orrlang::sym("value")
. Your attempts above are very close, and you have all the necessary ingredients for making it work.A working solution for this is using
value = sum(!!as.name("value"))
:Created on 2020-09-23 by the reprex package (v0.3.0)
As you mention above, writing this code interactively you would use
value = sum(value)
. To do this with a string programmatically, you need to convert"value"
tovalue
usingrlang::sym("value")
(or alternatives above), and then!!
is used to tellaggregate_key()
to use the column of datavalue
rather than the literal symbolvalue
.Some further tips on programming with dplyr (and consequently,
aggregate_key()
) can be found here: https://dplyr.tidyverse.org/articles/programming.html