Skip to content

Commit

Permalink
knitr::imgur_upload() is now simply a wrapper function of `xfun::up…
Browse files Browse the repository at this point in the history
…load_imgur()` (#2325)
  • Loading branch information
yihui authored Mar 28, 2024
1 parent ed80400 commit f5f2e7b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 107 deletions.
4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ Suggests:
gifski,
gridSVG,
htmlwidgets (>= 0.7),
curl,
jpeg,
JuliaCall (>= 0.11.1),
magick,
Expand All @@ -152,8 +151,7 @@ Suggests:
tinytex (>= 0.46),
webshot,
rstudioapi,
svglite,
xml2 (>= 1.2.0)
svglite
License: GPL
URL: https://yihui.org/knitr/
BugReports: https://github.com/yihui/knitr/issues
Expand Down
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

- Added a new chunk option `tab.cap` to specify the table caption for `kable()` (thanks, @ulyngs, #1679). Previously, the caption could only be specified via the `caption` argument of `kable()`. Now you can set it in the chunk header if you want. Please note that this chunk option only works with a single `kable()` in each code chunk, and its value must be of length 1.

- `knitr::spin()` now recognizes `# %%` as a valid code chunk delimiter (thanks, @kylebutts, #2307).
- `spin()` now recognizes `# %%` as a valid code chunk delimiter (thanks, @kylebutts, #2307).

- `knitr::spin()` also recognizes `#|` comments as code chunks now (thanks, @kylebutts, #2320).
- `spin()` also recognizes `#|` comments as code chunks now (thanks, @kylebutts, #2320).

- Chunk hooks can have the `...` argument now. Previously, only arguments `before`, `options`, `envir`, and `name` are accepted. If a chunk hook function has the `...` argument, all the aforementioned four arguments are passed to the hook. This means the hook function no longer has to have the four arguments explicitly in its signature, e.g., `function(before, ...)` is also valid if only the `before` argument is used inside the hook. See <https://yihui.org/knitr/hooks/#chunk-hooks> for more information.

Expand Down Expand Up @@ -36,6 +36,8 @@

- SQL code chunks that run `ALTER` statements are only executed and not tried to fecth a result (thanks, @maxschmi, #2330).

- The function `imgur_upload()` has been moved to (and enhanced in) the **xfun** package as `xfun::upload_imgur()` so it is no longer tied to **knitr** and can be reused by other pakages. Now `knitr::imgur_upload()` is only a wrapper function of `xfun::upload_imgur()`. You are recommended to use the latter (#2325).

# CHANGES IN knitr VERSION 1.45

## NEW FEATURES
Expand Down
59 changes: 7 additions & 52 deletions R/utils-upload.R
Original file line number Diff line number Diff line change
@@ -1,56 +1,11 @@
#' Upload an image to imgur.com
#'
#' This function uses the \pkg{curl} package to upload a image to
#' \url{https://imgur.com}, and parses the XML response to a list with
#' \pkg{xml2}, which contains information about the image on Imgur.
#'
#' When the output format from \code{\link{knit}()} is HTML or Markdown, this
#' function can be used to upload local image files to Imgur, e.g. set the
#' package option \code{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.
#' @param file Path to the image file to be uploaded.
#' @param key Client ID for Imgur. It can be set via either the global option
#' \code{knitr.imgur.key} or the environment variable \code{R_KNITR_IMGUR_KEY}
#' (see [xfun::env_option()]). If neither is set, this uses a client ID
#' registered by Yihui Xie.
#' @return A character string of the link to the image; this string carries an
#' attribute named \code{XML} which is a list converted from the response XML
#' file; see Imgur API in the references.
#' @author Yihui Xie, adapted from the \pkg{imguR} package by Aaron Statham
#' @note Please register your own Imgur application to get your client ID; you
#' can certainly use mine, but this ID is in the public domain so everyone has
#' access to all images associated to it.
#' @references A demo: \url{https://yihui.org/knitr/demo/upload/}
#' This function is an alias to \code{xfun::upload_imgur()}. It is kept in
#' \pkg{knitr} only for backward-compatibility reasons. You are recommended to
#' use \code{xfun::upload_imgur()} directly instead.
#' @param file,key,... See \code{xfun::\link[xfun]{upload_imgur}()}.
#' @export
#' @examples \dontrun{
#' 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
#' options(knitr.imgur.key = 'your imgur key')
#' }
imgur_upload = function(file, key = xfun::env_option('knitr.imgur.key', '9f3460e67f308f6')) {
if (!is.character(key)) stop('The Imgur API Key must be a character string!')
h = curl::new_handle(httpheader = paste("Authorization: Client-ID", key))
curl::handle_setform(h, image = curl::form_file(file))
res = curl::curl_fetch_memory('https://api.imgur.com/3/image.xml', h)$content
if (loadable('xml2')) {
res = xml2::as_list(xml2::read_xml(res))
link = res[[1]]$link[[1]]
} else {
res = rawToChar(res)
link = xfun::grep_sub('.*<link>([^<]+)</link>.*', '\\1', res)
}
if (length(link) != 1) stop(
'Failed to upload ', file, sprintf(' (reason: %s)', if (is.character(res)) {
xfun::grep_sub('.*<error>([^<]+)</error>.*', '\\1', res)
} else res[[1]]$error[[1]])
)
structure(link, XML = res)
#' @keywords internal
imgur_upload = function(file, key = xfun::env_option('knitr.imgur.key', '9f3460e67f308f6'), ...) {
xfun::upload_imgur(file, key, ..., include_xml = TRUE)
}
57 changes: 7 additions & 50 deletions man/imgur_upload.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f5f2e7b

Please sign in to comment.