Using R to Read Eye-tracking Files (.txt, .tsv, ect.) and Organize Data

[This article was first published on Hui Tang's R Site, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If a researcher is interested in certain time periods of an eye-tracking recording, such as problem-solving phases in education studies, he/she can replay the recording, generate segments or scenes, and then export the relevant data. However, this process is time-consuming. Using R may provide a way to quickly obtain and organize such data (when the definitions of time periods are clear). Here I use the text files exported by Tobii Studio as an example.
Suppose there are 15 .tsv files (which can be considered as .txt files) in a local folder “D:\tsv-aoi”. Each file contains the recording of a participant in an eye-tracking experiment.


The first step is to get all the 15 file names:

filenames <- list.files(full.names=TRUE)

Each file looks like:


If we plan to obtain information between the first AOI11_1 (Timesamp=306913 in the above file) and the first AOI11_1 coming back (Timesamp=319356 in the above file), the next step is to read all 15 files into (a list of) data frames (notice the first 11 lines in each file are not needed), get the “Timestamp” and “AoiNames” columns for AOI11’s only.

aoiFiles <- lapply(filenames, function(x){
                      read.table(x, header=TRUE,  sep=”\t”, skip=11)
aoiFiles_q11 <- lapply(aoiFiles, function(x){
                               x[grep(“tor11.html”, x$StimuliName), c(1,4)]

The result (showing from one of the 15 files):


We may remove consecutive duplicate AOIs from each data frame:

RmRepeat <- function(df, n){
   X <- rle(as.character(df[,n]))
   Y <- cumsum(c(1, X$lengths[-length(X$lengths)]))
aoiFiles_q11rd <- lapply(aoiFiles, function(x) RmRepeat(x, 2))


The final result (one of the 15) is shown below. From there we can analyze eye-tracking measures such as fixation duration and scanpath between the 2 red time stamps.








To leave a comment for the author, please follow the link and comment on their blog: Hui Tang's R Site. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)