r - 使用 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 错误。关于这里可能是什么问题的任何建议?
解决方案
“File” -> “Save with Encoding...”,在弹出的对话框中选择UTF-8。关闭文件,重新打开,如有必要,在 RStudio 中尝试“文件”->“使用编码重新打开..”,然后再次选择 UTF-8。
因为这可能是由于编码错误,其中可能是单个字符导致问题。甚至可以尝试将文件复制为新文件,以查看是否消除了编码错误。
另一个建议是确保您当前的工作目录是您要从中编织的目录。您的 Shiny 应用程序和 Rmd 文件是否在同一个目录中?如果没有,请尝试在同一目录中使用 .Rmd 运行该应用程序。
同样在您的 Rmd 中,您有
library(tidyquant)
library(tidyverse)
library(dplyr)
我认为那些应该在 R 块内,对吗?
这对我来说很好,作为html_document
推荐阅读
- php - LRAVEL 使用未定义的常量问题 - 控制器中假设的“问题”
- ios - 使用 xcodebuild 归档时缺少配置文件错误
- python - 经验洗牌检查python
- python-3.x - Python:创建一个形状未知的蒙版
- blockchain - Corda 中使用 SignatureConstraint 的非向后兼容状态升级
- java - java是否使用多个进程来运行代码
- flutter - 子滚动条影响父滚动条?
- python-3.x - 我想点击 tkinter 条目进行绑定,甚至返回三个差异值到条目
- c# - 使用反射客户端的 SFTP 连接
- visual-studio-code - java.lang.ClassNotFoundException:在 Visual Studio 代码中找不到 MyMapper 的实现