Weather forecast and good development practices

November 16, 2011

(This article was first published on One R Tip A Day, and kindly contributed to R-bloggers)

Inspired by this tutorial, I thought that it would be nice to have the possibility to have access to weather forecast directly from the R command line, for example for a personalized start-up message such as the one below:

Weather summary for Trieste, Friuli-Venezia Giulia:
The weather in Trieste is clear. The temperature is currently 14°C (57°F). Humidity: 63%.

Fortunately, thanks to the always useful Duncan Temple Lang’s XML package (see here for a tutorial about XML programming under R), it is straightforward to write few lines of R code to invoke the google weather api for the location of interest, retrieve the XML file, parse it using the XPath paradigm and get the required informations:

url = paste( "", URLencode(address), sep="" )
xml = xmlTreeParse(url, useInternalNodes=TRUE) # take a look at the xml output:
# Get the required informations:
cat( paste("The Weather in ", address, " is ", condition, ". The temperature is ", temp_c, "°C. Humidity is ", humidity, "%.") )

Times ago I came to the conclusion that the best way to organize my R code is to create packages even for basic tasks. I know that It seems too much effort for this trivial task (and it was in the past) but fortunately, thanks to the Hadley Wickham’s devtools package development It has become a piece of cake process (sort of)!

Below I present the minimal workflow I used to create this simple package. For a proper introduction to package development using devtools take a look at this link.

First create the skeleton for the project using the package.skeleton() function:


Read ‘./pkg/Read-and-delete-me’ file, compile the DESCRIPTION fiels according to your needs and delete ‘./pkg/Read-and-delete-me’.
Now the devtools magic:

pkg <- as.package("pkg") # pkg is the directory containing the structure created using package.skeleton()

Create your functions and documentation following the roxygen literate programming paradigm: basically you write your functions together with its documentation using in the preamble tags such as @param, @example, etc. to indicate the different constituents of the functions and devtools automagically will create the functions’ documentation (.Rd files).
Then you test your code, try your examples, verify that your package passes the check without errors and warnings, build it and, if you like, you can ftp it directly to CRAN (disclaimer: I didn’t check this feature)!

load_all(pkg, reset=T) # to reload the package without having to restart R
document(pkg) # to be used together with roxygen2 to creating the corresponding Rd files
run_examples(pkg) # to check the examples for the different functions
devtools:::check(pkg) # to verified if your package raises errors or warnings
install(pkg) # install your package
# release()

Final consideration: the devtools package improved significantly my day-by-day workflow and I want to thank Hadley Wickham for this and all the other valuable packages he gifted the R community! 
P.S. If you like to install the RWeather package I created using devtools, you can do it by typing:

install.packages("RWeather", repos="")

or download the source code from here.
P.S.2 I’d like to thank Kay Cichini for this post which explains how to set-up the syntax-highlighting for the R code on Blogger.

Update: Thanks to the useful info I got from this Python module, now RWeather can show weather information from Yahoo! Weather, Google Weather and NOAA APIs.
From now the stable version of the package can be installed directly from CRAN:


To leave a comment for the author, please follow the link and comment on their blog: One R Tip A Day. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training


CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)