In R, use fread in a Shh connection with pipe

Asked

Viewed 67 times

2

I have a virtual machine with many TXT files. I usually use an ssh and pipe connection to read the files. When I use the read.csv function there is no problem at the time of reading the files. Already when I use the fread function, the business does not work. I show you the case that works first!

 # my directory with the plink intallation (plink.exe)
 dir<- "C/Tools/Terminal/"
 setwd(dir)

 user <- 'XXXXXX'
 password <- 'XXXX'
 dir_file_machine <- '/media/projects/'
 name_file <- 'XXXXXX.TXT'
 capture_file <- pipe(paste0('plink -ssh ', 
                              user, 
                              '@123.45.678.901 -P 12 -pw ', #fictice 
                              password, 
                              ' "cat ', 
                              dir_file_machine, 
                              name_file))

 a <- read.csv(capture_file, sep = ";")

Now when I use the fread function the following error happens...

 capture_file <- pipe(paste0('plink -ssh ', 
                              user, 
                              '@123.45.678.901 -P 12 -pw ', #fictional 
                              password, 
                              ' "cat ', 
                              dir_file_machine, 
                              name_file))

 a <- fread(capture_file, sep = ";")
 Error in fread(capture_file) : 
'input' must be a single character string containing a file name, a command, full path to a file, a URL starting 'http[s]://', 'ftp[s]://' or 'file://', or the input data itself

Any idea?

  • 1

    The function read.csv accepts connections as one of the inputs. Already the fread, no... I don’t think there’s an easy way to fix this.

  • 2

    According to the comment here and the documentation, you can pass the direct command to the fread, without the pipe. I have no way to test here, but in principle it should work by removing the pipe only.

  • @Interesting Molx!! I withdraw my comment :P

  • 1

    Yes, it works. Thank you

  • 2

    @Molx would be nice if you put your comment as an answer, don’t you think?

  • @Danielfalbel In fact, I did not notice that it confirmed the functioning. Thank you.

Show 1 more comment

1 answer

2


In reality to do this just remove the pipe, for the very fread already does the Piping of command:

capture_file <- paste0('plink -ssh ', 
                       user, 
                       '@123.45.678.901 -P 12 -pw ', #fictional 
                       password, 
                       ' "cat ', 
                       dir_file_machine, 
                       name_file)
a <- fread(capture_file, sep = ";")

Note that this is on documentation of fread:

input

Either the file name to read (containing no n Character), a shell command that preprocesses the file (e.g. fread("grep blah filename") or the input itself as a string (containing at least one n), see examples. I

Source.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.