<! DOCTYPE html>
<!--  Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Persistent RStudio Server • googleComputeEngineR</title>
<!--  jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script><!-- Bootstrap --><link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/cosmo/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha256-U5ZEeKfGNOja007MMD3YBI0A3OSZOQbeG6z2f2Y0hu8=" crossorigin="anonymous"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.1/css/all.min.css" integrity="sha256-nAmazAk6vS34Xqo0BSrTb+abbtFlgsFK7NKSi6o7Y78=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.1/css/v4-shims.min.css" integrity="sha256-6qHlizsOWFskGlwVOKuns+D1nB6ssZrHQrNj1wGplHc=" crossorigin="anonymous">
<!--  clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js" integrity="sha256-FiZwavyI2V6+EXO1U+xzLG3IKldpiTFf3153ea9zikQ=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.9.4/headroom.min.js" integrity="sha256-DJFC1kqIhelURkuza0AvYal5RxMtpzLjFhsnVIeuk+U=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.9.4/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><meta property="og:title" content="Persistent RStudio Server">
<meta property="og:description" content="">
<meta name="twitter:card" content="summary">
<!--  mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![ endif]--><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=UA-47480439-2"></script><script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-47480439-2');
</script>
</head>
<body>
    <div class="container template-article">
      <header><div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">googleComputeEngineR</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.3.0.9000</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="../articles/installation-and-authentication.html">
    <span class="fa fa-wrench"></span>
     
    Setup
  </a>
</li>
<li>
  <a href="../articles/creating-vms.html">
    <span class="fa fa-laptop"></span>
     
    Creating VMs
  </a>
</li>
<li>
  <a href="../articles/docker-ssh-futures.html">
    <span class="fa fa-terminal"></span>
     
    SSH
  </a>
</li>
<li>
  <a href="../articles/docker.html">
    <span class="fa fa-docker"></span>
     
    Docker
  </a>
</li>
<li>
  <a href="../articles/gpu.html">
    <span class="fa fa-memory"></span>
     
    GPUs
  </a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Examples
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
<li>
      <a href="../articles/example-workflows.html">Introduction</a>
    </li>
    <li>
      <a href="../articles/rstudio-team.html">Custom team RStudio Server</a>
    </li>
    <li>
      <a href="../articles/persistent-rstudio.html">Persistent RStudio Server</a>
    </li>
    <li>
      <a href="../articles/single-scheduler.html">RStudio Server + scheduler</a>
    </li>
    <li>
      <a href="../articles/shiny-app.html">Shiny App</a>
    </li>
    <li>
      <a href="../articles/opencpu-api-server.html">OpenCPU R API</a>
    </li>
    <li>
      <a href="../articles/scheduled-rscripts.html">Scheduled R scripts</a>
    </li>
    <li>
      <a href="../articles/massive-parallel.html">Massive parallel computing</a>
    </li>
  </ul>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Help
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
<li>
      <a href="../reference/index.html">Function Reference</a>
    </li>
    <li>
      <a href="../news/index.html">News</a>
    </li>
    <li>
      <a href="../articles/troubleshooting.html">Troubleshooting</a>
    </li>
    <li>
      <a href="https://cloud.google.com/compute/">
        <span class="fa fa-google"></span>
         
        GCE Homepage
      </a>
    </li>
    <li>
      <a href="https://github.com/cloudyr/googleComputeEngineR">
        <span class="fa fa-github"></span>
         
        GitHub
      </a>
    </li>
    <li>
      <a href="https://docs.google.com/forms/d/e/1FAIpQLSerjirmMpB3b7LmBs_Vx_XPIE9IrhpCpPg1jUcpfBcivA3uBw/viewform">
        <span class="fa fa-slack"></span>
         
        Slack channel
      </a>
    </li>
  </ul>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right"></ul>
</div>
<! --/.nav-collapse -->
  </div>
