From iuke-tier@ey m@iii@g oii uiow@@edu  Mon Dec  7 16:11:10 2020
From: iuke-tier@ey m@iii@g oii uiow@@edu (iuke-tier@ey m@iii@g oii uiow@@edu)
Date: Mon, 7 Dec 2020 09:11:10 -0600 (CST)
Subject: [Rd] [External] Re: New pipe operator
In-Reply-To: <CAP01uRmd0tRM4xYnegK=YijJv6htqq+fuOwBMBns--0+2a7Opw@mail.gmail.com>
References: <e55c2093-0bb5-5f9b-5521-0ca8bdf35953@gmail.com>
 <CALyqOb8c1hHOSaMV+fqXTwpeOsP--KJ_t-oeqMYYafVXeNpqZg@mail.gmail.com>
 <7586e034-e287-0970-6480-f82d747b5445@gmail.com>
 <CADfFDC7bT58_+52uvmpHuMMQRNbbmqV3_7t_fc4sgJ_Esw=xag@mail.gmail.com>
 <CAJmOi+PO_8XDC23=rT=pQ76ENL=gx2V-41ax2fHSVs87xURcEA@mail.gmail.com>
 <alpine.DEB.2.21.2012051203250.3003@luke-Latitude-7480>
 <CAP01uRmd0tRM4xYnegK=YijJv6htqq+fuOwBMBns--0+2a7Opw@mail.gmail.com>
Message-ID: <alpine.DEB.2.21.2012070908010.3377@luke-Latitude-7480>

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 at 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 at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



[Rd] [External] Re: New pipe operator
iuke-tier@ey m@iii@g oii uiow@@edu