Formatting R code in WordPress

To show formatted code within a paragraph, for example a function name, use <code>plot</code> which will appear as plot.

To show a block of code, use something like this but using square brackets [] rather than braces {}.

{code language=”r”}
x<-rnorm(100)
hist(x)
#don't forget comments
{/code}

Setting the language to R lets the WordPress plugin use appropriate syntax highlighting.

When formatted, the code will look like this

x<-rnorm(100)
hist(x)
#don't forget comments

There are more options to set line numbers and highlighting by adding extra parameters.

Tips:

  • Keep the lines of code short – long lines will force the user to scroll.
  • Use the text editor not the visual editor (which may garble your code)
  • Check the code works (it is very tempting to edit it and break it)

About Richard Telford

Institutt for biologi

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.