21 Apr 2013

Programmatically Download CORINE Land Cover Seamless Vector Data with R

Thanks to a helpful SO-Answer I was able to download all CLC vector data (43 zip-files) programmatically:








require(XML)

path_to_files <- "D:/GIS_DataBase/CorineLC/Seamless"
dir.create(path_to_files)
setwd(path_to_files)

doc <- htmlParse("http://www.eea.europa.eu/data-and-maps/data/clc-2006-vector-data-version-2")
urls <- xpathSApply(doc,'//*/a[contains(@href,".zip/at_download/file")]/@href')

# function to get zip file names
get_zip_name <- function(x) unlist(strsplit(x, "/"))[grep(".zip", unlist(strsplit(x, "/")))]

# function to plug into sapply
dl_urls <- function(x) try(download.file(x, get_zip_name(x), mode = "wb"))

# download all zip-files
sapply(urls, dl_urls)

# function for unzipping
try_unzip <- function(x) try(unzip(x))

# unzip all files in dir and delete them afterwards
sapply(list.files(pattern = "*.zip"), try_unzip)

# unlink(list.files(pattern = "*.zip"))

18 Apr 2013

Use ZOTERO Bibliographies with Custom CSL Citation Styles in MS Word Processor

Just discovered this nice online CSL editor which is very handy for plugging together your desired citation styles. With Zotero's Word Plug-In for Firefox (Tutorial) you can nicely insert intext citation and bibliographies in an existing or tailored citation style.

12 Apr 2013

Download File from Google Drive/Docs Programmatically with R

Following up my lattest posting on how to download files from the cloud with R..

dl_from_GoogleD <- function(output, key, format) {

## Arguments:
## output = output file name
## key = Google document key
## format = output format (pdf, rtf, doc, txt..)
## Note: File must be shareable!

                        require(RCurl)
                        bin <- getBinaryURL(paste0("https://docs.google.com/document/d/", key, "/export?format=", format),
                                            ssl.verifypeer = FALSE)
                        con <- file(output, open = "wb")
                        writeBin(bin, con)
                        close(con)
                        message(noquote(paste(output, "read into", getwd())))                        
                        }


# Example:
dl_from_GoogleD(output = "dl_test.pdf", 
                key = "1DdauvkcVm5XtRBkQIv1na8PeLAwpCBdW8pALCFpRWeM",
                format = "pdf")
shell.exec("dl_test.pdf")
EDIT: Here's how it can be done for spreadsheet-like data, like HERE, which is a comma seperated file with .txt extension saved to Google Drive. See also this post
library(RCurl)
setwd(tempdir())
destfile = "test_google_docs.csv"
x = getBinaryURL("https://docs.google.com/uc?export=download&id=0B2wAunwURQNsR0I0a0NlQUlJdzA", followlocation = TRUE, ssl.verifypeer = FALSE)
writeBin(x, destfile, useBytes = TRUE)
shell.exec(paste(tempdir(), "/test_google_docs.csv", sep = ""))

10 Apr 2013

Tweaking Movie Subtitles with R

I use R to fix subtitles that are not in sync with my movies. For the example below the subs were showing too early - so I added some time to each sequence in the srt file. For simplicity I used exactly 1 second in the below example.
You'll see that I use my function dl_from_dropbox(), on which I wrote a post previously, to get the example file!



Download Files from Dropbox Programmatically with R

Here is a usefull snippet that I stole from qdap::url_dl to download files from my Dropbox to the working directory.
Argument x is the document name and d the document key.

dl_from_dropbox <- function(x, key) {
                        require(RCurl)
                        bin <- getBinaryURL(paste0("https://dl.dropboxusercontent.com/s/", key, "/", x),
                                            ssl.verifypeer = FALSE)
                        con <- file(x, open = "wb")
                        writeBin(bin, con)
                        close(con)
                        message(noquote(paste(x, "read into", getwd())))                        
                        }
# Example:
dl_from_dropbox("GViewer_Embeds.txt", "06fqlz6gswj80nj")
shell.exec("GViewer_Embeds.txt")
PS: Also see this R-package for interfacing with Dropbox

5 Apr 2013

Embedding Google Docs and Google Viewer

..Here's a short example of the usage of the Google Viewer and Google Docs embeds (see here). If you want to embed a view of any file on Google Drive/Docs you can do this by putting a pdf preview (the file could be in any format, see first example, which is a Google text document) in an iframe or, in the case of spreadsheets, the speradsheet itself into an iframe.

The second embed below shows the latest outcome of an online questionnaire and in the speradsheet that holds the result the new answers to the questoinnaire are updated every 5 minutes after refreshing the view. So, you can, i.e., make an online questoinnaire with a Google form and display the results like below. With a little tweaking of the parameters, which I obviously haven't done yet, the result would of course be more visually appealing.

If you try for one of your files you just need to replace the id/key in the URL..





The Result:



2 Apr 2013

VBS Shutdown Timer

When running time comsuming processes I often wish to shutdown automatically after a given time. There are several applications on the internet but I was not really happy with the ones I found. So, I put together a really small VB-Script which in fact does the job perfectly.

Dim Shell : Set Shell = CreateObject("WScript.Shell")

argument_t = inputbox("Time in Seconds:", "Shutdown", "3600")

Shell.Run "C:/Windows/system32/shutdown.exe -f -s -t " & _
          argument_t, 0

MsgBox "Press OK if you want to cancel the Shutdown..", _
       vbInformation + vbOkayonly, "Cancel Shutdown"

Shell.Run "C:/Windows/system32/shutdown.exe -a", 0

I put the vbs file on my disk and a shortcut to it on the Desktop. You can choose the time until shutdown with an input box.