嗨,我有一个简单的闪亮应用程序,它由 ui.r、server.r 和 kable.rmd 文件组成。我想在闪亮的应用程序中导入一个 csv,然后能够生成并下载一个带有kableExtra
.pdf 格式的表格rmarkdown
。我更喜欢kableExtra
intead,DT
因为我认为这为格式化最终的 pdf 表格提供了更多选择。这可能吗?
#ui.r
library(shiny)
fluidPage(
# App title ----
titlePanel(div("CLINICAL TABLE",style = "color:blue")),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("file1", "Input CSV-File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Horizontal line ----
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head"),
radioButtons('format', 'Document format', c('PDF', 'CSV'),
inline = TRUE),
downloadButton('downloadReport')
),
# Main panel for displaying outputs ----
mainPanel(
)
))
#server.r
function(input, output) {
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', CSV = 'csv'
))
},
content = function(file) {
src <- normalizePath('kable.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'kable.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('kable.Rmd', switch(
input$format,
PDF = pdf_document(), CSV = csv_document()
))
file.rename(out, file)
}
)
}
#kable.rmd
---
title: "Clinical Table"
author: Ek
date: January 29, 2018
output:
pdf_document:
keep_tex: yes
latex_engine: lualatex
mainfont: Calibri Light
sansfont: Calibri Light
fontsize: 10
urlcolor: blue
---
```{r echo=FALSE,message=FALSE,include=FALSE}
library(knitr)
## Warning: package 'knitr' was built under R version 3.4.3
library(kableExtra)
library(rmarkdown)
library(shiny)
library(readr)
```
```{r nice-tab, tidy=FALSE,echo=FALSE,message=FALSE}
knitr::kable(
req(input$file1)
csvdata <- read.csv(input$file1$datapath,
header = input$header
)
, caption = 'REPORT TABLE',
booktabs = TRUE,longtable = T,format = "latex",escape = F
)%>%
kable_styling(latex_options = "striped",full_width = T,font_size = 10 )%>%
row_spec(row = 0,bold = T,background = "gray")
```