In theory this should work, as I've read the tidyverse guide on NSE, but it throws me an error as seen in the bottom of this example. Why is this? I understand how to do a simple quasiquotation of an object, but I do not understand how to evaluate a fraction of two quasiquoted objects. Can anyone help with this?
tmp <- structure(list(qa11a = structure(c(1616, 7293, 1528, 1219, 2049, 286),
label = "Total voters removed from Nov. 2008 to Nov. 2010",
class = c("labelled","numeric")),
state_abbv = c("AL", "AL", "AL", "AL", "AL", "AL"),
fipscode = c("0100100000", "0100300000", "0100500000",
"0100700000", "0100900000", "0101100000"),
qa1a = structure(c(34727, 114952, 16450, 12239, 31874, 7650),
label = "Total registered & eligible to vote November 2010",
class = c("labelled", "numeric")),
reg.pct = structure(c(0.0465343968669911, 0.0634438722249287,
0.092887537993921, 0.0995996404935044,
0.0642843697057163, 0.0373856209150327),
label = "Total voters removed from Nov. 2008 to Nov. 2010",
class = c("labelled", "numeric")),
precleared = c(1, 1, 1, 1, 1, 1),
year = c(2010, 2010, 2010, 2010, 2010, 2010),
shelby = c(0, 0, 0, 0, 0, 0),
pres = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)),
.Names = c("qa11a", "state_abbv", "fipscode", "qa1a",
"reg.pct", "precleared", "year", "shelby", "pres"),
row.names = c(NA, -6L),
class = c("tbl_df", "tbl", "data.frame"))
require(tidyverse)
require(rlang)
upper="qa11a"
lower="qa1a"
pct.name="rej.reg"
pct.reg <- quo_name(enquo(pct.name))
upper <- enquo(upper)
lower <- enquo(lower)
h <- tmp %>%
mutate(!! pct.reg := (!!upper)/(!!lower))
#> Error in mutate_impl(.data, dots): Evaluation error: non-numeric argument to binary operator.
We can convert the string to symbol and then evaluate
when we apply
enquo
on a string, it is converting to a quosure with quotesInstead of converting from a string, it could be easier to do
In the OP's code, calling
enquo
i.e. converting to quosure on a string object will result in string quosure and that is not intendedWe can compare it to
and executing it