# How to use Code tags

#### Dason

Using Code Tags

This thread contains a brief introduction to using code tags and presenting code in general on this forum. I'm biased toward R so that is what I'll be focusing my example on but the same advice holds true for SAS, C, LaTeX, ... as well.

Let's say you want to present some code that does a simple for loop

out <- numeric(10)
for(i in 1:10){
out <- i
}

Quite simple. It is a terrible way to accomplish the goal but it has some indentation. Oh wait you can't see the indentation because it wasn't formatted properly by the forum. This is where [noparse]
Code:
[/noparse] tags come in handy. They provide you a way to explicitly post code and retain indentation. Here is the same example using code tags. The first thing is what you would actually write in your post

View attachment 3167

And this is how the forum will format it for you:
Code:
out <- numeric(10)
for(i in 1:10){
out[i] <- i
}
Also note that by wrapping the code in code tags the forum provides a "select all" button which gives the people reading your post an easy way to copy the code you've provided. Please use the code tags when posting code.

Reproducible Examples

The following provides a very brief note on providing data to others on the forum in R . For more advice please read the following: Hadley Wickham's page on reproducible exampels which provides many more pieces of advice on creating reproducible examples.

Let's say you want to provide us some data in R. A lot of times we don't need the full data set but if you can provide a small reproducible example of the data you're working with it can help us out immensely. The dput() function can allow you to easily upload your data in a way that it's easy for us to read into R without needing to download extra files.

Let's say you want to give us the data I have named 'dat' in the following example
Code:
> # At the R command prompt
> dat
x           y
1   1  0.31234017
2   2  0.03003890
3   3  0.09835056
4   4 -0.02253704
5   5  0.58560351
6   6  0.75123437
7   7  0.37347965
8   8 -0.44980892
9   9 -1.04424109
10 10  0.36274473
You can use dput on dat to get an ascii representation
Code:
> # At the R command prompt
> dput(dat)
structure(list(x = 1:10, y = c(0.312340173436533, 0.0300389023361891,
0.0983505582610428, -0.0225370444278445, 0.585603508706503, 0.75123437405997,
0.37347964854251, -0.449808918431326, -1.04424109329699, 0.362744734756743
)), .Names = c("x", "y"), row.names = c(NA, -10L), class = "data.frame")
Then when you're sharing your code/data with us you can easily give us the data in by copying the output given from dput and including it in your post like I do here:
Code:
# In the script you're providing
dat <- structure(list(x = 1:10, y = c(0.312340173436533, 0.0300389023361891,
0.0983505582610428, -0.0225370444278445, 0.585603508706503, 0.75123437405997,
0.37347964854251, -0.449808918431326, -1.04424109329699, 0.362744734756743
)), .Names = c("x", "y"), row.names = c(NA, -10L), class = "data.frame")
Notice that anybody can now just copy that code and run it in R and they have access to the data. This is much easier, transparent, and trustworthy than asking somebody to download a file and use that.