I've taken my first steps with Octopress, but I'm still a little wobbly on my feet (er, arms). Since I write most of my posts in RStudio using the RMarkdown language and the knitr package, I thought I'd try writing a function in R that knits my .rmd (that's the RMarkdown extension) files directly to my blog, including making a new post and assigning categories and tags. When I was using wordpress I could use the knit2wp function, but that was back when I was still doing things the easy way. Here goes!

knit2blog = function(file, layout = "post", title, 
                     comments = c("true", "false"), categories = "", 
                     blogdir = getOption("blogdir"),
                     postdir = getOption("postdir"), mate = F){

  curdir = getwd(); on.exit(setwd(curdir))

  # knit to temporary html file
  outfile = "temp_outfile.html"
  knit2html(file, output = outfile) # HTML content, temporarily stored in wd

  # Make yaml front matter:
  comments = match.arg(comments)
  curtime = format(Sys.time(), "%Y-%m-%d %H:%M:%S %z") 

  if(length(categories) > 1) 
    categstr = paste0("[", paste(categories, collapse = ", "), "]") 
  else categstr = categories

  yamlfm = paste0("---\nlayout: ", layout, '\ntitle: "', title, '"\ndate: ', 
                  curtime, "\ncomments: ", comments, "\ncategories: ", 
                  categstr, "\n---")

  # make file with only yaml front matter
  curdate = format(Sys.time(), "%Y-%m-%d-") 
  postname = paste0(curdate, title, ".html")
  postloc = paste0('"', blogdir, postdir, '"')
  write.table(yamlfm, postname, quote = F, row.names = F, col.names = F)

  # Add html content to yaml front matter html file
  file.append(postname, outfile)
  file.remove(outfile)

  pn.q = paste0('"', postname, '"') # NEED EXTRA QUOTES FOR CALLING mv

  calstr = paste("mv -f", pn.q, postloc, sep = " ")
  print(calstr)
  system(calstr)  

  # Optionally open in TextMate
  if(mate) system(paste0("mate ", postloc, "/", pn.q))
}

options(blogdir = "/Volumes/Macintosh\ HD/Users/mark/Documents/OctopressBlog/octopress/",
        postdir = "source/_posts/")
knit2blog("USGS dataRetrieval.rmd", title = "testtest", categories = c("cat1", "cat2"),
          mate = T)

This seems to work. Now I'll add this to my .Rprofile for the working directory I write my blog posts out of. I'll also add the options settings for the blog and post directories. Cool!