【发布时间】:2021-01-11 06:59:46
【问题描述】:
编辑:因为我在全局函数中设置了格式选项,所以我必须在 kable_styling() 调用中设置 latex_options 或 bootstrap_options。我使用的是bootstrap_options,它没有被乳胶读取。我的解决方法是制作两次表格,一次在 html 块中,一次在乳胶块中。不是很好,但如果我单击Knit 按钮并选择Knit to PDF,它会起作用。但是,当我尝试在闪亮的应用程序中运行它时,它会引发原始错误。
我已经为我的项目创建了一个测试版本 (MiniTest)。我需要做的是让shiny 应用程序运行一个选项卡,该选项卡将为用户选择的(反应性)国家/地区生成一个 html 文件,并提供 Excel 下载(我有这个工作,所以将其排除在此示例之外)和 PDF 下载。我在一个 .Rmd 中编织,它选择格式并允许参数化。 (闪亮的部分是其他人设置的,当他们在完成之前离开时我接手了这个项目。)
我使用kable 和kableExtra 来创建和格式化表格,因为我听说它用于html 和LaTeX 输出。 HTML 比我想要的少。我可以编织 html 或 PDF,它可以运行,但是在闪亮的应用程序中,只有 html 部分有效。我想我已经将 PDF 问题缩小到 column_spec 导致下载崩溃。如果我注释掉 t01 和 t02 中的 column_spec 行,下载 PDF 就会运行。但我需要那种格式。很抱歉,我忘记了我搜索过的所有网站。
在global.R,我设置:
countries <- c("ABC", "DEF", "GHI", "JKL")
在 .Rmd 中,我设置了 YAML(国家和输出类型有两个空格缩进):
参数:
国家:ABC
输出:
pdf_document:默认
html_document: 默认
相关的 .Rmd 块和内联代码包括:
knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = function() {
if (knitr::is_latex_output()) "latex" else "html"
})
library(shiny)
library(htmlwidgets)
library(shinythemes)
library(shinydashboard)
library(shinyjs)
library(shinycssloaders)
library(markdown)
library(tidyr)
library(tidyverse)
library(janitor)
library(kableExtra)
options(scipen = 999)
mini <- mtcars %>%
tibble::rownames_to_column(var = "car") %>%
mutate(Country = c(rep("ABC", 8), rep("DEF", 8), rep("GHI", 8), rep("JKL", 8)))
## https://bookdown.org/yihui/rmarkdown-cookbook/font-color.html
colorize <- function(x, color) {
if (knitr::is_latex_output()) {
## hack setting color='blue' instead of a hexcode with # that breaks the LaTeX code
sprintf("\\textcolor{%s}{%s}", color = 'blue', x) ## works, but isn't right blue
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>", color, x)
} else x
}
## make two tables with `kable` and `kableExtra`
new_title <- paste0("Dynamically Changing Country Name in column", params$Country)
t01 <- mini %>%
filter(Country == params$Country) %>%
select(car, mpg:hp) %>%
rename({{new_title}} := car) %>%
kable(align = c("l", "c", "c", "c", "c")) %>%
kable_styling(full_width = FALSE, position = "left", bootstrap_options = c("striped", "condensed")) %>%
column_spec(1, bold = TRUE) %>%
column_spec(2:3, width = "5em") %>%
row_spec(0, color = "#2A64AB") %>%
row_spec(6, bold = TRUE)
t02_title <- paste0(params$Country, " Table with Dollar Signs in Var Names")
t02 <- mini %>%
filter(Country == params$Country) %>%
select(car, drat, wt) %>%
mutate(car = case_when(car == "Mazda RX4" ~ "Mazda RX4 (US\\$)*", TRUE ~ as.character(car))) %>%
## want to blank out column names - removing them entirely would be best, but it fails
kable(align = c("l", "r", "c"), escape = TRUE, col.names = c("", "", "")) %>%
kable_styling(full_width = FALSE, position = "left", bootstrap_options = c("striped", "condensed")) %>%
column_spec(1, bold = TRUE) %>%
column_spec(2, width = "10em") %>%
footnote(general = "*Never smart to start with an asterisk, but here we are", general_title = "")
## make two charts with `ggplot2`
chart1 <- mini %>%
filter(Country == params$Country) %>%
select(car, mpg:hp) %>%
ggplot2::ggplot(mapping = aes(x = mpg)) +
geom_col(aes(y = `cyl`, fill = "cyl"), color = "black")
c1_title <- paste0("Some fab title here for ", params$Country)
chart2 <- mini %>%
filter(Country == params$Country) %>%
select(car, vs:carb) %>%
ggplot2::ggplot(mapping = aes(x = carb)) +
geom_col(aes(y = `gear`, fill = "gear"), color = "black")
c2_title <- paste0("Another chart, ", params$Country)
## make a "tiny" LaTeX environment that is only generated for LaTeX output, with chunk setting `include = knitr::is_latex_output()`.
knitr::asis_output('\n\n\\begin{tiny}')
## Table 1
t01
我希望会弹出一个 PDF,但会弹出一个保存文件框,要求保存没有文件扩展名的“DownloadPDF”。 ui.R 应该将其命名为“FactCountryName.pdf”,其中“CountryName”是从用户在下拉列表中选择的国家/地区输入的。无论我选择保存(没有任何反应)还是取消,我的 R 都会抛出以下错误:
如果我注释掉column_spec(1, bold = TRUE) %>%这一行,错误会变成:
请帮忙!
【问题讨论】:
标签: html r pdf shiny kableextra