# R Markdown tip: use Insert Chunk shortcut in RStudio

I have been using R Markdown more often in RStudio to prepare reproducible documents for our statistical consulting class. For R Markdown, we have to declare the start of a coding region by typing {r labelandoptions} and the end by . This becomes tedious when there are many coding regions. Fortunately, in RStudio, there is a Insert Chunk feature (Ctrl-Alt-I on PC and Cmd-Option-I on Mac) that automatically types this. It saves so much typing!

On a side note, it’d be great if a R Markdown document could be collapsed to only R code or only markdown, perhaps hiding the other with ellipses. This would help to analyze the R code or to see the flow of the document. If any RStudio developers read this, please consider it!

# R tip: read data into R from code file

Most often, we enter data from an external data file. Sometime when we have a very small dataset like from a tutorial or the r-help mailing list, it is more concise to include it as part of the R code. We can use read.table() but use text=”” instead of file=””.

I ran into this from Rui Barradas on the r-help mailing list at https://stat.ethz.ch/pipermail/r-help//2013-May/353030.html.

dat <- read.table(text = "
X     Y
0    137.659166666667
2.502    152.623333333333
4.998    162.391333333333
7.501    170.753666666667
9.997    174.877
12.499    178.4275
15.002    184.565
17.498    189.8985
20    192.6885
dat


Output:

       X        Y
1  0.000 137.6592
2  2.502 152.6233
3  4.998 162.3913
4  7.501 170.7537
5  9.997 174.8770
6 12.499 178.4275
7 15.002 184.5650
8 17.498 189.8985
9 20.000 192.6885