20

R can read files on a web server using convenient syntax such as

data <- read.delim("http://remoteserver.com/file.dat")

I wonder if there is a way to do something similar with a file on an ssh server with passwordless-ssh already in place?

hatmatrix
  • 42,883
  • 45
  • 137
  • 231

1 Answers1

40

You can read a file using pipes like this:

d = read.table( pipe( 'cat data.txt' ), header = T )

If you wanted to read from an SSH connection, try this:

d = read.table( pipe( 'ssh hostname "cat data.txt"' ), header = T )

There's also no reason to confine this to just ssh commands, you could also do something like this:

d = read.table( pipe( 'cat *.txt' ) )

See the R Data Import/Export page for more information, specifically the Connections section.

James Thompson
  • 46,512
  • 18
  • 65
  • 82
  • 1
    @james Thompson, do you need a package for ssh connection in R. I need to do same, connect to remote server with ssh and read a file. Thanks in advance. – user1471980 Jun 27 '13 at 12:23
  • I tried using Windows R to connect a Linux server and failed, no problem between linux servers, any idea? – Negrito Jan 07 '21 at 04:04