首页 > 解决方案 > 使用 Flexdashboard 输出格式呈现 Rmd 的 Shiny App 显示 pandoc 文档转换失败,错误为 1

问题描述

我有一个基于迷你闪亮的应用程序来渲染具有 FlexDashboard 输出格式的 RMD 文件。但是,当我在渲染过程中运行应用程序时,它显示以下错误

pandoc.exe:无法解码字节 '\xcf':Data.Text.Internal.Encoding.decodeUtf8:无效的 UTF-8 流警告:错误:pandoc 文档转换失败,错误 1

我的闪亮应用程序如下

 ui <- pageWithSidebar(headerPanel("Shiny Render RMD"),
        sidebarPanel(
         shinyjs::useShinyjs(),
         shinyalert::useShinyalert(),
         actionButton("render", "Render RMD",icon=icon("play"),class="btn-success"),
         ),
        mainPanel(uiOutput("report"))
       )

     server <- function(session,input,output){  
       observeEvent(input$render, {
             output$report <- renderUI({
                includeHTML(
                   rmarkdown::render('mpg_ex.rmd',
                                     envir = new.env(),
                                     output_file=paste('RenderRMD_usingShiny_',
                                                       as.Date(data.table::as.IDate(Sys.Date())),'_',                                                           
                 strftime(Sys.time(),format="%I%M%S%p"),'.html', sep=""))
                 )
               
                })

             })
            }
          shinyApp(ui,server)

我的 mpg_ex.rmd 文件如下

    ---
    title: "Render RMD"
    date: "`r format(Sys.Date(),'%d %B %Y')`"
    output: 
    flexdashboard::flex_dashboard:
    theme: cosmo
    ---

   library(tidyquant)
   library(tidyverse)
   library(dplyr)

   ```{r, echo=FALSE}
   knitr::opts_chunk$set(error = TRUE)
   ```

   ```{r}
    mpg_pivot_table_1 <- mpg%>%
    dplyr::group_by(manufacturer)%>%
    count(class, name = "n")%>%
   pivot_wider(names_from = class, values_from = n, values_fill = 0)%>%
   ungroup()

   mpg_long_summary_table <- mpg_pivot_table_1 %>%
   pivot_longer(
   cols      = compact:subcompact,
   names_to  = "class",
   values_to = "value")

    mpg_long_summary_table %>%
    ggplot(aes(class, manufacturer, fill = value)) +
    geom_tile() +
    geom_label(aes(label = value), fill = "white") +
    scale_fill_viridis_c() +
    theme_minimal() +
    labs(title = "Class by Auto Manufacturer")
  ```

如果我将 RMD 文件中的输出更改为 html_output ,它会呈现没有 pandoc 错误。关于这里可能是什么问题的任何建议?

标签: rshinyr-markdownflexdashboard

解决方案


来自这个 SO 答案的建议

“File” -> “Save with Encoding...”,在弹出的对话框中选择UTF-8。关闭文件,重新打开,如有必要,在 RStudio 中尝试“文件”->“使用编码重新打开..”,然后再次选择 UTF-8。

因为这可能是由于编码错误,其中可能是单个字符导致问题。甚至可以尝试将文件复制为新文件,以查看是否消除了编码错误。

另一个建议是确保您当前的工作目录是您要从中编织的目录。您的 Shiny 应用程序和 Rmd 文件是否在同一个目录中?如果没有,请尝试在同一目录中使用 .Rmd 运行该应用程序。

同样在您的 Rmd 中,您有

library(tidyquant)
library(tidyverse)
library(dplyr)

我认为那些应该在 R 块内,对吗?

这对我来说很好,作为html_document

在此处输入图像描述


推荐阅读