windowsrshellcsvpipe

Read columns of a csv file using shell or pipe inside R - Windows


I'm looking for a way of reading only a few columns from a csv file into R using shell() or pipe. I found this thread that explains how to accomplish that on Linux: Quicker way to read single column of CSV file

On Linux this works adding the what argument:

a <-as.data.frame(scan(pipe("cut -f1,2 -d, Main.csv"),
                       what=list("character","character"),sep= ","))

However this doesn't seem to work on Windows. When using pipe("cut -f1 -d, Main.csv") the connection gets opened but it doesn't return anything.

What would be the functions/syntax I need to use in order to make this work on Windows.

Is it possible to accomplish this by using shell()?


Solution

  • Make sure that cut is on your path - its in Rtools. This works for me:

    # check that cut is availble
    Sys.which("cut")
    
    # create test data
    Lines <- "a,b,c
    1,2,3
    4,5,6"
    cat(Lines, file = "in.csv")
    
    # read it
    DF <- read.csv(pipe("cut -f1,2 -d, in.csv"))
    

    Added

    Rtools is now Rtools40 and cut is at C:\Rtools40\usr\bin\cut.exe .