Tuesday, April 7, 2015

How to Auto-Download a CSV File From a Password Protected Website

The Challenge

I’d like to automatically download a csv file from a password protected site daily.

Step 1

I first attempted to use read.table but this would not work because the userid is my email address and contains a @. Therefore, I needed to use browseURL.
  ## browseURL("http://myEmail@work.com:myPsswrd@website.com/myFile.csv", 
  ##      browser = "C:/Users/Path/Local/Google/Chrome/Application/chrome.exe")
This willl automatically download my csv file to whatever local folder I have designated for Chrome downloads.

Step 2

My file has been downloaded, but now I want to copy the original into a new folder and delete the original.
  ## myData <- read.csv("myFile.csv")
  ## setwd("C:/Path/To/New/Folder")
  ## write.table(myData,"newFile.csv")
  ## setwd("C:/Path/To/Old Download/Folder")
  ## remove.file("myFile.csv")

Step 3

Next we save the combined scripts above as autoDownload.R and schedule the script to run daily in Windows Task Scheduler.
  • Open Task Scheduler and Create a new task
  • Under Action tab: use path to Rscript.exe in Program/Script field
  • Use autoDownload.R in the Add Attributes field
  • Use path to autoDownload.R in the Start in field

Complete

Thursday, April 2, 2015

How to Autopost an Rmarkdown File to Blogger

The Challenge

I’d like to create a blog that I can automatically post an Rmarkdown file daily.

Step 1

My first solution was to use the RWordPress package in R, but it was not available for my version. My next solution was to automatically post to a blog with an email, which is allowed by Blogger.com (and I’m assuming other blog sites). My first step was therefore to set up a blog with blogger.com.

Step 2

After setting up a blogger account and creating a blog, I needed to set up a blog email address to receive my Rmarkdown files. I used this Google Support site to walk me through the process.

Step 3

Now that I’ve created my blog and my blog email address, I need to set up an R script to knit the Rmarkdown file to html then mail the file to my blog email address. In mailR, keep in mind that html = TRUE and you can post pictures using attach.files.
##  library(mailR)
##  library(knitr)
##  knit2html("blogPosts.Rmd", options = "")
##  subject_line <- "How to Autopost an Rmarkdown File to Blogger"
##  send.mail(from = "myEmail@work.com",
##      to = "mySecrectEmail@blogger.com",
##      subject = subject_line,
##      body = "blogPosts.html",
##      html = TRUE,
##      smtp = list(host.name = "smtp.gmail.com", port = 465,
##      user.name = "myEmail@work.com",
##      passwd = "*******", ssl = TRUE),
##    attach.files = blogPostPicture.png,
##      authenticate = TRUE, send = TRUE)
Next we save the above script as post2Blog.R and schedule the script to run daily in Windows Task Scheduler.
  • Open Task Scheduler and Create a new task
  • Under Action tab: use path to Rscript.exe in Program/Script field
  • Use post2Blog.R in the Add Attributes field
  • Use path to post2Blog.R in the Start in field

Complete