【问题标题】:shinyApp not rendering Rmarkdown file as RStudioshinyApp 没有将 Rmarkdown 文件呈现为 RStudio
【发布时间】:2019-10-05 03:50:48
【问题描述】:

我很难理解为什么 shinyApp 不会像 RStudio 那样渲染 Rmarkdown 文件,即使 Rmarkdown 在 app.R 中明确定义。

下面的 Rmd 文件是用 RStudio 编写的,当单击“运行文档”按钮时,会生成一个 HTML 文档,其中包含应用程序的友好 Web、侧边栏、页面和绘图。但是,如果它与闪亮服务器中的 app.R 文件一起托管,则会返回一些错误并且呈现的文件缺少原始文档的结构(例如侧边栏、页面等)。 .)。这也可以通过运行Rscript --vanilla app.R 并转到localhost:port 来生成。

这是我正在使用的文件:

example.Rmd

---
title: "Example file"
runtime: shiny
theme: simplex
vertical_layout: fill
output:
  flexdashboard::flex_dashboard:
    orientation: rows
---

```{r setup, include=FALSE}
library(shiny)
library(tidyverse)
library(plotly)
```

Sidebar {.sidebar}
======================================================================

**Options**

```{r opt_general, echo = FALSE}
selectInput("opt_cyl",
            label = "Select cyl",
            choices = mtcars %>% .$cyl %>% unique %>% sort,
            multiple = TRUE,
            selected = "4")

sliderInput("opt_qsec", label = "Qsec", min = mtcars$qsec %>% min, max = mtcars$qsec %>% max, value = mtcars$qsec %>% max, step = 0.01)
```

**More options**

```{r opt_dist, echo = FALSE}
checkboxInput("opt_log", label = "Log scale (qsec)", value = FALSE)
```

Explore
======================================================================

```{r global, echo=FALSE}
 mtcars$cyl <- as.character(mtcars$cyl)
 ```

 ```{r reactive_global, echo=FALSE}
rcars <- reactive({
   C <- dplyr::filter(mtcars, cyl==input$opt_cyl, qsec <= input$opt_qsec)
  return(C)
})
 ```

Row
------------------------

### One nice plot

```{r plot1a, echo = FALSE}
    uiOutput("r1a")

output$r1a <- renderUI({
  plotlyOutput("p1a")
})

output$p1a <- renderPlotly({
  P <- mtcars %>% ggplot() + geom_point(aes(x=cyl, y=qsec))
  ggplotly(P)
})
```

### Another nice plot

```{r plot1b, echo = FALSE}
uiOutput("r1b")

output$r1b <- renderUI({
  plotlyOutput("p1b")
})

output$p1b <- renderPlotly({
  P <- rcars() %>% ggplot() + geom_point(aes(x=cyl, y=qsec))
  ggplotly(P)
})
```

Row
------------------------

### Second row plot

```{r plot2, echo = FALSE}
uiOutput("r2")

output$r2 <- renderUI({
  plotlyOutput("p2")
})

output$p2 <- renderPlotly({
  C <- rcars()
  if (input$opt_log) C$qsec <- log(C$qsec)
  P <- C %>% ggplot() + geom_point(aes(x=mpg, y=qsec))
  ggplotly(P)
})
```

About
======================================================================

Some nice README

对应的app.R文件为:

library(shiny)
library(knitr)

ui <- shinyUI(
  fluidPage(
    uiOutput('markdown')
  )
)

server <-function (input, output) {
  output$markdown <- renderUI({
    HTML(markdown::markdownToHTML(knit('example.Rmd', quiet = TRUE)))
  })
}

shinyApp(ui, server)

日志文件中返回的错误是:

收听http://127.0.0.1:44229 警告:错误:结果的长度必须为 32,而不是 0 125: 警告:错误:结果的长度必须为 32,而不是 0 100:

有人可以告诉我为什么会这样吗?谢谢

【问题讨论】:

    标签: r shiny rstudio r-markdown shiny-server


    【解决方案1】:

    您可以尝试通过更改app.R 来模仿函数rmarkdown::run,如下所示。

    library(shiny)
    
    file <- 'example.Rmd'
    dir <- dirname(file)
    
    ui <- rmarkdown:::rmarkdown_shiny_ui(dir, file)
    render_args <- list()
    render_args$envir <- parent.frame()
    server <- rmarkdown:::rmarkdown_shiny_server(dir, file, 'UTF-8', T, render_args)
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2017-05-27
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 2013-10-17
      • 1970-01-01
      相关资源
      最近更新 更多