我在Rstudio中使用rmarkdown(1.4)/ knitr(1.15.1)来创建一个R markdown HTML页面。我将闪亮的内容整合到文件中以制作交互式文档(通过runtime: shiny
)。
当我尝试从Shiny运行示例时,我收到错误:
```{r, eval=TRUE,chunk_title='Shiny_Example'}
runExample("01_hello")
```
错误:无法从
runApp()
内拨打runApp()
。如果您的应用代码包含runApp()
,请将其删除。
如果我在R markdown代码块之外运行代码,我可以让应用程序加载得很好(即使在Rstudio的同一会话中)。
我注意到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
另请注意:
运行其他闪亮代码可以正常使用此设置:
---
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)
```
会话信息:
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
答案 0 :(得分: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()
或解析内部函数isWindows()
(即我收到错误Error: could not find function "resolve"
),所以我使用getAnywhere()
查找代码,然后在创建runExample2
之前手动分配这些函数。功能
我的完整工作 r降价代码:
---
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")
```