# Statistical Analysis: an Introduction using R/Contributors/uploadToCommons.r

``````### Call this function as in the following example:
###
###
### 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
) {

##Here are the defaults for the information template submitted to wikicommons
list(Description=description,
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

if (identical(dummy.run, FALSE)) {
wpName=wikiuser,
wpRemember="1",
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()

##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()",
"```
```

`",`

``````         sep="\n") -> wikiInfoText
```
```
``````   if (identical(dummy.run, FALSE)) {
getURL(paste("http://commons.wikimedia.org/wiki", plotfile, sep="/"), curl=cHandle) -> page2
wpSourceType='file',
wpDestFile=plotfile,
wpIgnoreWarning='true',
wpDestFileWarningAck="1",
curl=cHandle) -> page3
if (length(grep("No file by this name exists", page2)) > 0) #a file existed before
{
```
```
1. `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
```
```

`}`