Formatting Code: the Basics
All code or console output you include in your posts should be formatted properly. Luckily, this is very easy to do! Just use the code formatting button at the top of the post editing box:
Select some code
Click the </> button!
The code formatting button automatically adds special Markdown formatting symbols to whatever you selected. If you check the preview pane to the right of your post, you'll see that the forum software now outputs nicely formatted code .
The code formatting button is pretty smart! It can tell the difference between snippets of code that are part of a sentence and blocks of code that stand alone. Sometimes the software will even detect that you are pasting in code and add the formatting symbols automatically!
Why should I format code and console output?
There are plenty of good reasons to learn how to properly format your code!
- Keeping things tidy is one of our core community principles :sweep:
- Unformatted code is difficult for other people to read
- Leaving your code unformatted makes it less likely you'll get replies .
- Continuing to post unformatted code after someone has asked you to fix it often comes across as rude .
- Console output often contains characters that the forum software interprets as special formatting symbols (e.g.,
>). This can cause parts of the text to become unrecognizable, or even to disappear completely! Marking console output as code keeps it safe from confusing accidental formatting.
Can't I just post a screenshot?
Please don't post screenshots of code. Screenshots of code are even worse than unformatted code . Here's why:
- Screenshots are often difficult to read (and useless to anybody using a screen reader).
- Helpers usually want to try to run your code, not just read it. Screenshots force your helpers to type everything back in by hand . That's a lot to ask and many people won't bother.
- Screenshots aren't searchable, so they don't help others with similar problems find your post later on.
Formatting Code: Beyond the Basics
How do I format R Markdown source?
Use four backticks and the "markdown" syntax hint to surround the source of R Markdown documents:
|Type this:||See this:|
What is this "backtick" symbol?
The backtick symbol looks like this:
It is not the same thing as a single quote. It is technically the grave accent symbol, but used alone and not combined with any letter.
On QWERTY keyboards, the backtick is found on the same key as the tilde (~), immediately to the left of the 1 key:
Adapted from: Denelson83, CC BY-SA 3.0
My keyboard doesn't have a backtick!
The backtick is not available on some keyboard layouts, and can be hard to find on others.
In these cases, the easiest thing to do is probably to use the code formatting buttons instead of typing backticks manually. However, you can also click the code formatting button without anything selected to insert backticks into the post editing box. Then copy and paste those backticks wherever you need them!
What are all the ways I can format code by hand?
Code can be formatted by hand in several ways:
Use any of the CommonMark markdown code formatting styles: indented code blocks, fenced code blocks, and single-backtick inline code.
For fenced code blocks, you can supply syntax highlighting hints, similar to Github-flavored Markdown. If you don't supply a hint, the highlighter will guess. The hint for R code is
```r, but on this site the highlighter will usually identify R code correctly without a hint.
Use BBCode syntax:
<code></code>for inline code, and
<pre><code></code></pre>for blocks of code.
|Type this:||See this:|
[code] var body = d3.select("body"); var div = body.append("div"); div.html("Hello, world!"); [/code]
<pre><code> var body = d3.select("body"); var div = body.append("div"); div.html("Hello, world!"); </code></pre>
Got something to ask? Or add?
FAQ posts are closed to replies. If you have any questions or suggestions, please visit the corresponding discussion thread: https://community.rstudio.com/t/faq-discussion-how-to-format-your-code/37220