+ Reply to Thread
Results 1 to 1 of 1

Thread: How to use Code tags

Hybrid View

  1. #1
    Devorador de queso
    Points: 95,995, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent Poster
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,938
    Thanks
    307
    Thanked 2,630 Times in 2,246 Posts

    How to use Code tags

    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] <- 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 [code] [/code] 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

    Name:  EditingCode.jpg
Views: 930
Size:  15.9 KB

    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.
    I don't have emotions and sometimes that makes me very sad.

  2. The Following 7 Users Say Thank You to Dason For This Useful Post:

    gianmarco (11-23-2013), GretaGarbo (01-03-2013), Jesmin (07-17-2013), quark (10-17-2012), Target.Stat (11-28-2013), TheEcologist (08-12-2013), trinker (02-15-2013)

+ Reply to Thread

           




Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats