Teaching Tricks Previous Up Next

22  Teaching Tricks

There are several tricks that are of use to teachers of statistics using R in the lab. Here are a few.
Exchanging data with students
The task of getting data and functions to the students so that they may easily use it in the lab is really quite simple using R thanks to some handy commands provided by R's developers.

The scenario is you, the instructor, have created some functions and have data sets that will save your students from typing or something else. You want to get them from your computer to the students. Thus there are two things: saving and reading in.
Saving your work
The package mechanism for R can be used for this and should be if you have major amounts of work. However, if you have a lab sessions worth of data and functions you may not want to go to the trouble. Instead, you can save the commands and data using dump into one file.

For example, suppose you have a function really.convenient.function and a dataset too.large.to.type and you want to save these in a file to distribute to your students. This can be done with

> dump( c("really.convenient.function","too.large.to.type"),
        file = "file-for-my-students.R")      
    
This creates the file with your given file name which can later be "sourced" into your student's R session.
Distributing your work
You probably can put your file on floppies and distribute to your students to read in during a lab session using source.

This is very easy, but not the best solution if you have access to the internet. In this case, you can place your file on a web site and then have your students "source" the file using a url for the file. To be specific, suppose you put your file so that its web address (url) is http://www.simpleR.edu/file-for-my-students.R. Then, students can read this into their session using the following command

> source(file=url("http://www.simpleR.edu/file-for-my-students.R"))
    
That's it. Make sure your students know how important punctuation is. If you have a really long base for your url's, you might suggest to students to define this as a variable so they don't need to type it. Then the paste command can be used. For example

> baseurl = "http://www.simpleR.edu/reallylongbaseurl"
> source(file=url(paste(baseurl,"file-for-my-students.R",sep='')))      
    
Students saving their work
If a student wishes to save their working session they can easily do so.

If a student is always assigned to the same machine or is working with the same account, then when R quits it stores a copy of its session in a file called .Rdata in the current directory. If R is started from that directory it will automatically load this in.

If the students move about and want to take a copy of their work with them, the underlying mechanism is available to them via the function save.image(). For example, if the student is on the windows platform, then the following should save the image to a file on the ``a'' drive in a file called ``rdata.Rd''

> save.image("a:\rdata.Rd")      
    
To load the session back in, the load command is used. This command will restore from the floppy

> load("a:\rdata.Rd")      
    
As well, the menus in windows allow this to be done with a mouse.
Copyright © John Verzani, 2001-2. All rights reserved.

Previous Up Next