title: SSH within R script to get to MySQL Database
body: <p>I am trying to connect to a MySQL server, which is restricted by being connected to a given server. I am trying to connect through this restricting server while not physically connected.</p>
  
  <p>Through the command line this is doable by creating a SSH connection, after which I can run MySQL commands from the command line. For example:</p>
  
  <pre><code>ssh myUsername@Hostname
  myUsername@Hostname's password:
  [myUsername@Host ~]$ mysql -h mySQLHost -u mySQLUsername -p mySQLPassword
  </code></pre>
  
  <p>However, I wish to connect to the MySQL database from within R, so I can send queries to read in tables into my current R session. Usually I would run a R session inside of the commandline, but the server does not have R installed on it.</p>
  
  <p>For example, I have this snippet of code that work when I am physically connected to the server (filled in information changed):</p>
  
  <pre><code>myDB &lt;- dbConnect(MySQL(), user="mySQLUsername", password="mySQLPassword", dbname="myDbname", host="mySQLHost")
  </code></pre>
  
  <p>In essence, I want to run this same command through a pipe, so that the myDB object is a working mySQL connection.</p>
  
  <p>I have been trying to pipe my way into the restricting server from within R, and have been able to read in a csv file. For example:</p>
  
  <pre><code>dat &lt;- read.table(pipe('ssh myUsername@Hostname "cat /path/to/your/file"'))
  </code></pre>
  
  <p>This prompts me for my password, and the table is read (as is suggested it would <a href="https://stackoverflow.com/questions/17347450/how-do-you-connect-to-a-remote-server-with-ssh-in-r">here</a>). However, I am unsure how to translate this to a MySQL connection. For example, should I make the pipe part of the <code>host</code> argument? That was my first thought, but have been unable to make that work.</p>
  
  <p>Any help would be appreciated.</p>