Visualizzazione post con etichetta Google. Mostra tutti i post
Visualizzazione post con etichetta Google. Mostra tutti i post

mercoledì 16 novembre 2011

Weather forecast and good development practices

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:

address="Trieste"
url = paste( "http://www.google.com/ig/api?weather=", URLencode(address), sep="" )
xml = xmlTreeParse(url, useInternalNodes=TRUE) # take a look at the xml output:
# Get the required informations:
condition=xpathSApply(xml,"//xml_api_reply/weather/current_conditions/condition",xmlGetAttr,"data")
temp_c=xpathSApply(xml,"//xml_api_reply/weather/current_conditions/temp_c",xmlGetAttr,"data")
humidity=xpathSApply(xml,"//xml_api_reply/weather/current_conditions/humidity",xmlGetAttr,"data")
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:
package.skeleton("pkg")
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:
library("devtools")
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
devtools:::build(pkg)
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="http://R-Forge.R-project.org")
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:
install.packages("RWeather")

lunedì 8 novembre 2010

A R wrapper for Google Prediction API

Since I got the chance to access to both Google Storage for Developers and Google Prediction API (more details here and here), I decided to create a simple wrapper (just 4 basic functions until now) to be capable to play with the Google Prediction API from R.
Here you can find the github repository for the project and below few lines of code reproducing an example you can find on the Google Prediction API website.

Download the source code from here.
Either source the functions contained in the R directory or install the package typing (from the command line in a Unix-like environment):
R CMD INSTALL predictionapirwrapper_1.0.tar.gz
# start R and type (code highlighting thanks to Revolution Analytics Pretty R syntax highlighter):
library(predictionapirwrapper)
## The first stage of using the API is to acquire an authorization token. This can be done via this command:
token <- GetAuthToken(email="user@gmail.com", passwd="mypassword")
## This command begins training on data that has been previously uploaded to Google Storage.
GoogleTrain(auth_token=token$Auth, mybucket="data_languages", mydata="language_id.txt")
## Once training has started, this command checks the status of the training job and gets meta-information on the model (if available).
GoogleTrainCheck(auth_token=token$Auth, mybucket="data_languages", mydata="language_id.txt")
## When training has finished, this command issues a request for a new prediction from the model. 
GooglePredict(auth_token=token$Auth, mybucket="data_languages", mydata="language_id.txt", myinput="La idioma mas fina")

All comments, corrections, alternative code are more than welcome!

Update: a more complete and functional alternative can be found here.

mercoledì 5 agosto 2009

Locate the position of CRAN mirror sites on a map using Google Maps

Inspired by this post (suggested here by the always useful Revolutions blog), I attempted to plot the position of CRAN mirrors on a map taking advantage of the nice R package RgoogleMaps (check the dependencies!). Below the code:

library(XML)
# download.file("http://www.maths.lancs.ac.uk/~rowlings/R/Cranography/cran.gml",destfile="cran.gml")
cran.gml <- xmlInternalTreeParse("cran.gml")
# Create a data.frame assembling all the information from the gml file
Name <- sapply(getNodeSet(cran.gml, "//ogr:Name"), xmlValue)
Country <- sapply(getNodeSet(cran.gml, "//ogr:Country"), xmlValue)
City <- sapply(getNodeSet(cran.gml, "//ogr:City"), xmlValue)
URL <- sapply(getNodeSet(cran.gml, "//ogr:URL"), xmlValue)
Host <- sapply(getNodeSet(cran.gml, "//ogr:Host"), xmlValue)
Maintainer <- sapply(getNodeSet(cran.gml, "//ogr:Maintainer"), xmlValue)
CountryCode <- sapply(getNodeSet(cran.gml, "//ogr:countryCode"), xmlValue)
lng <- as.numeric(sapply(getNodeSet(cran.gml, "//ogr:lng"), xmlValue))
lat <- as.numeric(sapply(getNodeSet(cran.gml, "//ogr:lat"), xmlValue))
cran.mirrors <- data.frame(Name, Country, City, URL, Host, Maintainer, CountryCode, lng, lat)
# cran.mirrors <- cbind(getCRANmirrors(), lng, lat) ## alternatively
library(RgoogleMaps)
# Define the markers:
cran.markers <- cbind.data.frame( lat=cran.mirrors$lat, lon=cran.mirrors$lng, 
size=rep('tiny', length(cran.mirrors$lat)), col=colors()[1:length(cran.mirrors$lat)], 
char=rep('',length(cran.mirrors$lat)) )
# Get the bounding box:
bb <- qbbox(lat = cran.markers[,"lat"], lon = cran.markers[,"lon"])
num.mirrors <- 1:dim(cran.markers)[1] ## to visualize only a subset of the cran.mirrors
maptype <- c("roadmap", "mobile", "satellite", "terrain", "hybrid", "mapmaker-roadmap", "mapmaker-hybrid")[1]
# Download the map (either jpg or png): 
MyMap <- GetMap.bbox(bb$lonR, bb$latR, destfile = paste("Map_", maptype, ".png", sep=""), GRAYSCALE=F, maptype = maptype)
# Plot:
png(paste("CRANMirrorsMap_", maptype,".png", sep=""), 640, 640)
tmp <- PlotOnStaticMap(MyMap,lat = cran.markers[num.mirrors,"lat"], lon = cran.markers[num.mirrors,"lon"], 
cex=1, pch="R",col=as.numeric(cran.mirrors$Country), add=F)
dev.off()


## Hosts from Italy
maptype <- c("roadmap", "mobile", "satellite", "terrain", "hybrid", "mapmaker-roadmap", "mapmaker-hybrid")[4]
num.it <- row.names(cran.mirrors[cran.mirrors$CountryCode=="IT",])
# Get the bounding box:
bb.it <- qbbox(lat = cran.markers[num.it,"lat"], lon = cran.markers[num.it,"lon"])
# Download the map (either jpg or png):
ITMap <- GetMap.bbox(bb.it$lonR, bb.it$latR, destfile = paste("ITMap_", maptype, ".png", sep=""), GRAYSCALE=F, maptype = maptype)
#ITMap <- GetMap.bbox(bb.it$lonR, bb.it$latR, destfile = paste("ITMap_", maptype, ".jpg", sep=""), GRAYSCALE=F, maptype = maptype)
# Plot:
png(paste("CRANMirrorsMapIT_", maptype,".png", sep=""), 640, 640);
tmp <- PlotOnStaticMap(ITMap,lat = cran.markers[num.it,"lat"], lon = cran.markers[num.it,"lon"], 
cex=2, pch="R",col="dodgerblue", add=F)
# tmp <- PlotOnStaticMap(ITMap,lat = cran.markers[num.it,"lat"], lon = cran.markers[num.it,"lon"],labels=as.character(cran.mirrors[cran.mirrors$CountryCode=="IT",]$Host),col="black", FUN=text, add=T)
dev.off()


CAVEAT: To reproduce the example you need the gml file you can download from here , a  Google account and a Google Maps API key. Here you can sign up for a free API key.