您好我有一个简单的闪亮应用程序,它由ui.r,server.r和kable.rmd文件组成。我想在闪亮的应用程序中导入一个csv,然后能够使用kableExtra
以pdf格式生成并下载一个包含rmarkdown
的表格。我更喜欢kableExtra
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")
```
答案 0 :(得分:0)
这是您尝试完成的最小工作版本。我使用html
作为pdf
的输出格式instad。您可以使用
csv
文件进行测试
write.csv(mtcars, "mtcars.csv")
确保这三个文件位于同一目录中!
library(shiny)
library(rmarkdown)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
fileInput("file1", "Input CSV-File"),
downloadButton('downloadReport')
),
mainPanel(tableOutput("table"))
))
server <- function(input, output) {
output$downloadReport <- downloadHandler(
filename = "my-report.html",
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)
out <- render('kable.Rmd', params = list(file = input$file1$datapath))
file.rename(out, file)
}
)
output$table <- renderTable({
inFile <- req(input$file1)
read.csv(inFile$datapath)
})
}
shinyApp(ui, server)
---
params:
file: "mtcars.csv"
---
```{r echo = FALSE, message = FALSE, include = FALSE}
library(kableExtra)
library(knitr)
```
```{r nice-tab, tidy = FALSE, echo = FALSE, message = FALSE}
csvdata <- read.csv(file = params$file)
kable(csvdata, "html", caption = 'REPORT TABLE',
booktabs = TRUE, longtable = TRUE, escape = FALSE) %>%
kable_styling(full_width = T, font_size = 10 ) %>%
row_spec(row = 0, bold = T, background = "gray")
```
另请注意,您可以通过将Rmd
放在同一目录中并使用&#34;编织&#34;来分别从应用中测试mtcars.csv
文件。 RStudio中的按钮。