Learn R Programming

xfun (version 0.48)

upload_imgur: Upload an image to imgur.com

Description

This function uses the curl package or the system command curl (whichever is available) to upload a image to https://imgur.com.

Usage

upload_imgur(
  file,
  key = env_option("xfun.upload_imgur.key", "9f3460e67f308f6"),
  use_curl = loadable("curl"),
  include_xml = FALSE
)

Value

A character string of the link to the image. If include_xml = TRUE, this string carries an attribute named XML, which is the XML response from Imgur (it will be parsed by xml2 if available). See Imgur API in the references.

Arguments

file

Path to the image file to be uploaded.

key

Client ID for Imgur. It can be set via either the global option xfun.upload_imgur.key or the environment variable R_XFUN_UPLOAD_IMGUR_KEY (see env_option()). If neither is set, this uses a client ID registered by Yihui Xie.

use_curl

Whether to use the R package curl to upload the image. If FALSE, the system command curl will be used.

include_xml

Whether to include the XML response in the returned value.

Author

Yihui Xie, adapted from the imguR package by Aaron Statham

Details

One application is to upload local image files to Imgur when knitting a document with knitr: you can set the knitr::opts_knit$set(upload.fun = xfun::upload_imgur, so the output document does not need local image files any more, and it is ready to be published online.

References

A demo: https://yihui.org/knitr/demo/upload/

Examples

Run this code
if (FALSE) {
f = tempfile(fileext = ".png")
png(f)
plot(rnorm(100), main = R.version.string)
dev.off()

res = imgur_upload(f, include_xml = TRUE)
res  # link to original URL of the image
attr(res, "XML")  # all information
if (interactive())
    browseURL(res)

# to use your own key
options(xfun.upload_imgur.key = "your imgur key")
}

Run the code above in your browser using DataLab