From ggrothend|eck @end|ng |rom gm@||@com  Mon Dec  7 15:09:41 2020
From: ggrothend|eck @end|ng |rom gm@||@com (Gabor Grothendieck)
Date: Mon, 7 Dec 2020 09:09:41 -0500
Subject: [Rd] [External] Re: New pipe operator
In-Reply-To: <alpine.DEB.2.21.2012051203250.3003@luke-Latitude-7480>
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>
Message-ID: <CAP01uRmd0tRM4xYnegK=YijJv6htqq+fuOwBMBns--0+2a7Opw@mail.gmail.com>

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)))))



[Rd] [External] Re: New pipe operator
Gabor Grothendieck