Wordpress.com "code blocks": special characters like dplyr pipe (%>%) causing errant display

98 Views Asked by At

I am trying to show R code on my wordpress.com blog but the code is being distorted when I preview or publish. This particularly happens with the dplyr pipe function (%>%), of which I lose the percent and greater than sign sometimes.

For example, this:
[code language="r"]
library(dplyr)
tester <- mtcars %>% head(5) %>%
pull(cyl)
[/code]

becomes the following (missing ">%" after tester):
library(dplyr) tester % head(5) %>% pull(cyl)

Even using the <pre> tag referenced in the Wordpress "Writing Code in Your Posts" article caused some distortion, despite this tag purportedly causing the browser to "exactly reproduce whatever is inside of the tags".

1

There are 1 best solutions below

0
Rick Pack On

Add the code to a Github Gist at https://gist.github.com/.

Then insert the entire Gist URL (starts with https://gist.github.com/) for the webpage displaying your code (has your Github ID name at the top and an embed block at the top you will not use) into the HTML section of your code on its own line. For example:
This is some text. And now here is some code: https://gist.github.com/RickPack/f159e1b35b096f5325ac7093652d4b2e

Placing the Gist URL into the HTML section of your Wordpress.com post will cause Wordpress to display the code associated with your Gist in an attractive fashion rather than the URL. While your particular language's syntax highlighting may not be preserved, at least there is no loss of characters.