2

I am interested in the ability to pass a string not as an argument within a function but as an entire function. This may not be the smartest approach but I am simply curious so that I can understand the functionality between dplyr and how R interprets strings. Perhaps I am missing something very obvious but here are my attempts:

#what i want----
library(dplyr)
mtcars %>% count()

#replicate by passing string as count---
#feed string as a function
my_string = "count()"

#attempt 1
mtcars %>% my_string

#attempt 2
mtcars %>% eval(noquote(my_string))

#neither of the attempts work

If this is not possible I understand, but it would be interesting if possible as I can see some applications for this in my mind.

EDIT

A little more to explain why I want to do this. I have worked with fst files for some time for some very large data and load data into my environment like so, often performing operations on one file at a time and in parallel which is very efficient for my purposes:

#pseudo code---
seq.Date(1,2,by = "days") %>%
pblapply(function(x){
read.fst(list.files(as.character(x), as.data.table = T) %>%

#this portion turn into a string----    
group_by(foo) %>%
        count()
#------------------------------
        }) %>% rbindlist()


#application-------
my_string = "group_by(foo) %>%
            count()"

  seq.Date(1,2,by = "days") %>%
    pblapply(function(x){
    read.fst(list.files(as.character(x), as.data.table = T) %>% my_string
}) %>% rbindlist()

I use data table more often but I think dplyr might be better for this specific task I am interested with. What I want to be able to do is separately write out the entire pipeline as a string and then pass it. This will allow me to write out a library package to shorten my workflow. Something to that effect.

LoF10
  • 1,907
  • 1
  • 23
  • 64
  • What you're looking to do is more for the `magrittr` package than `dplyr`, as the functionality you think you need is in `%>%` (which is just re-exported by `dplyr`). I don't know that what you want to do will be easy in any stretch, so perhaps you can explain more about your original problem that makes you think this is a necessary solution. – r2evans Apr 09 '19 at 21:22
  • added an edit with some more explanation, thanks for explaining some of this already. – LoF10 Apr 09 '19 at 21:32
  • Why don't you just write a function? `my_func <- function(x) group_by(x, foo) %>% count()` is a little hasty but might work well enough for you. – r2evans Apr 09 '19 at 21:47
  • That's the backup plan lol but let's see if anyone has any other ideas. – LoF10 Apr 09 '19 at 22:10
  • It doesn't impact me, but how do you think an unparsed character string could out-perform any function or stored expression? By just about every measure I can consider (speed, flexibility, correctness), a string is problematic. ***Perhaps*** you could look into [`drake`](https://cran.r-project.org/web/packages/drake/index.html). No promises. – r2evans Apr 09 '19 at 22:16
  • That's fair, I was willing to sacrifice performance for customizability in this case but you have a good point. Thanks for the tip. – LoF10 Apr 09 '19 at 23:29

0 Answers0