<! --/.container -->
</div>
<! --/.navbar -->

      

      </header><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header toc-ignore">
      <h1>Persistent RStudio Server</h1>
                        <h4 class="author">Mark Edmondson</h4>
            
            <h4 class="date">2019-11-25</h4>
      
      
      <div class="hidden name"><code>persistent-rstudio. Rmd</code></div>

    </div>

    
    
<div id="persistent-rstudio-on-google-compute-engine" class="section level1">
<h1 class="hasAnchor">
<a href="#persistent-rstudio-on-google-compute-engine" class="anchor"></a>Persistent RStudio on Google Compute Engine</h1>
<p>A new Docker image is available which installs tools on top of the default <a href="https://hub.docker.com/r/rocker/tidyverse/"><code>rocker/tidyverse</code></a> to help persist files over Docker containers.  This image is part of the <a href="https://cloudyr.github.io/googleComputeEngineR/articles/docker.html#public-docker-images">public Docker images</a> built on top of <code>googleComputeEngineR</code>.</p>
<p>With this image, there are three ways to save files between Docker sessions:</p>
<ol style="list-style-type: decimal">
<li>Write files to the host VM file system - this is through the -v flag upon Docker startup</li>
<li>Use Git to pull/push from Git repositories.</li>
<li>Use <a href="https://github.com/cloudyr/googleCloudStorageR"><code>googleCloudStorageR</code></a> to save and read R working directories between machines, including your GitHub/SSH configurations.</li>
</ol>
<p>A combination of the above should be used for what best fits your workflow.</p>
<div id="using-base-vm-file-system" class="section level2">
<h2 class="hasAnchor">
<a href="#using-base-vm-file-system" class="anchor"></a>Using base VM file system</h2>
<p>These files will disappear if you delete the VM, so it is recommend if they are important to write them somewhere else as well.</p>
<p>If relying on this, you will probably want to create a larger VM disk than the default 10GBs using the <code>disk_size_gb</code> argument:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1">vm &lt;-<span class="st"> </span><span class="kw"><a href="../reference/gce_vm.html">gce_vm</a></span>(<span class="st">"vm-larger-disk"</span>, </a>
<a class="sourceLine" id="cb1-2" data-line-number="2">             <span class="dt">predefined_type =</span> <span class="st">"n1-standard-1"</span>, </a>
<a class="sourceLine" id="cb1-3" data-line-number="3">             <span class="dt">template =</span> <span class="st">"rstudio"</span>, </a>
<a class="sourceLine" id="cb1-4" data-line-number="4">             <span class="dt">username =</span> <span class="st">"mark"</span>, <span class="dt">password =</span> <span class="st">"blah"</span>,</a>
<a class="sourceLine" id="cb1-5" data-line-number="5">             <span class="dt">disk_size_gb =</span> <span class="dv">100</span>)</a></code></pre></div>
</div>
<div id="github" class="section level2">
<h2 class="hasAnchor">
<a href="#github" class="anchor"></a>GitHub</h2>
<p>Generally git is the best place for code under version control across many computers.  The below details how you can pull code to your Docker container each restart without needing to resupply your GitHub SSH keys.</p>
<p>See also these references:</p>
<ul>
<li><a href="https://www.r-bloggers.com/rstudio-and-github/" class="uri">https://www.r-bloggers.com/rstudio-and-github/</a></li>
<li><a href="http://happygitwithr.com/" class="uri">http://happygitwithr.com/</a></li>
</ul>
<p>The below assumes you have started a VM using the <code>persistent-rstudio</code> image, which includes SSH tools:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1">vm &lt;-<span class="st"> </span><span class="kw"><a href="../reference/gce_vm.html">gce_vm</a></span>(<span class="st">"vm-ssh"</span>, </a>
<a class="sourceLine" id="cb2-2" data-line-number="2">             <span class="dt">predefined_type =</span> <span class="st">"n1-standard-1"</span>, </a>
<a class="sourceLine" id="cb2-3" data-line-number="3">             <span class="dt">template =</span> <span class="st">"rstudio"</span>, </a>
<a class="sourceLine" id="cb2-4" data-line-number="4">             <span class="dt">username =</span> <span class="st">"mark"</span>, <span class="dt">password =</span> <span class="st">"blah"</span>, </a>
<a class="sourceLine" id="cb2-5" data-line-number="5">             <span class="dt">dynamic_image =</span> <span class="st">"gcr.io/gcer-public/persistent-rstudio"</span>)</a></code></pre></div>
<div id="first-time-you-launch-a-vm" class="section level3">
<h3 class="hasAnchor">
<a href="#first-time-you-launch-a-vm" class="anchor"></a>First time you launch a VM:</h3>
<ol style="list-style-type: decimal">
<li>Once the VM is launched, log in to RStudio Server at the IP provided by the script</li>
<li>Go to <code>Tools &gt; Global Options &gt; Git/SVN &gt; Create RSA Key</code>
</li>
<li>Click on “View public key”" then add it to GitHub here: <a href="https://github.com/settings/keys" class="uri">https://github.com/settings/keys</a>
</li>
<li>Open the terminal in RStudio via <code>Tools &gt; Shell...</code>, and configure you GitHub email and username:</li>
</ol>
<pre><code>git config --global user.email "your@githubemail.com"
git config --global user.name "GitHubUserName"</code></pre>
<ol start="4" style="list-style-type: decimal">
<li>Check it works - you should see your GitHub details via <code>cat .gitconfig</code> and SSH keys in <code>ls .ssh</code>, <code>ssh -T git@github.com</code> should succeed.</li>
</ol>
</div>
<div id="a-new-github-project" class="section level3">
<h3 class="hasAnchor">
<a href="#a-new-github-project" class="anchor"></a>A new GitHub project</h3>
<p>Do the below for each new RStudio Project to download from GitHub:</p>
<ol style="list-style-type: decimal">
<li>On GitHub, click the <code>Clone or download</code> green button and copy the <code>Clone with SSH</code> URI.  <strong>Do not copy the browser URL! -  it won’t work</strong>
</li>
<li>Put the URI on RStudio Server via <code>New Project &gt; Version Control &gt; Git &gt; Repository URL</code>
</li>
<li>The first connect you may need to input “yes” in the scary dropdown</li>
<li>Make changes, push to GitHub via the RStudio Git pane</li>
</ol>
</div>
<div id="restarting-the-vmdocker" class="section level3">
<h3 class="hasAnchor">
<a href="#restarting-the-vmdocker" class="anchor"></a>Restarting the VM/Docker</h3>
<p>This configuration should now persist across Docker sessions e.g. you can stop/start the VM and still have GitHub configured.</p>
<ol style="list-style-type: decimal">
<li>Stop the RStudio server via the Web UI or <code><a href="../reference/gce_vm_stop.html">gce_vm_stop()</a></code>
</li>
<li>Restart it via the Web UI or <code><a href="../reference/gce_vm_start.html">gce_vm_start()</a></code>
</li>
<li>Login to RStudio via the URL, then open terminal and check your older configurations are there via <code>cat .gitconfig</code> and SSH keys in <code>ls .ssh</code> and <code>ssh -T git@github.com</code> works</li>
</ol>
</div>
</div>
<div id="using-googlecloudstorager" class="section level2">
<h2 class="hasAnchor">
<a href="#using-googlecloudstorager" class="anchor"></a>Using googleCloudStorageR</h2>
<p>This can be combined with the above GitHub settings to persist the GitHub settings over VMs.</p>
<p>The authentication for the <code>googleCloudStorageR</code> backups is re-using the credentials you used to launch the VM</p>
<p>It is not intended as a replacement for Git - it only adds files if they are not present locally.  I use it to copy projects over to more powerful VMs as required.</p>
<div id="on-local-computer" class="section level3">
<h3 class="hasAnchor">
<a href="#on-local-computer" class="anchor"></a>On local computer</h3>
<ol style="list-style-type: decimal">
<li>Create a Google Cloud Bucket to save your R sessions to - you can do this via the web UI or using <code>googleCloudStorageR</code>s <code>gce_create_bucket()</code> function.</li>
</ol>
<p>Choose a bucket region that is closest to you and your VM for best performance</p>
<ol start="2" style="list-style-type: decimal">
<li>Add that bucket to your <code>.Renviron</code> as the <code>GCS_SESSION_BUCKET</code> argument:</li>
</ol>
<pre><code>GCS_SESSION_BUCKET=gcer-bucket-name</code></pre>
<p>The <code>.Renviron</code> usually sits in your computer home directory, see <code><a href="https://rdrr.io/r/base/Startup.html">?Startup</a></code> for details.</p>
<ol start="3" style="list-style-type: decimal">
<li>Add the <code>gcs_first()</code> and <code>gcs_last()</code> functions to your <code>.RProfile</code> file like so:</li>
</ol>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" data-line-number="1">.First &lt;-<span class="st"> </span><span class="cf">function</span>(){</a>
<a class="sourceLine" id="cb5-2" data-line-number="2"></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">  <span class="kw"><a href="https://rdrr.io/r/base/cat.html">cat</a></span>(<span class="st">"</span><span class="ch">\n</span><span class="st"># Welcome Mark!  Today is "</span>, <span class="kw"><a href="https://rdrr.io/r/base/date.html">date</a></span>(), <span class="st">"</span><span class="ch">\n</span><span class="st">"</span>)</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">  <span class="kw"><a href="https://rdrr.io/r/base/cat.html">cat</a></span>(<span class="st">"</span><span class="ch">\n</span><span class="st"># Loading .Rprofile from"</span>, <span class="kw"><a href="https://rdrr.io/r/base/path.expand.html">path.expand</a></span>(<span class="st">"~"</span>))</a>
<a class="sourceLine" id="cb5-5" data-line-number="5"></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">  googleCloudStorageR<span class="op">::</span><span class="kw"><a href="https://cloudyr.github.io/googleCloudStorageR//reference/gcs_first.html">gcs_first</a></span>()</a>
<a class="sourceLine" id="cb5-7" data-line-number="7">}</a>
<a class="sourceLine" id="cb5-8" data-line-number="8"></a>
<a class="sourceLine" id="cb5-9" data-line-number="9">.Last &lt;-<span class="st"> </span><span class="cf">function</span>(){</a>
<a class="sourceLine" id="cb5-10" data-line-number="10">  <span class="co"># will only upload if a _gcssave.yaml in directory with bucketname</span></a>
<a class="sourceLine" id="cb5-11" data-line-number="11">  googleCloudStorageR<span class="op">::</span><span class="kw"><a href="https://cloudyr.github.io/googleCloudStorageR//reference/gcs_first.html">gcs_last</a></span>()</a>
<a class="sourceLine" id="cb5-12" data-line-number="12">  <span class="kw"><a href="https://rdrr.io/r/base/message.html">message</a></span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Goodbye Mark at "</span>, <span class="kw"><a href="https://rdrr.io/r/base/date.html">date</a></span>(), <span class="st">"</span><span class="ch">\n</span><span class="st">"</span>)</a>
<a class="sourceLine" id="cb5-13" data-line-number="13">}</a>
<a class="sourceLine" id="cb5-14" data-line-number="14"></a></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li>Create RStudio Project</li>
<li>Make R stuff</li>
<li>Add a <code>_gcssave.yaml</code> file specifying the GCS bucket to save to.</li>
</ol>
<p>It can carry various settings shown below:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode yaml"><code class="sourceCode yaml"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="co">## The GCS bucket to save/load R workspace from step 1</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"><span class="fu">bucket:</span><span class="at"> my-bucket</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3"></a>
<a class="sourceLine" id="cb6-4" data-line-number="4"><span class="co">## set to FALSE if you want to load on R session startup</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5"><span class="fu">load_on_startup:</span><span class="at"> FALSE</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6"></a>
<a class="sourceLine" id="cb6-7" data-line-number="7"><span class="co">## on first load, whether to look for a different directory on GCS than present getwd()</span></a>
<a class="sourceLine" id="cb6-8" data-line-number="8"><span class="fu">loaddir:</span></a>
<a class="sourceLine" id="cb6-9" data-line-number="9"></a>
<a class="sourceLine" id="cb6-10" data-line-number="10"><span class="co">## regex to only save these files to GCS</span></a>
<a class="sourceLine" id="cb6-11" data-line-number="11"><span class="fu">pattern:</span></a></code></pre></div>
<ol start="7" style="list-style-type: decimal">
<li>Exit RStudio project.  You should see a message similar to:</li>
</ol>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb7-1" data-line-number="1">Saving data to Google Cloud Storage<span class="op">:</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">your<span class="op">-</span>gcs<span class="op">-</span>bucket</a>
<a class="sourceLine" id="cb7-3" data-line-number="3"><span class="dv">2017-08-18</span> <span class="dv">23</span><span class="op">:</span><span class="dv">25</span><span class="op">:</span><span class="dv">43</span> <span class="op">--</span><span class="st"> </span>File size detected as <span class="fl">1.3</span> Mb</a></code></pre></div>
<p>When you startup that project again you should see:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1">[Workspace loaded from<span class="op">:</span><span class="st"> </span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">gs<span class="op">:</span><span class="er">//</span>your<span class="op">-</span>gcs<span class="op">-</span>bucket<span class="op">/</span>Users<span class="op">/</span>the<span class="op">-</span>rproject<span class="op">-</span>folder]</a></code></pre></div>
<div id="summary" class="section level4">
<h4 class="hasAnchor">
<a href="#summary" class="anchor"></a>Summary</h4>
<p>There are three files to configure:</p>
<ul>
<li>
<code>.Renviron</code> - environment arguments ìncluding <code>GCS_SESSION_BUCKET=gcer-bucket-name</code> that will be looked for as where your session files are</li>
<li>
<code>.Rprofile</code>- general R startup behaviour that carry the <code><a href="https://cloudyr.github.io/googleCloudStorageR//reference/gcs_first.html">googleCloudStorageR::gcs_last()</a></code> and <code><a href="https://cloudyr.github.io/googleCloudStorageR//reference/gcs_first.html">googleCloudStorageR::gcs_first()</a></code> functions</li>
<li>
<code>_gcssave.yaml</code> - per folder settings for what to save that specifies which files to save in which folder</li>
</ul>
</div>
</div>
<div id="on-cloud-rstudio-server" class="section level3">
<h3 class="hasAnchor">
<a href="#on-cloud-rstudio-server" class="anchor"></a>On cloud RStudio server</h3>
<p>Now the R data is saved to GCS under the local folder name.  We can load this data in an RStudio server cloud instance via:</p>
<ol style="list-style-type: decimal">
<li>Launch the RStudio Server image <code>gcr.io/gcer-public/persistent-rstudio</code> that has appropriate libraries loaded.</li>
</ol>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb9-1" data-line-number="1">vm &lt;-<span class="st"> </span><span class="kw"><a href="../reference/gce_vm.html">gce_vm</a></span>(<span class="st">"mark-rstudio"</span>,</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">             <span class="dt">template =</span> <span class="st">"rstudio"</span>,</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">             <span class="dt">username =</span> <span class="st">"mark"</span>, <span class="dt">password =</span> <span class="st">'mypassword'</span>,</a>
<a class="sourceLine" id="cb9-4" data-line-number="4">             <span class="dt">predefined_type =</span> <span class="st">"n1-standard-2"</span>,</a>
<a class="sourceLine" id="cb9-5" data-line-number="5">             <span class="dt">dynamic_image =</span> <span class="st">"gcr.io/gcer-public/persistent-rstudio"</span>)</a></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li>Add a GCS_SESSION_BUCKET metadata, either via webUI or via:</li>
</ol>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="kw"><a href="../reference/gce_set_metadata.html">gce_set_metadata</a></span>(<span class="kw"><a href="https://rdrr.io/r/base/list.html">list</a></span>(<span class="dt">GCS_SESSION_BUCKET =</span> <span class="st">"your-session-bucket"</span>), vm)</a></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li><p>Login to RStudio server and create an RStudio project</p></li>
<li><p>As you did on your local machine, you need to create an .Rprofile so <code>googleCloudStorageR</code> can load and save and load data.  For example:</p></li>
</ol>
<pre><code>.First &lt;- function(){
  cat("\n# Welcome Ignacio!  Today is ", date(), "\n")

  ## will look for download if GCS_SESSION_BUCKET env arg set
  googleCloudStorageR::gcs_first()
}


