Skip to content

How to post R code on WordPress blogs


Most WordPress BloggeRs are using this text highlight syntax, some are not.
I hope that this post would be a reference source for new WordPress BloggeRs for posting their R code on their blog posts.

According to an official guide by WordPress.com on “Posting Source Code“, To post R code in the WordPress.com, just wrap R code as follows (without “#” in both wrappers):

######################################################

[#sourcecode language="r"]
Your R code and comments
x <- rnorm(100)
y <- x + 10
[#/sourcecode]

######################################################

  • From above, before your R code put the command in line 1, or [#sourcecode language=”r”]but without #
  • Then, place your R code (line 2-4).
  • End the code box by put the command line as in line  5, but without # or “[/sourcecode]

Then the code will appear as following.

Your R code and comments
x <- rnorm(100)
y <- x + 10

Moreover, more options can be configured to better describe the code efficiently.

  • autolinks (true/false)
    TRUE: Makes all URLs in your posted code clickable.
    Defaults: TRUE
  • collapse (true/false) 
    TRUE: The code box will be collapsed when the page loads, requiring the visitor to click to expand it.
    Comment: Good for large code posts.
    Defaults: False.
  • firstline (number)
    Comments: Use this to change what number the line numbering starts at
    Defaults =  1
  • gutter (true/false)
    TRUE:  Show the line numbering on the left hand side.
    FALSE: The line numbering on the left side will be hidden.
    Defaults = TRUE
  • highlight (comma-seperated list of numbers)
    You can list the line numbers you want to be highlighted.
    Example = “4,7,19″.
  • htmlscript (true/false)
    TRUE: Any HTML/XML in your code will be highlighted.
    Comment: This is useful when you are mixing code into HTML, such as PHP inside of HTML.
    Defaults = FALSE (only work with certain code languages)
  • light (true/false)
    TRUE: The gutter (line numbering) and toolbar (see below) will be hidden.
    Comment: This is helpful when posting only one or two lines of code.
    Defaults = FALSE
  • padlinenumbers (true/false/integer)
    TRUE: Automatic padding
    FALSE: No padding, and entering a number will force a specific amount of padding.
    Comment: Allows you to control the line number padding.
  • toolbar (true/false)
    FALSE: The toolbar containing the helpful buttons that appears when you hover over the code will not be shown.
    Defaults = TRUE
  • wraplines (true/false)
    FALSE: Line wrapping will be disabled. This will cause a horizontal scrollbar to appear for long lines of code.

If you are using WordPress.org, here is a plugin.

Update: I just found a nice post by William K. Morris on How to update your WordPress.com blog from R

One Comment

Trackbacks & Pingbacks

  1. Embed R Code in Blog of WORDPRESS.COM « xuyafei

Comments are closed.

%d bloggers like this: