· r-2

R: Command line - Error in GenericTranslator$new : could not find function "loadMethod"

I’ve been reading Text Processing with Ruby over the last week or so and one of the ideas the author describes is setting up your scripts so you can run them directly from the command line.

I wanted to do this with my Wimbledon R script and wrote the following script which uses the 'Rscript' executable so that R doesn’t launch in interactive mode:

wimbledon

#!/usr/bin/env Rscript

library(rvest)
library(dplyr)
library(stringr)
library(readr)

# stuff

Then I tried to run it:

$ time ./wimbledon

...

Error in GenericTranslator$new : could not find function "loadMethod"
Calls: write.csv ... html_extract_n -> <Anonymous> -> Map -> mapply -> <Anonymous> -> $
Execution halted

real	0m1.431s
user	0m1.127s
sys	0m0.078s

As the error suggests, the script fails when trying to write to a CSV file - it looks like Rscript doesn’t load in something from the core library that we need. It turns out adding the following line to our script is all we need:

library(methods)

So we end up with this:

#!/usr/bin/env Rscript

library(methods)
library(rvest)
library(dplyr)
library(stringr)
library(readr)

And when we run that all is well!

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket