This function uses the httr package to upload a image to imgur.com, and parses the XML response to a list with xml2 which contains information about the image in the Imgur website.
imgur_upload(file, key = "9f3460e67f308f6")
Path to the image file to be uploaded.
Client ID for Imgur. By default, this uses a client ID registered by Yihui Xie.
A character string of the link to the image; this string carries an
attribute named XML
which is a list converted from the response XML
file; see Imgur API in the references.
When the output format from knit()
is HTML or Markdown, this
function can be used to upload local image files to Imgur, e.g. set the
package option opts_knit$set(upload.fun = imgur_upload)
, so the output
document is completely self-contained, i.e. it does not need external image
files any more, and it is ready to be published online.
Imgur API version 3: https://apidocs.imgur.com; a demo: https://yihui.name/knitr/demo/upload/
# NOT RUN {
f = tempfile(fileext = ".png")
png(f)
plot(rnorm(100), main = R.version.string)
dev.off()
res = imgur_upload(f)
res # link to original URL of the image
attr(res, "XML") # all information
if (interactive())
browseURL(res)
# to use your own key
opts_knit$set(upload.fun = function(file) imgur_upload(file, key = "your imgur key"))
# }
Run the code above in your browser using DataLab