.Last &lt;- function(){
  # will only upload if a _gcssave.yaml in directory with bucketname
  googleCloudStorageR::gcs_last()
  message("\nGoodbye Ignacio at ", date(), "\n")
}

message("n*** Successfully loaded .Rprofile ***n")</code></pre>
<ol start="5" style="list-style-type: decimal">
<li>Transfer the local RStudio project to this cloud VM by creating a <code>_gcssave.yaml</code> file at the root of the project with these entries:</li>
</ol>
<div class="sourceCode" id="cb12"><pre class="sourceCode yaml"><code class="sourceCode yaml"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="fu">bucket:</span><span class="at"> your-gcs-bucket</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2"><span class="fu">loaddir:</span><span class="at"> your-local-directory-name</span></a></code></pre></div>
<ol start="5" style="list-style-type: decimal">
<li>Close and re-open the RStudio project.  Your local files should now load from GCS</li>
<li>Do work, then exit the project.  It will be saved to a new folder on GCS</li>
</ol>
</div>
<div id="persisting-github-with-googlecloudstorager" class="section level3">
<h3 class="hasAnchor">
<a href="#persisting-github-with-googlecloudstorager" class="anchor"></a>Persisting GitHub with googleCloudStorageR</h3>
<p>You can also use the above in conjunction with the GitHub setup to persist over VMs.</p>
<p>To do so, you need to :</p>
<ol style="list-style-type: decimal">
<li>Keep the same RStudio login username,</li>
<li>Use the same bucket for <code>GCS_SESSION_BUCKET</code> or in the <code>_gcssave.yaml</code>
</li>
<li>Use this Dockerfile’s image - <code>gcr.io/gcer-public/persistent-rstudio</code>
</li>
</ol>
<p>The configurations of GitHub that are saved in <code>.ssh</code> and <code>.gitconfig</code> folders in your home directory will be backed up to Google Cloud Storage.</p>
<div id="saving-github-configurations" class="section level4">
<h4 class="hasAnchor">
<a href="#saving-github-configurations" class="anchor"></a>Saving GitHub configurations</h4>
<ol style="list-style-type: decimal">
<li>Add a <code>_gcssave.yaml</code> file to your home folder that will download/upload the configurations.</li>
</ol>
<div class="sourceCode" id="cb13"><pre class="sourceCode yaml"><code class="sourceCode yaml"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="co">## The GCS bucket to save/load R workspace from</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2"><span class="fu">bucket:</span><span class="at"> gcer-store-my-rstudio-files</span></a>
<a class="sourceLine" id="cb13-3" data-line-number="3"></a>
<a class="sourceLine" id="cb13-4" data-line-number="4"><span class="co">## regex to only save these files to GCS</span></a>
<a class="sourceLine" id="cb13-5" data-line-number="5"><span class="fu">pattern:</span><span class="at"> </span><span class="st">"id_rsa|.gitconfig"</span></a></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li>With no project open and your working directory the base (e.g. <code><a href="https://rdrr.io/r/base/getwd.html">getwd()</a></code> is <code>/home/you</code>) save the yaml file and quit the R session:</li>
</ol>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="kw"><a href="https://rdrr.io/r/base/quit.html">q</a></span>(<span class="dt">save =</span> <span class="st">"no"</span>)</a></code></pre></div>
<p>You should see a message saying its saving the home folder.  Upon restart, that folder will load from the bucket.</p>
</div>
<div id="loading-github-configurations" class="section level4">
<h4 class="hasAnchor">
<a href="#loading-github-configurations" class="anchor"></a>Loading GitHub configurations</h4>
<ol style="list-style-type: decimal">
<li>Start another VM, with the same details as before:</li>
</ol>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb15-1" data-line-number="1">vm2 &lt;-<span class="st"> </span><span class="kw"><a href="../reference/gce_vm.html">gce_vm</a></span>(<span class="st">"mark-rstudio"</span>,</a>
<a class="sourceLine" id="cb15-2" data-line-number="2">             <span class="dt">template =</span> <span class="st">"rstudio"</span>,</a>
<a class="sourceLine" id="cb15-3" data-line-number="3">             <span class="dt">username =</span> <span class="st">"mark"</span>, <span class="dt">password =</span> <span class="st">'mypassword'</span>,</a>
<a class="sourceLine" id="cb15-4" data-line-number="4">             <span class="dt">predefined_type =</span> <span class="st">"n1-standard-2"</span>,</a>
<a class="sourceLine" id="cb15-5" data-line-number="5">             <span class="dt">dynamic_image =</span> <span class="st">"gcr.io/gcer-public/persistent-rstudio"</span>)</a>
<a class="sourceLine" id="cb15-6" data-line-number="6"></a>
<a class="sourceLine" id="cb15-7" data-line-number="7"><span class="kw"><a href="../reference/gce_set_metadata.html">gce_set_metadata</a></span>(<span class="kw"><a href="https://rdrr.io/r/base/list.html">list</a></span>(<span class="dt">GCS_SESSION_BUCKET =</span> <span class="st">"your-session-bucket"</span>), vm2)</a></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li>Upon logging in, you should see a message saying its loading data from GCS:</li>
</ol>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb16-1" data-line-number="1">[Workspace loaded from<span class="op">:</span><span class="st"> </span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2">gs<span class="op">:</span><span class="er">//</span>your<span class="op">-</span>session<span class="op">-</span>bucket<span class="op">/</span>home<span class="op">/</span>you]</a></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li>You should now be able to run <code>ssh -T git@github.com</code> successfully</li>
<li>Pull/push (private) GitHub repos via the steps outlined in the GitHub section above.</li>
</ol>
<p>You can now delete VMs and start up new ones using RStudio Docker, and the GitHub configurations will persist so long as you follow the steps above.</p>
</div>
</div>
<div id="running-rstudio-server-on-app-engine" class="section level3">
<h3 class="hasAnchor">
<a href="#running-rstudio-server-on-app-engine" class="anchor"></a>Running RStudio Server on App Engine</h3>
<p>Since the compute and the data are now separated, you can now become fully cloud native by running RStudio Server on App Engine.  This means you don’t need to worry about servers at all.  Each time you visit your RStudio Server App Engine URL, a new instance will start, loading your data from your last session.  When you finish, close the browser and the VM will tear down itself.</p>
<p>See more at GitHub: <a href="https://github.com/MarkEdmondson1234/appengine-rstudio">RStudio on App Engine</a>.</p>
<p>Running on App Engine has many advantages, including:</p>
<ul>
<li>No servers to setup, stop/start</li>
<li>Only pay for when you are on the VM</li>
<li>Auto auth via the app engine service account</li>
<li>Load balancing and OAuth2 login options</li>
</ul>
</div>
<div id="details-on-how-the-above-is-working" class="section level3">
<h3 class="hasAnchor">
<a href="#details-on-how-the-above-is-working" class="anchor"></a>Details on how the above is working</h3>
<p>This build includes the newest version of <code>googleCloudStorageR</code> and <code>googleComputeEngineR</code> which have had functions added to help with the workflow above.</p>
<p>The functions can store data to Google’s dedicated store via <code>googleCloudStorageR</code>s <code>gcs_first</code> and <code>gcs_last</code> functions.  This Dockerbuild puts the functions into a custom <code>.Rprofile</code> file that will save the projects workspace data to its own bucket, if they have a <code>_gcssave.yaml</code> file in the folder, or if the directory matches one already saved.</p>
<p>The <code>.yaml</code> tells <code>googleCloudStorageR</code> which bucket to save the folder to, or if not present an environment argument <code>GCS_SESSION_BUCKET</code> - this is used on first load when no <code>.yaml</code> file is present.</p>
<p>Thus, you can save an RStudio project via your local computer, then launch an RStudio server in the cloud with the <code>loaddir:</code> argument set to that directory name to load the files onto your cloud server.  Once done, when you quit the R session it will save your work to its own new folder, that when you stop/start a Docker container with RStudio within and create a project with the same name, will automatically load.</p>
<p>It will only download files to your folder that don’t exist, so local changes won’t be overwritten if they already exist.  It is not git, treat it more as a backup that will load if the files are not already present (such as when you relaunch a Docker container)</p>
<p>If you upload to GCS, make sure to load the directory and files you want - delete the GCS folder if you want to stop backups via <code>gcs_delete_all()</code></p>
<p>Example <code>_gcssave.yaml</code>:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode yaml"><code class="sourceCode yaml"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="co">## The GCS bucket to save/load R workspace from</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2"><span class="fu">bucket:</span><span class="at"> gcer-store-my-rstudio-files</span></a>
<a class="sourceLine" id="cb17-3" data-line-number="3"></a>
<a class="sourceLine" id="cb17-4" data-line-number="4"><span class="co">## set to FALSE if you dont want to load on R session startup</span></a>
<a class="sourceLine" id="cb17-5" data-line-number="5"><span class="fu">load_on_startup:</span><span class="at"> TRUE</span></a>
<a class="sourceLine" id="cb17-6" data-line-number="6"></a>
<a class="sourceLine" id="cb17-7" data-line-number="7"><span class="co">## on first load and init, whether to look for a different directory on GCS than present getwd()</span></a>
<a class="sourceLine" id="cb17-8" data-line-number="8"><span class="fu">loaddir:</span><span class="at"> /Users/mark/the/folder/on/local</span></a>
<a class="sourceLine" id="cb17-9" data-line-number="9"></a>
<a class="sourceLine" id="cb17-10" data-line-number="10"><span class="co">## regex to only save these files to GCS</span></a>
<a class="sourceLine" id="cb17-11" data-line-number="11"><span class="fu">pattern:</span></a></code></pre></div>
<p>An advantage on using R on a GCE instance is that you can reuse the authentication used to launch the VM for other cloud services, via <code><a href="https://code.markedmondson.me/googleAuthR//reference/gar_gce_auth.html">googleAuthR::gar_gce_auth()</a></code> so you don’t need to supply your own auth file.</p>
<p>To use, the VM needs to be supplied with a bucket name environment.  Using a separate bucket means the same files can be transferred across Docker RStudio stop/starts and VMs.  This is set in the instance running the Docker’s metadata, that will get copied over to an environment argument R can see.</p>
</div>
</div>
</div>
  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="sidebar">

        <div id="tocnav">
      <h2 class="hasAnchor">
<a href="#tocnav" class="anchor"></a>Contents</h2>
      <ul class="nav nav-pills nav-stacked">
<li>
<a href="#persistent-rstudio-on-google-compute-engine">Persistent RStudio on Google Compute Engine</a><ul class="nav nav-pills nav-stacked">
<li><a href="#using-base-vm-file-system">Using base VM file system</a></li>
      <li><a href="#github">GitHub</a></li>
      <li><a href="#using-googlecloudstorager">Using googleCloudStorageR</a></li>
      </ul>
</li>
      </ul>
</div>
      </div>

</div>



      <footer><div class="copyright">
  <p>Developed by <a href="http://code.markedmondson.me">Mark Edmondson</a>.</p>
</div>

<div class="pkgdown">
  <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.4.1.</p>
</div>

      </footer>
</div>

  


  </body>
</html>
