| 1 |
#' Knitr S3 methods |
| 2 |
#' |
| 3 |
#' These S3 methods are necessary to help Shiny applications and UI chunks embed |
| 4 |
#' themselves in knitr/rmarkdown documents. |
| 5 |
#' |
| 6 |
#' @name knitr_methods |
| 7 |
#' @keywords internal |
| 8 |
#' @param x Object to knit_print |
| 9 |
#' @param ... Additional knit_print arguments |
| 10 |
NULL |
| 11 |
|
| 12 |
# If there's an R Markdown runtime option set but it isn't set to Shiny, then |
| 13 |
# return a warning indicating the runtime is inappropriate for this object. |
| 14 |
# Returns NULL in all other cases. |
| 15 |
shiny_rmd_warning <- function() { |
| 16 |
runtime <- knitr::opts_knit$get("rmarkdown.runtime") |
| 17 |
if (!is.null(runtime) && runtime != "shiny") |
| 18 |
# note that the RStudio IDE checks for this specific string to detect Shiny |
| 19 |
# applications in static document |
| 20 |
list(structure( |
| 21 |
"Shiny application in a static R Markdown document", |
| 22 |
class = "rmd_warning")) |
| 23 |
else |
| 24 |
NULL |
| 25 |
} |
| 26 |
|
| 27 |
#' @rdname knitr_methods |
| 28 |
knit_print.shiny.appobj <- function(x, ...) { |
| 29 |
opts <- x$options %||% list() |
| 30 |
width <- if (is.null(opts$width)) "100%" else opts$width |
| 31 |
height <- if (is.null(opts$height)) "400" else opts$height |
| 32 |
|
| 33 |
runtime <- knitr::opts_knit$get("rmarkdown.runtime") |
| 34 |
if (!is.null(runtime) && runtime != "shiny") { |
| 35 |
# If not rendering to a Shiny document, create a box exactly the same |
| 36 |
# dimensions as the Shiny app would have had (so the document continues to |
| 37 |
# flow as it would have with the app), and display a diagnostic message |
| 38 |
width <- validateCssUnit(width) |
| 39 |
height <- validateCssUnit(height) |
| 40 |
output <- tags$div( |
| 41 |
style=paste("width:", width, "; height:", height, "; text-align: center;", |
| 42 |
"box-sizing: border-box;", "-moz-box-sizing: border-box;", |
| 43 |
"-webkit-box-sizing: border-box;"), |
| 44 |
class="muted well", |
| 45 |
"Shiny applications not supported in static R Markdown documents") |
| 46 |
} |
| 47 |
else { |
| 48 |
path <- addSubApp(x) |
| 49 |
output <- deferredIFrame(path, width, height) |
| 50 |
} |
| 51 |
|
| 52 |
# If embedded Shiny apps ever have JS/CSS dependencies (like pym.js) we'll |
| 53 |
# need to grab those and put them in meta, like in knit_print.shiny.tag. But |
| 54 |
# for now it's not an issue, so just return the HTML and warning. |
| 55 |
|
| 56 |
knitr::asis_output(htmlPreserve(format(output, indent=FALSE)), |
| 57 |
meta = shiny_rmd_warning(), cacheable = FALSE) |
| 58 |
} |
| 59 |
|
| 60 |
# Let us use a nicer syntax in knitr chunks than literally |
| 61 |
# calling output$value <- renderFoo(...) and fooOutput(). |
| 62 |
#' @rdname knitr_methods |
| 63 |
#' @param inline Whether the object is printed inline. |
| 64 |
knit_print.shiny.render.function <- function(x, ..., inline = FALSE) { |
| 65 |
x <- htmltools::as.tags(x, inline = inline) |
| 66 |
output <- knitr::knit_print(tagList(x), ..., inline = inline) |
| 67 |
attr(output, "knit_cacheable") <- FALSE |
| 68 |
attr(output, "knit_meta") <- append(attr(output, "knit_meta"), |
| 69 |
shiny_rmd_warning()) |
| 70 |
output |
| 71 |
} |
| 72 |
|
| 73 |
# Lets us drop reactive expressions directly into a knitr chunk and have the |
| 74 |
# value printed out! Nice for teaching if nothing else. |
| 75 |
#' @rdname knitr_methods |
| 76 |
knit_print.reactive <- function(x, ..., inline = FALSE) { |
| 77 |
renderFunc <- if (inline) renderText else renderPrint |
| 78 |
knitr::knit_print(renderFunc({ |
| 79 |
x() |
| 80 |
}), ..., inline = inline) |
| 81 |
} |