Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ render <- function(input,
else
stop("The shiny package is required for shinyrmd documents")

# source global.R if it exists
global_r <- file.path.ci(".", "global.R")
if (file.exists(global_r)) {
source(global_r, local = envir)
}

# force various output options
output_options$self_contained <- FALSE
output_options$dependency_resolver <- function(deps) {
Expand Down
15 changes: 12 additions & 3 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@
run <- function(file = "index.Rmd", dir = dirname(file), default_file = NULL,
auto_reload = TRUE, shiny_args = NULL, render_args = NULL) {

# if the file argument is missing then substitute ui.Rmd if it exists
# (and index.Rmd does not exist)
if (missing(file) && missing(default_file)) {
if (!file.exists(file.path(dir, "index.Rmd")) &&
file.exists(file.path(dir, "ui.Rmd"))) {
file <- file.path(dir, "ui.Rmd")
}
}

# select the document to serve at the root URL if not user-specified. We exclude
# documents which start with a leading underscore (same pattern is used to
# designate "sub-documents" in R Markdown websites and bookdown)
Expand All @@ -75,8 +84,8 @@ run <- function(file = "index.Rmd", dir = dirname(file), default_file = NULL,
# just one R Markdown document
default_file <- allRmds
} else {
# more than one: look for an index
index <- which(tolower(allRmds) == "index.rmd")
# more than one: look for an index or ui
index <- which(tolower(allRmds) %in% c("index.rmd", "ui.rmd"))
if (length(index) > 0) {
default_file <- allRmds[index[1]]
} else {
Expand All @@ -94,7 +103,7 @@ run <- function(file = "index.Rmd", dir = dirname(file), default_file = NULL,

if (is.null(default_file)) {
# no R Markdown default found; how about an HTML?
indexHtml <- list.files(dir, "index.html?", ignore.case = TRUE)
indexHtml <- list.files(dir, "(index|ui).html?", ignore.case = TRUE)
if (length(indexHtml) > 0) default_file <- indexHtml[1]
}

Expand Down
26 changes: 24 additions & 2 deletions R/shiny_prerendered.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,30 @@ shiny_prerendered_app <- function(input_rmd, render_args) {

# remove server code before serving
server_contexts <- c("server-start", "data", "server")
html_lines <- shiny_prerendered_remove_contexts(html_lines, server_contexts)
html <- HTML(one_string(html_lines))
redacted_html_lines <- shiny_prerendered_remove_contexts(html_lines, server_contexts)

# if there were server contexts then update html w/ removed contexts
if (length(redacted_html_lines) < length(html_lines)) {
html <- HTML(one_string(redacted_html_lines))
}
# if there were not server contexts then this may be a ui-only rmd,
# check for a server.R
else if (file.exists(file.path(dirname(input_rmd), "server.R"))) {
# source global.R onStart
onStart <- function() {
global_r <- file.path.ci(dirname(input_rmd), "global.R")
if (file.exists(global_r)) {
source(global_r, local = FALSE)
}
}
# server function from server.R
server_r <- file.path(dirname(input_rmd), "server.R")
server <- source(server_r, local = FALSE)$value
} else {
stop("No server contexts or server.R available for ", input_rmd)
}

# attach dependencies to final html
html <- htmltools::attachDependencies(html, deps)

# create shiny app
Expand Down