我在Rstudio中使用rmarkdown(1.4)/knitr(1.15.1)创建R markdown HTML页面。我将Shiny合并到文件中以创建交互式文档(通过runtime: shiny
)。
当我尝试从Shiny运行示例时,出现错误:
```{r, eval=TRUE,chunk_title='Shiny_Example'}
runExample("01_hello")
```
如果我在R markdown代码块之外运行代码,则可以使应用程序正常加载(即使在Rstudio的同一 session 中)。
我确实注意到
runExample
代码使用runApp
而不是shinyapp
。这是我问题的根源吗?还是我可能做错了其他事情?
我的代码:
---
title: "Shiny Not Working"
output: html_document
runtime: shiny
---
```{r, eval=TRUE,chunk_title='Shiny_Example'}
library(shiny)
runExample("01_hello")
```
带有堆栈跟踪的完整错误:
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
另请注意:
在此设置下,运行其他 Shiny 的代码可以正常工作:
---
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)
```
note的sessionInfo:
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
最佳答案
因此,我找到了一种解决方法(我将在下面详细介绍)。
但是,我仍然想知道runExample
代码为什么不起作用(即,这是一个错误吗?),以及是否有更正式/更合适的方法来使其起作用。
我的解决方案:
将runExample
中的功能从runApp()
更改为shinyAppDir()
。
原始runExample
代码:
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)
}
}
我的修改代码:
(注意:将最终内部函数更改为
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)
}
}
我意识到我已经失去了将
port
,host
,launch.browser
和display.mode
参数应用于修改后的函数的能力(尽管为了比较起见,我保留了该代码),但是我已经将其换成了可行的解决方案。注意:由于某种原因,我的机器无法识别
resolve()
和resolve的内部函数isWindows()
(即,我收到错误Error: could not find function "resolve"
),因此我使用getAnywhere()
查找代码,然后在创建runExample2
之前手动分配了这些功能。我的完整R Markdown 代码:
---
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")
```