I’m trying to create dynamic SQL pulls in R. I’m using dplyr to create the pulls. However, I haven’t found a way to properly convert a string to expression with dplyr.
For example, the following code will work.
data("CO2")
CO2_mod <- CO2 %>%
mutate(new_c = trimws(paste0(Plant, Type)))
However, when I try any of these below none of them will work. Does anyone know how to convert this string to something I can use in a dplyr pull.
command <- 'mutate(new_c = trimws(paste0(Plant, Type)))'
expr <- parse_expr(command)
expr <- str2lang(command)
expr <- str2expression(command)
CO2_mod <- CO2 %>%
expr #Tried this too for all combinations as well eval(expr)
Define your own piping operator:
"%>e%" <- function(lhs, rhs) {
do.call("%>%", list(lhs, str2lang(rhs)))
}
command <- 'mutate(new_c = trimws(paste0(Plant, Type)))'
CO2 %>e% command %>% head
giving
Plant Type Treatment conc uptake new_c
1 Qn1 Quebec nonchilled 95 16.0 Qn1Quebec
2 Qn1 Quebec nonchilled 175 30.4 Qn1Quebec
3 Qn1 Quebec nonchilled 250 34.8 Qn1Quebec
4 Qn1 Quebec nonchilled 350 37.2 Qn1Quebec
5 Qn1 Quebec nonchilled 500 35.3 Qn1Quebec
6 Qn1 Quebec nonchilled 675 39.2 Qn1Quebec
this seems to be what you are looking for:
library(dplyr)
df <- data.frame(
Plant = c("Rose", "Tulip", "Daisy"),
Type = c("Flower", "Flower", "Flower")
)
command <- 'df <- mutate(df, new_c = trimws(paste0(Plant, Type)))'
expr <- parse(text = command)
eval(expr)
print(df)
Plant Type new_c
1 Rose Flower RoseFlower
2 Tulip Flower TulipFlower
3 Daisy Flower DaisyFlower
The steps are to include the data frame in the string, then parse the string as an expression using the parse()
function and then evaluate the expression using the eval()
function.
When the data frame is printed to the console, you will find that it the mutate operation has been performed on it. I hope this helps!
0
If the object returned by str2lang
or by str2expression
are of class "call"
or "expression"
you need to eval
uate it.
library(tidyverse)
data(CO2)
command <- 'mutate(CO2, new_c = trimws(paste0(Plant, Type)))'
expr <- str2lang(command)
class(expr)
#> [1] "call"
CO2_mod1 <- eval(expr)
head(CO2_mod1)
#> Plant Type Treatment conc uptake new_c
#> 1 Qn1 Quebec nonchilled 95 16.0 Qn1Quebec
#> 2 Qn1 Quebec nonchilled 175 30.4 Qn1Quebec
#> 3 Qn1 Quebec nonchilled 250 34.8 Qn1Quebec
#> 4 Qn1 Quebec nonchilled 350 37.2 Qn1Quebec
#> 5 Qn1 Quebec nonchilled 500 35.3 Qn1Quebec
#> 6 Qn1 Quebec nonchilled 675 39.2 Qn1Quebec
rlang::eval_tidy(expr) |> head()
#> Plant Type Treatment conc uptake new_c
#> 1 Qn1 Quebec nonchilled 95 16.0 Qn1Quebec
#> 2 Qn1 Quebec nonchilled 175 30.4 Qn1Quebec
#> 3 Qn1 Quebec nonchilled 250 34.8 Qn1Quebec
#> 4 Qn1 Quebec nonchilled 350 37.2 Qn1Quebec
#> 5 Qn1 Quebec nonchilled 500 35.3 Qn1Quebec
#> 6 Qn1 Quebec nonchilled 675 39.2 Qn1Quebec
Created on 2024-08-29 with reprex v2.1.0
And an object of class "expression"
.
library(tidyverse)
command <- 'mutate(CO2, new_c = trimws(paste0(Plant, Type)))'
expr <- str2expression(command)
class(expr)
#> [1] "expression"
CO2_mod2 <- eval(expr)
head(CO2_mod2)
#> Plant Type Treatment conc uptake new_c
#> 1 Qn1 Quebec nonchilled 95 16.0 Qn1Quebec
#> 2 Qn1 Quebec nonchilled 175 30.4 Qn1Quebec
#> 3 Qn1 Quebec nonchilled 250 34.8 Qn1Quebec
#> 4 Qn1 Quebec nonchilled 350 37.2 Qn1Quebec
#> 5 Qn1 Quebec nonchilled 500 35.3 Qn1Quebec
#> 6 Qn1 Quebec nonchilled 675 39.2 Qn1Quebec
Created on 2024-08-29 with reprex v2.1.0