How to do lab sessions

RStudio Notebooks for Lab Sessions

This is a brief description of how to use RStudio for the MTH202 lab sessions.

The first step is to go to the “Project” drop-down menu on the right top corner of the RStudio window and open a new project with a name like “Lab1” or “Lab2” and so on, depending on which lab session you are working in.

The second step is to go to the “File” drop-down menu on the left top corner of the RStudio window and, under the sub-menu “Open File”, open an “R Notebook”.

This will give a panel with some data already entered.

This will be an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

You can the greyish chunks of R code by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter or by pressing the green right arrow that it near the top of the chunk. The output of the chunk will be placed immediately after it.

2+2
[1] 4

You can add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I. You can also create a new chunk just by typing three open quote (`) characters and putting {r} at the start of this. Everything you type from there till three more open-quote characters will be treated as a program and you can run it.

sample(c(0,1),20,replace=T)
 [1] 1 1 0 0 0 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0

Next thing to learn is to save a notebook. You can do this by pressing Ctrl+S or by using the little “floppy” like icon on the line below the menu line. The first time you do this you will be prompted for a file name. You can give any suitable name to the file. On later occasions the same name will be used.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file). When you ask for a preview you may have to click Try Again if a popup window appears.

The preview shows you a rendered HTML copy of the contents of your panel. This is ready for submission! To download this, look in the “Files” panel on your right for a file with the same name as your notebook and extension “.nb.html”. Select it by clicking the button to its left. Then go to the “More” sub-menu of this panel and select “Export”. You will be prompted for a download.

In case you close the notebook or session and wish to re-open it just use the “Open File” sub-menu of the “File” menu or use the right “Files” panel to click and open the file you created (with extension “.Rmd”).


Last modified: Thursday, January 11, 2018, 8:54 AM