On Sat, Dec 5, 2020 at 1:19 PM <luke-tierney at uiowa.edu> wrote:> Let's get some experienceHere 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)))))
iuke-tier@ey m@iii@g oii uiow@@edu
2020-Dec-07 15:11 UTC
[Rd] [External] Re: New pipe operator
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
On Mon, Dec 7, 2020 at 9:09 AM Gabor Grothendieck <ggrothendieck at gmail.com> 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. SeemsIt occurred to me it would also be interesting to show this example rewritten using John Mount's bizarro pipe (which is clever use of syntax to get the effect of a pipe) with the new \(x) ... This can be done entirely in base R 4.1. It does not use \>, just \(x)... "myfile.csv" ->.; readLines(.) ->.; gsub(r'{(c\(.*?\)|integer\(0\))}', r'{"\1"}', .) ->.; read.csv(text = .) ->.; replace(., 2:3, lapply(.[2:3], \(col) lapply(col, \(x) eval(parse(text = x))))) ->.; . -> DF