Load Data From Your R Script Directly, No Files Needed

Ran across this nice little trick a few months back. One of the things I miss about SAS was the ability to copy/paste data directly into a SAS script. While it’s nice to load from an external file, for small datasets it can be more useful to have everything in just one file. This is especially useful with students who have trouble understanding the concept of a working directory or file paths in a smartphone app dominated world.

Here’s what I found months ago (I believe on Stack Overflow):

Input = ("
participant condition time qsans qscorr
1 1 350 10 3
2 1 360 6 6
3 1 360 3 2
4 1 360 8 4
5 1 360 7 2
 ")
data = read.table(textConnection(Input),header=TRUE

There are no line numbers on that snippet – you’ve simply got 5 variables with 5 lines of data. The data is then read through a textConnection function into a data table. You can now work with it as you would any other data set.