Programming with Tplyr and converting strings to unquoted strings #133
Unanswered
dereksonderegger
asked this question in
Q&A
Replies: 1 comment
-
So in your example above, by <- "Species"
iris |>
Tplyr::tplyr_table(treat_var = !!rlang::sym(by)) |>
Tplyr::add_layer(
Tplyr::group_desc(target_var=Sepal.Length)
) |>
Tplyr::build()
# A tibble: 6 × 6
row_label1 var1_setosa var1_versicolor var1_virginica ord_layer_index ord_layer_1
<chr> <chr> <chr> <chr> <int> <int>
1 n " 50" " 50" " 50" 1 1
2 Mean (SD) "5.01 (0.352)" "5.94 (0.516)" "6.59 (0.636)" 1 2
3 Median "5.00" "5.90" "6.50" 1 3
4 Q1, Q3 "4.80, 5.20" "5.60, 6.30" "6.23, 6.90" 1 4
5 Min, Max "4.3, 5.8" "4.9, 7.0" "4.9, 7.9" 1 5
6 Missing " 0" " 0" " 0" 1 6 But considering your example, the context for this is within a function - and this works for me: my_func <- function(by) {
by <- ensym(by)
iris |>
Tplyr::tplyr_table(treat_var = !!by) |>
Tplyr::add_layer(
Tplyr::group_desc(target_var=Sepal.Length)
) |>
Tplyr::build()
}
my_func("Species")
# A tibble: 6 × 6
row_label1 var1_setosa var1_versicolor var1_virginica ord_layer_index ord_layer_1
<chr> <chr> <chr> <chr> <int> <int>
1 n " 50" " 50" " 50" 1 1
2 Mean (SD) "5.01 (0.352)" "5.94 (0.516)" "6.59 (0.636)" 1 2
3 Median "5.00" "5.90" "6.50" 1 3
4 Q1, Q3 "4.80, 5.20" "5.60, 6.30" "6.23, 6.90" 1 4
5 Min, Max "4.3, 5.8" "4.9, 7.0" "4.9, 7.9" 1 5
6 Missing " 0" " 0" " 0" 1 6 That said - note that there's an open issue for some specific ellipsis arguments of Tplyr functions where this approach for unpacking quosures doesn't quite work - see #111. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When writing programs that will ultimately call dplyr functions, there are a number of tricks that can be used to convert a character string to an unquoted symbol. However the tricks I normally use for dplyr aren't working for Tplyr. For example, if we have a character string that defines the treatment variable to split the table by:
All the commented out calls to tplyr_table() result in errors and only calling the the treatment name hard-coded seems to work. StackOverflow and StackExchange don't have much on Tplyr and most everything else regarding tidy verse evaluation suggests that one of the above should have worked. Any suggestions?
Beta Was this translation helpful? Give feedback.
All reactions