统计分析:一个使用 R 的介绍/Contributors/uploadToCommons.r
外观
### Call this function as in the following example:
###
### uploadToCommons("username", "password", c("R-example_plot_1.r", R-example_plot_2.r), "svg", 2, 4, 8)
###
### This will generate and upload an svg graphics file called "R-example_plot_1.svg" from the R code specified in "R-example_plot_1.r".
###
### Notes:
### 1) extra arguments (e.g. the "2, 4, 8" above) are passed to the graphics device (here "svg")
### 2) the R code should NOT include a call to a graphics device (e.g. svg(), pdf(), etc.).
###This is generated automatically by the script (within an R session, this allows you to produce an on-screen test plot by using source())
### 3) you can also test the script by specifying "dummy.run=TRUE" in the call, which will attempt to generate a file and display in in a web browser window
uploadToCommons <- function(wikiuser, wikipass, file.names, graphics.func, ...,
description="{{en|1=Unspecified plot generated from R}}",
description.language="en",
plotfile.names=sub(
##make up the plot name by removing ".r" if necessary
##then appending .`graphics.func`
pattern='(\\.r)?$',
replacement=switch(graphics.func,
cairo_pdf=".pdf",
CairoSVG=".svg",
paste(".", graphics.func, sep="")),
x=basename(file.names),
ignore.case=TRUE),
plotfile.contentType=switch(graphics.func,
##Of the R devices, wikicommons allows SVG, PNG, JPEG, PDF
CairoSVG="image/svg+xml",
svg="image/svg+xml",
png="image/png",
jpg="image/jpeg",
pdf="application/pdf"),
dummy.run=FALSE,
view.files.in.browser=dummy.run
) {
require(RCurl) #for the uploading via http
##Here are the defaults for the information template submitted to wikicommons
list(Description=description,
Source="Own work by uploader",
Author=paste("[[User:", wikiuser, "|", wikiuser, "]]", sep=""),
Date=as.character(Sys.Date()),
Permission="",
other_versions="") -> info
save.dir <- getwd() #note that all plots will be saved in your R working directory
getCurlHandle(cookiefile="", cookiejar=file.path(save.dir, "temp.cookie"), ssl.verifyhost=0, ssl.verifypeer=0, followlocation=0) -> cHandle
if (identical(dummy.run, FALSE)) {
#postForm("https://secure.wikimedia.org/wikipedia/commons/w/index.php?title=Special:UserLogin&action=submitlogin&type=login",
postForm("http://commons.wikimedia.org/w/index.php?title=Special:UserLogin&action=submitlogin&type=login",
wpName=wikiuser,
wpPassword=wikipass,
wpRemember="1",
wpLoginAttempt="Log in",
curl=cHandle) -> page1
}
mapply(function(codefile, plotfile) {
##run the R code:
graphics.call <- do.call(call, append(list(graphics.func, file=plotfile), list(...)))
eval(graphics.call)
source(codefile)
dev.off()
code <- readLines(codefile)
##assume that first line, if starting with a "#", is the description
##this code should probably check for disallowed wikitext (e.g. "=" and "}}")
if(substring(code[1], 1, 1)=="#") info$Description <-if(is.null(description.language)) substring(code[1], 2) else paste("{{en|1=", substring(code[1], 2), "}}", sep="")
## construct the wikitext version of the information
paste("{{Information",
paste("|",names(info),"=", info, sep="", collapse="\n"),
"}}",
"{{Created with R}} using the following commands",
"<pre>",
paste(deparse(graphics.call, width.cutoff = 500), collapse=""),
paste(code, collapse="\n"),
"dev.off()",
"</pre>",
sep="\n") -> wikiInfoText
if (identical(dummy.run, FALSE)) {
getURL(paste("http://commons.wikimedia.org/wiki", plotfile, sep="/"), curl=cHandle) -> page2
postForm("http://commons.wikimedia.org/wiki/Special:Upload",
wpUploadFile=fileUpload(file.path(save.dir,plotfile), contentType=plotfile.contentType),
wpSourceType='file',
wpDestFile=plotfile,
wpUploadDescription=wikiInfoText,
wpLicense="PD-self",
wpIgnoreWarning='true',
wpUpload="Upload file",
wpDestFileWarningAck="1",
curl=cHandle) -> page3
if (length(grep("No file by this name exists", page2)) > 0) #a file existed before
{
###TO DO: should overwite original summary here
warning("This file already exists: overwriting (you may need to change the summary information by hand)")
}
} else {
cat(wikiInfoText)
if(view.files.in.browser)
browseURL(paste("file:/",getwd(), plotfile, sep="/"))
}
}, file.names, plotfile.names) -> dummy
rm(cHandle)
gc() #force writing the cookie file
}