From 24a71d021fa7ac951700029b9d0a9a7c7a9e7e56 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Wed, 17 Dec 2014 22:47:38 -0600 Subject: [PATCH] guard against renderMarkdown(text = character(0) or '') (yihui/knitr#918) --- R/renderMarkdown.R | 8 ++++++++ man/renderMarkdown.Rd | 3 +++ 2 files changed, 11 insertions(+) diff --git a/R/renderMarkdown.R b/R/renderMarkdown.R index cdd2f44..79571a3 100644 --- a/R/renderMarkdown.R +++ b/R/renderMarkdown.R @@ -94,6 +94,9 @@ rendererOutputType <- function(name) { #' @export renderMarkdown #' @examples #' (renderMarkdown(text = "Hello World!")) +#' # a few corner cases +#' (renderMarkdown(text = character(0))) +#' (renderMarkdown(text = '')) renderMarkdown <- function( file, output = NULL, text = NULL, renderer = 'HTML', renderer.options = NULL, extensions = getOption('markdown.extensions'), encoding = getOption('encoding') @@ -125,6 +128,11 @@ renderMarkdown <- function( stop('HTML options must be a character vector') } + if (length(text) == 0 || text == '') { + if (is.null(output)) return(invisible(character(length(text)))) + file.create(output) + return() + } ret <- .Call(rmd_render_markdown, file, output, text, renderer, renderer.options, extensions) diff --git a/man/renderMarkdown.Rd b/man/renderMarkdown.Rd index c982b04..929ad27 100644 --- a/man/renderMarkdown.Rd +++ b/man/renderMarkdown.Rd @@ -46,6 +46,9 @@ GitHub. C stubs are available to implement new renderers. } \examples{ (renderMarkdown(text = "Hello World!")) +# a few corner cases +(renderMarkdown(text = character(0))) +(renderMarkdown(text = "")) } \seealso{ \code{\link{markdownExtensions}}, \code{\link{markdownHTMLOptions}},