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
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!