Blog with R Markdown and tumblr: Part II
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In Part I of this series I described how to set up your tumblr blog so that you can create posts like those on the example site R Markdown Blog.
Now I’ll describe how you can actually create such posts. I’ll be using the RStudio IDE for the desktop in all the steps below, but know that you can use your own version of R and your own editor for steps 1, 2, and 4. I personaly like the the RStudio knitr integration. It provides a really easy and fast iterative process to quickly edit markdown and render to HTML.
Step 1: Install The Latest Version of the R markdown package
markdown version 0.5.2 is needed for this process, and since it’s currently not on CRAN (it’s on its way) you will need to get it from github. This is easily done with Hadley Wickam’s devtools package. Follow these steps to install devtools, markdown, and knitr which you will need in later steps:
install.packages("devtools") library(devtools) install_github("markdown", "rstudio") library(markdown) install.packages("knitr") library(knitr)
Step 2: Create a New R Markdown Document
In RStudio, click on File -> New -> R Markdown. This will create a new untitled file with some example markdown text. The first two lines of the file contain a proper title for the document in markdown syntax, but we won’t need that for our blog post. Go ahead and delete them.
Save the file and name it First-Post.Rmd.
Step 3: Click the “Knit HTML” Button
That button is just above the first line of the file. You should see a ball of yarn with a knitting needle sticking in it. After clicking the button you should see a couple of windows flicker by with info, and then ultimately this:
If your window looks like this, then congratulations! You just created a valid R Markdown document and rendered it into an HTML page. This step automatically creates a new file called First-Post.html, but we’re not ready to blog just yet.
Side Note About Iterative Development
If your window doesn’t look like the above, then you’ve got some editing to do. You will now enter an iterative edit/save/knit loop, and this is where RStudio really shines. Here are the steps:
- Make your edits to First-Post.Rmd.
- Type Ctrl-s to save.
- Type Ctrl-Shift-h to re-knit the document. equivalent to clicking the “Knit HTML” button.
- If you get the output you want, your done, if not go to 1.
Simple as that!
Suppose you’re not using RStudio. Then you can still get pretty close to the above. Using your favorite editor, your favorite browser, and another R IDE, follow these steps:
- Make your edits to First-Post.Rmd, then save.
- Execute the following commands in R:
knit2html("First-Post.Rmd") # Step 3 browseURL("First-Post.html") # Step 3.1
Your browser should open with First-Post.html displayed. If you get the output you want, hurray! Otherwise go to step 1.
So goes iterative development 😉 Now on to blogging…
Step 4: Execute knit() and markdown2HTML() Manually
Up to this step, we’ve been using knit commands to create complete HTML documents, those that contain beginning HTML tags like , , and … tags that every blogging platform will not accept in their blog posts. These commands also inject javascript code to render math equations and highlight R code chunks, which is nice, but those are already in our tumblr theme we created in Part I (something I didn’t tell you before).
So now our job is to prepare the HTML without these tags and extra javascript. We need to call knit() and then markdownToHTML() with the fragment.only option set to TRUE. Run these steps manually in RStudio (or R):
knit("First-Post.Rmd") # produces the intermediate file 'First-Post.md' markdownToHTML("First-Post.md", "First-Post.html", fragment.only = TRUE)
Now open up First-Post.html in your editor and you should see the following:
I’ve highlighted lines 1 and 22 with a red circle. Notice on line 1 that there’s no beginning HTML markup as I described above. That’s good, and your output should look similar if not the same.
Also notice on line 22 that the tag looks a little unusual. That’s because by default markdownToHTML will automatically embed locally linked images using base64 encoding. You really don’t need to know how it’s encoded, but just know that your browser will show you the image that you were expecting. That’s the beauty of the markdown package. You have just one HTML document that contains all your codes and plots!
Okay, now we leave RStudio for a second and go to tumblr…
Step 5: Log In to tumblr and Click the “Gear” Icon
I’ve highlighted it in red:
Step 6: Click the “plain text/HTML” Radio Button, Then Click “Save”
The blog posts we are creating will contain HTML, so we want to ensure that we’re using the correct editor, highlighted in red:
Note that the “Save” button is at the bottom of the page, so you’ll probably have to use the scroll bar on your browser to get there.
Step 6: Click on the “Text” Icon to Start a New Blog Post
I still get tripped up on this as I haven’t blogged much in the past, but once you log in to your tumblr account and click on your blog name, you will want to click the icon circled in red below to start a new “Text” blog post:
Be sure that you see “HTML enabled” highlighted below in red:
Go ahead and fill out the Title with “Text” (or something else to your liking).
Step 7: Copy First-Post.html
Switch back to RStudio, and copy the entire text of First-Post.html using Ctrl-a then Ctrl-c (or your favorite incantation).
Step 8: Paste First-Post.html Into the tumblr Editor, Then Click “Create Post”
Now switch back to your browser, Paste First-Post.html (Ctrl-v) into the tumblr editor, scroll down to the bottom of the page and click the “Create Post” button.
You will now be taken back to your Dashboard and you should see something like this:
What you are now looking at is your Dashboard’s interpretation of your blog post. That’s okay, but what you really want to see is your blog. Click on the button I’ve highlighted in red above to get to your blog, and you should see something like the example I created at http://jeffrey-horner.tumblr.com/.
Step 8: Done!
Congratulations! You just created your first blog post with R Markdown. Now go back to R and create some meaninguful statistical content that we can all learn from! And don’t forget to blog about it!
Cheers!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.