Last week, we learned how to build simple web pages using R Markdown. Over the next few weeks, we will learn to build more complex web pages using a combination of HTML and JavaScript. In particular, we will learn to use a JavaScript library called JSPsych. (Note: A library is a little like an R package – a set of functions for doing useful tasks in a programming language). JSPsych is created by Josh de Leeuw, and this tutorial is based on his introduction.
Create a new directory for this week’s lab, and then create a subdirectory called SimpleExpt
. Download the lab zip file from Learn. Place it in the SimpleExpt
directory and unzip it.
What does the zip file contain?
css
contains jspsych.css
; a css
file contains layout instructions for a webpage (e.g., how large should the default text be; how should the different elements be arranged).jspsych.js
is a JavaScript file (that’s what .js
stands for). It contains a set of functions for accessing all the other aspects of the JSPsych package.plugins
contains a large number of .js
files. Each file (called a plugin here) contains JavaScript code, and that code provides a template for specifying the appearance of a webpage and how the user can respond to that webpage (e.g., should the website display an image? should the user press a button in response to that image?)tests&examples
contains web pages that exemplify how JSPsych can be used. Try opening the file demo-simple-rt-task.html
in your web browser.In this lab, you will go through some instructions for how to create that website. In the next lab, we will significantly modify the code, in order to turn that website into a game for a tablet computer.
At its simplest, a web page is simple an HTML
document. HTML
stands for hyper-text mark-up language; so, a web page is a mark up document (like R Markdown!), but one that also has hyper-links embedded in the text.
A neat fact about R Studio is that it can be used to edit both HTML documents and JavaScript documents. So, we can continue to use R Studio for this entire project.
Create an HTML file in R Studio. Go to File > New > R HTML. Save the file (File > Save) in the lab directory (call it, e.g., Simple_Expt
). You’ll note that this isn’t really an HTML file, but an RHTML file. An RHTML file is an R file that can be used to generate an HTML document; you’ll learn more about this in a moment.
Basic R HTML file
Let’s run through the components of the web page.