title: Run cron job within RStudio
body: <p>I am trying to schedule a cron job via RStudio on EC2 instance.<br>
  When I use a basic script with very basic functions it works fine.
  But when I want to schedule a script that contains R functions from specfic R packages, the scripts fails.</p>
  
  <p>My cron looks like this:</p>
  
  <pre><code>MAILTO="myemail@adress.com"
  36 * * * * /home/user1/Test_for_Cron.R
  </code></pre>
  
  <p>After the script is launched, I receive an email from Cron Daemon specifying something like:</p>
  
  <blockquote>
    <p>Error in <strong>R_function</strong> : 
      could not find function <strong>R_function</strong><br>
    Execution halted</p>
  </blockquote>
  
  <p>From what I found, it seems I have to specify that I want to run the script within RStudio or that I want to "source" it.<br>
  I have the feeling the answer might be in the combination of these 3 posts below but I am not finding the solution after testing out some suggestions:</p>
  
  <p><a href="https://stackoverflow.com/questions/12389158/check-if-r-is-running-in-rstudio">Check if R is running in RStudio</a><br>
  <a href="https://stackoverflow.com/questions/45518592/cron-job-for-an-r-script-failing">Cron job for an R script failing</a><br>
  <a href="https://stackoverflow.com/questions/13672720/r-command-for-setting-working-directory-to-source-file-location-in-rstudio">R command for setting working directory to source file location in Rstudio</a>  </p>
  
  <p>Help much appreciated !</p>