I'm using rmarkdown (1.4) / knitr (1.15.1) in Rstudio to create an R markdown HTML page. I'm incorporating shiny into the file to make an interactive doc (via runtime: shiny
).
When I try to run an example from Shiny, I get an error:
```{r, eval=TRUE,chunk_title='Shiny_Example'}
runExample("01_hello")
```
Error: can't call
runApp()
from withinrunApp()
. If your application code containsrunApp()
, please remove it.
If I run the code outside of an R markdown code chunk, I can get the app to load just fine (even in the same session of Rstudio).
I did notice that the runExample
code uses runApp
and not shinyapp
.
Is this the root of my problem? Or am I possibly doing something else wrong??
My Code:
---
title: "Shiny Not Working"
output: html_document
runtime: shiny
---
```{r, eval=TRUE,chunk_title='Shiny_Example'}
library(shiny)
runExample("01_hello")
```
Full Error w/ Stack Trace:
Warning: Error in runApp: Can't call `runApp()` from within `runApp()`.
If your application code contains `runApp()`, please remove it.
Stack trace (innermost first):
117: runApp
116: runExample
115: eval
114: eval
113: withVisible
112: withCallingHandlers
111: handle
110: timing_fn
109: evaluate_call
108: evaluate::evaluate
107: evaluate
106: in_dir
105: block_exec
104: call_block
103: process_group.block
102: process_group
101: withCallingHandlers
100: process_file
99: knitr::knit
98: <Anonymous>
97: do.call
96: contextFunc
95: .getReactiveEnvironment()$runWith
94: shiny::maskReactiveContext
93: <reactive>
82: doc
81: shiny::renderUI
80: func
79: origRenderFunc
78: output$__reactivedoc__
3: <Anonymous>
2: do.call
1: rmarkdown::run
Also Note:
Running other shiny code works fine with this set-up:
---
title: "Shiny Not Working"
output: html_document
runtime: shiny
---
```{r, eval=TRUE,chunk_title='Simple_Shiny'}
library(shiny)
ui <- fluidPage()
server <- function(input, output){}
shinyApp(ui = ui, server = server)
```
sessionInfo of note:
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
shiny_1.0.1 rmarkdown_1.4 knitr_1.15.1
So I found a workaround solution (which I'll detail below).
However, I would still like to know why the runExample
code doesn't work (i.e., is this a bug??) and whether there is a more official/proper way to go about making it work.
Change the function in runExample
from runApp()
to shinyAppDir()
.
Original runExample
code:
function (example = NA, port = NULL, launch.browser = getOption("shiny.launch.browser",
interactive()), host = getOption("shiny.host", "127.0.0.1"),
display.mode = c("auto", "normal", "showcase"))
{
examplesDir <- system.file("examples", package = "shiny")
dir <- resolve(examplesDir, example)
if (is.null(dir)) {
if (is.na(example)) {
errFun <- message
errMsg <- ""
}
else {
errFun <- stop
errMsg <- paste("Example", example, "does not exist. ")
}
errFun(errMsg, "Valid examples are \"", paste(list.files(examplesDir),
collapse = "\", \""), "\"")
}
else {
runApp(dir, port = port, host = host, launch.browser = launch.browser,
display.mode = display.mode)
}
}
My edited code:
(note: change in final internal function to shinyAppDir
)
runExample2 <- function (example = NA, port = NULL, launch.browser = getOption("shiny.launch.browser",
interactive()), host = getOption("shiny.host", "127.0.0.1"),
display.mode = c("auto", "normal", "showcase"))
{
examplesDir <- system.file("examples", package = "shiny")
dir <- resolve(examplesDir, example)
if (is.null(dir)) {
if (is.na(example)) {
errFun <- message
errMsg <- ""
}
else {
errFun <- stop
errMsg <- paste("Example", example, "does not exist. ")
}
errFun(errMsg, "Valid examples are \"", paste(list.files(examplesDir),
collapse = "\", \""), "\"")
}
else {
shinyAppDir(appDir = dir)
}
}
I realize I've lost the ability to apply port
, host
, launch.browser
, and display.mode
arguments to my modified function (though I left that code in for comparison sake), but I've traded that for a working solution.
Note: for some reason neither resolve()
nor resolve's internal function isWindows()
was recognized by my machine (i.e., I got the error Error: could not find function "resolve"
), so I used getAnywhere()
to find the code and then assigned these functions manually before creating runExample2
.
My full WORKING r markdown code:
---
title: "Shiny Not working"
date: "Today"
output: html_document
runtime: shiny
---
```{r, eval=TRUE,chunk_title='Shiny_Example'}
library(shiny)
isWindows <- function ()
.Platform$OS.type == "windows"
resolve <- function (dir, relpath)
{
abs.path <- file.path(dir, relpath)
if (!file.exists(abs.path))
return(NULL)
abs.path <- normalizePath(abs.path, winslash = "/", mustWork = TRUE)
dir <- normalizePath(dir, winslash = "/", mustWork = TRUE)
if (isWindows())
dir <- sub("/$", "", dir)
if (nchar(abs.path) <= nchar(dir) + 1)
return(NULL)
if (substr(abs.path, 1, nchar(dir)) != dir || substr(abs.path,
nchar(dir) + 1, nchar(dir) + 1) != "/") {
return(NULL)
}
return(abs.path)
}
runExample2 <- function (example = NA, port = NULL, launch.browser = getOption("shiny.launch.browser",
interactive()), host = getOption("shiny.host", "127.0.0.1"),
display.mode = c("auto", "normal", "showcase"))
{
examplesDir <- system.file("examples", package = "shiny")
dir <- resolve(examplesDir, example)
if (is.null(dir)) {
if (is.na(example)) {
errFun <- message
errMsg <- ""
}
else {
errFun <- stop
errMsg <- paste("Example", example, "does not exist. ")
}
errFun(errMsg, "Valid examples are \"", paste(list.files(examplesDir),
collapse = "\", \""), "\"")
}
else {
shinyAppDir(appDir = dir
)
}
}
runExample2("01_hello")
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With