[Rd] [External] Re: New pipe operator

iuke-tier@ey m@iii@g oii uiow@@edu iuke-tier@ey m@iii@g oii uiow@@edu
Mon Dec 7 16:11:10 CET 2020


Or, keeping dplyr but with R-devel pipe and function shorthand:

DF <- "myfile.csv" %>%
    readLines() |>
    \(.) gsub(r'{(c\(.*?\)|integer\(0\))}', r'{"\1"}', .) |>
    \(.) read.csv(text = .) |>
    mutate(across(2:3, \(col) lapply(col, \(x) eval(parse(text = x)))))

Using named arguments to redirect to the implicit first does work,
also in magrittr, but for me at least it is the kind of thing I would
probably regret a month later when trying to figure out the code.

Best,

luke

On Mon, 7 Dec 2020, Gabor Grothendieck wrote:

> On Sat, Dec 5, 2020 at 1:19 PM <luke-tierney using uiowa.edu> wrote:
>> Let's get some experience
>
> Here is my last SO post using dplyr rewritten to use R 4.1 devel.  Seems
> not too bad.  Was able to work around the placeholder for gsub by specifying
> the arg names and used \(...)... elsewhere.  This does not address the
> inconsistency discussed though.  I have indented by 2 spaced in case the
> email wraps around.  The objective is to read myfile.csv including columns that
> contain c(...) and integer(0), parsing and evaluating them.
>
>
>  # taken from:
>  # https://stackoverflow.com/questions/65174764/reading-in-a-csv-that-contains-vectors-cx-y-in-r/65175172#65175172
>
>  # create input file for testing
>  Lines <- "\"col1\",\"col2\",\"col3\"\n\"a\",1,integer(0)\n\"c\",c(3,4),5\n\"e\",6,7\n"
>  cat(Lines, file = "myfile.csv")
>
>  #########################################################################
>  # base R 4.1 (devel)
>  DF <- "myfile.csv" |>
>    readLines() |>
>    gsub(pattern = r'{(c\(.*?\)|integer\(0\))}', replacement = r'{"\1"}') |>
>    \(.) read.csv(text = .) |>
>    \(.) replace(., 2:3, lapply(.[2:3], \(col) lapply(col, \(x)
> eval(parse(text = x)))))
>
>  #########################################################################
>  # dplyr/magrittr
>  library(dplyr)
>
>  DF <- "myfile.csv" %>%
>    readLines %>%
>    gsub(r'{(c\(.*?\)|integer\(0\))}', r'{"\1"}', .) %>%
>    { read.csv(text = .) } %>%
>    mutate(across(2:3, ~ lapply(., function(x) eval(parse(text = x)))))
>

-- 
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney using uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



More information about the R-devel mailing list