11
$\begingroup$

Adding r tag to the question syntax highlights the R code present in the question or in the answers. Examples here and here.

On the other hand questions tagged r imply that the question is related to R. So if somebody gives an example of R code in answer it is desirable that it should be highlighted, but it may be undesirable to tag the question with r tag, because the question is unrelated to R.

What should the policy be in this case? Should we simply add r tag with explanation, or add explanation in r tag wiki? Or maybe there is other way to indicate that answer needs special highlighting without adding additional tag to the question? Note that this applies for other programming language tags.

Update Note that I am only asking for an opinion and not advocating one approach or another. I felt that my language was not very clear in expressing that, hence this update.

$\endgroup$
  • 1
    $\begingroup$ is it possible to detect R code, for example, do any other languages use the <- assignment? $\endgroup$ – David LeBauer Jan 23 '11 at 2:39
7
$\begingroup$

Although I particularly appreciate users that provide R code (or any other language) to illustrate their response, I think that tags should actually reflect the question (or general subject matter) rather than individual responses. In some other cases, MC methods were used where analytical solutionw were provided.

We should make a clear distinction between questions that are purely R-related (e.g., R tag often comes with data-visualization) from questions that are more theoretical in essence, or at least independent of software issues. Otherwise, there's a risk of over-representing the R software.

$\endgroup$
  • 2
    $\begingroup$ after looking at the questions with r tag, I tend to agree. $\endgroup$ – mpiktas Jan 10 '11 at 9:45
  • $\begingroup$ ok, as nobody else commented, this means that everybody agrees to @chl. So I am choosing this an answer, and personally myself will consider this answer as official policy $\endgroup$ – mpiktas Jan 11 '11 at 8:37
5
$\begingroup$

Why is it so essential that the R code be syntax highlighted?

Unless the question owner specifically asked for R code, adding the tag just for a bit of highlighting feels rather abusive to me.

Can you make the case that

  1. R is the only language code will ever be posted in here, and thus should be forced for all code blocks on all questions site-wide?

  2. all preformatted code blocks will be a recognizable language and default "best guess" code syntax highlighting should be performed?

(Note in the latter case in our experience, a LOT of preformatted "code" in the form of logfiles and so forth is actively harmed by attempting to infer the syntax highlighting rules in every single code block.)

$\endgroup$
  • 1
    $\begingroup$ Well highlighting of course is not essential, but is certainly helpful. Note that this is my first meta site post and I've added feature-request tag, because the meta site requests one of the 4 tags, bugs, feature-request, discussion, support for question to be posted. I have picked the first tag which was apropriate. My initial intention was to ask what is the site policy. $\endgroup$ – mpiktas Jan 10 '11 at 9:16
  • $\begingroup$ I can argue for the second case, because the it is entirely appropriate that the answerer gives the Monte-Carlo code which partially or entirely answers purely theoretical question. One can argue that this even should be encouraged. Both of my links provided support this. $\endgroup$ – mpiktas Jan 10 '11 at 9:19
  • $\begingroup$ @mpi the top tag on the site is [r] per stats.stackexchange.com/tags -- so we need the rest of the community to weigh in a little. $\endgroup$ – Jeff Atwood Jan 10 '11 at 9:22
  • 1
    $\begingroup$ Sir, having done only a bit of stat consulting, my suspicion is that cv should use r as a best guess for syntax highlighting because unless a user explicitly asks for SAS, or SPSS, or Minitab, etc. Most here will provide examples in R because it's free to download / use whereas the others require $$. And yes, though we may use c++, python, or others for heavy lifting most of those questions are relegated to Stack Overflow. $\endgroup$ – M. Tibbits Jun 30 '11 at 0:26
5
$\begingroup$

There is another option that has not been pointed out here. You can insert an HTML comment that will automatically turn on syntax highlighting within a post (see: What is syntax highlighting and how does it work?). The way it works is that you put <!-- language: lang-r --> on its own line prior to the code. Then the next code block will be highlighted as R code. (Note that the comment itself will not be visible in the post.)

This can be done with a number of languages, including (at least), lang-r, lang-python, and lang-matlab, as well as the major programming languages (c++, java, etc.). I don't know if there is support for other statistical software languages (e.g., SAS or Stata).

There are a couple of limitations with this. First, there will only be highlighting within code blocks; inline code is not highlighted. Second, this will only highlight the next code block, if there is text, or a figure, or something else between one code block and a subsequent code block, the subsequent code block will not be highlighted. Thus, you need to either add the comment prior to each code block, or use <!-- language-all: lang-r --> to highlight all subsequent code blocks (note the addition of -all in the comment). Third, these comments will not highlight code blocks prior to the comment. Lastly, the comment will only highlight the code blocks within that post (question or individual answer); other posts on the same thread will not be affected. (Adding, say, the tag to the question will highlight all code blocks on the thread.)

You can also use these comments to locally override syntax highlighting on a thread. For example, if a question included the tag, but a code block in an answer was not r code, highlighting could be turned off via <!-- language: lang-none -->, or could be switched to highlighting for a different language, e.g., <!-- language: lang-python -->.


The following provides an example of how this can be done. Because the HTML comments will not show up, when I place them, I will subsequently place a copy of the comment as inline code to make it clear what was done. (Hmmm, annoyingly the syntax highlighting does not seem to be working on meta, although I just tested it and everything below shows up as it should on the main site.)

Here is some text answering a question.

here  # is some non-highlighted code in a code block

Now I have more pearls of wisdom.

<!-- language: lang-r -->

this = "some highlighted code"  # nifty!

Random crankiness and weird digressions from old man.

# once again, code is not highlighted

Sage remarks.

<!-- language-all: lang-r -->

highlighted  # code

Increasingly tedious exposition with every other word inexplicably italicized.

code "still"  # highlighted 
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .