这里我们利用下载报表, 使用的是rmarkdown格式. 功能很强大. 流程1, 生成数据代码: ID = 1:20
y = rnorm(20)
dat = data.frame(ID,y) 2, 生成一个rmarkdown文件, head, summary, plot, 以及内容. shiny 代码app.R library(shiny)
library(data.table)
library(shinydashboard)
ui = dashboardPage(
dashboardHeader(title = "如何上传数据"),
dashboardSidebar(
menuItem("生成数据",tabName = "a")
),
dashboardBody(
tabItems(
tabItem(tabName = "a",
tableOutput("head"),
downloadButton("down1","下载rmarkdown的html报表"))
)
)
)
server <- function(input, output) {
d1 <- reactive({
ID = 1:20
y = rnorm(20)
dat = data.frame(ID,y)
})
output$head <- renderTable({
dat= d1()
head(dat)
})
output$down1 <- downloadHandler(
filename = function() {
paste('Data-summary', Sys.time(), sep = '.', 'html')
},
content = function(file) {
dat = d1()
src <- normalizePath('rmarkdown1.Rmd')
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'rmarkdown1.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('rmarkdown1.Rmd', html_document())
file.rename(out, file)
})
}
shinyApp(ui = ui, server = server) rmarkdown1.Rmd # First title
> 引用
## 二级标题
```{r}
1+12
```
## head
```{r}
head(dat)
```
## summary
```{r}
summary(dat)
```
## plot
```{r}
plot(dat)
``` shiny运行界面下载的html界面:
|
|