Tag Archives: sink()

Helping the blind use R – by exporting R console to Word

Preface – R seems a natural fit for the blind statistician

For blind people who wish to do statistics, R can be ideal. R command line interface offers straight forward statistical scripting in the form of question (what is the mean of x) followed by an answer (0.2). That is, instead of point-and-click dialog boxes with jumping windows of results that GUI statistical systems offer.

But there are still more hurdles to face before R can offer a perfect solution to the blind.
In this post I would like to address just one such problem – reading R console output.

Directing R console output to word – to allow blind people to easily navigate in it

Recently, a question was posed in the R-help mailing list by a guy names Faiz, a blind new user of R. Faiz wants to direct R output into word, to allow him to be able to read it. Here is what he wrote:

I would like to read the results of the commands type in the terminal window in Microsoft Word. As a blind user my options are somewhat limited and are time consuming if I want to see the results of the commands that I have type earlier. for example if my first two commands were
x<-c(1,2,3,4,5)
mean(x)
and I have typed ten more commands after the first two commands it is not easy for me to see that what was the result of mean(x)
but if I can somehow divert the results of the commands to Microsoft Word it is comparatively easy for me to see what was the result of mean(x) and what were the results of other commands. One another advantage of diverting R's output to Microsoft Word for me is that from there they can be easily copied into assignments as well.

Faiz later elaborated more on his issue:

I am using Windows XP, and using a screen reader called JAWS. When I type something at the console, I hear once what I have typed, and then the focus is on the next line. Then if I press the up arrow key I get to hear the function I just typed, not its output. For example if I type mean(x) and then I press enter I will hear “[5]” if it is the mean of x. Then I will hear “>”. Now if I want to find out what was the mean of x by pressing the
up arrow key, I will only hear mean(x) and I will not hear [5].
My screen reader does provide options to use different cursors to read command lines.
but if I have typed median(x) sd(x) var(x) length(x) after typing mean(x), it takes a long time before I can move my cursor to the location where I can hear the mean of x. If the results of the commands can be diverted to MS Word it becomes comparatively easy for me to quickly move forward and backward in the document.

Any ideas and suggestions are appreciated.

Since recently I reviewed how one could export R output to MS-Word with R2wd, It was only fitting to try and implement R2wd for this problem.
I went looking on how to direct R console into a txt file, so I could later dump it into word. I found that two commands gave me half of what I wanted. sink() allows me to direct R output to a txt file, and savehistory() can save the command history into a txt file. But I needed something that combines the two and captures all of R console output into a file.
Failing to locate one, I turned to the R mailing list. Among the kind people trying to help (Thank you David Winsemius, Bert Gunter and Duncan Murdoch) Greg Snow came through in supplying the help (not surprisingly…).
Greg directed me to a function he wrote called txtStart() (from the TeachingDemos package), which operates in a similar way as sink(), only it also captures the R commands that where used – exactly what I was looking for!

Based on this, I devised two functions that can be used to redirect R output into word.

Here is how to use them:

# Step 1: reading the functions needed for this task, from the file I uploaded to www.r-statistics.com
source("http://www.r-statistics.com/wp-content/uploads/2010/05/R-console-to-word.r.txt")
# Example:
# Step 2 - start capturing
txtStart.2wd()	# start capturing text.  If you are missing any packages - this function will prompt you to install them
				# IF the installation fails - consider changing your mirror location
# Step 3 - run R code
	date()
	x <- rnorm(25)
	mean(x)
	var(x)
	Sys.Date()
	Sys.time()
# Step 4 - close connection - print output to word
txtStop.2wd(T)	# This closes the capturing of the output.  And writes it into a new word file.
				# if this is the first time in your session you are using this function, you should pass the
				# "T" paramater to the function so it will open a new document and connect to it
				# IF the doc is already open, the paramater should be "F", as it will soon be demonstrated.
 
# Step 5, adding some more text to that doc file
txtStart.2wd()	# start capturing text.  
# Code to run:
stem(x)	# stem offers a text alternative to a histogram 
txtStop.2wd(F)	# This closes the capturing of the output.  Notice we use "F" as paramater in the function - since we alread have an open doc file

For me, this worked…

If you would like R to automatically run in the startup the code needed to get the two functions: txtStart.2wd and txtStop.2wd , you can run this in your R console: (once is enough)

# Start of code
Rprofile.site.loc <- paste(R.home(), "\\etc\\Rprofile.site", sep = "")
cat(paste('\n', 'source("http://www.r-statistics.com/wp-content/uploads/2010/05/R-console-to-word.r.txt")', '\n') ,  # You could also put source() with a link to a local copy of the source code.
file = Rprofile.site.loc, append = T)
# End of code

Bringing R to the blind: there is much more work a head!

Until this point, it didn’t cross my mind to ask how can R be used by the blind. But once this question was raised – it brings with it many more questions.
Can R be adjusted to easily be read by known aids to sight impaired people? (I am sure Linux users here will have much to add)
Can people in the community think of writing function to turn R output into a more easily read text for the blind?
For example – the summary() command is wonderful for me. But I am trying to imagine how it would look like in the “eyes” of a person who can’t see. Surly there could be some way to turn the wide summary format into a long format.
Perhaps there is room for a more general approach to the question of how to help blind people to be able to use R.
And is there a need? How many blind people choose to pursue studying statistics (or disciplines for which they would need to know statistics/R)?
I hope to read your thoughts on the matter.

On a personal note: My father was on the verge of blindness, prior to his cataract surgery. I saw first hand how the life of the sight-impaired can look like. Giving people in that situation help is a great MITZVA (a.k.a: “good deed” in Hebrew).