第一個博文講了如何構(gòu)建shiny app, 如何上傳數(shù)據(jù).
第二個博文講了如何利用shiny app, 下載數(shù)據(jù).
這里我們利用下載報表, 使用的是rmarkdown格式. 功能很強(qiáng)大.
1, 生成數(shù)據(jù)代碼:
ID = 1:20
y = rnorm(20)
dat = data.frame(ID,y)
2, 生成一個rmarkdown文件, head, summary, plot, 以及內(nèi)容.
3, 下載報表
app.R
library(shiny)
library(data.table)
library(shinydashboard)
ui = dashboardPage(
dashboardHeader(title = "如何上傳數(shù)據(jù)"),
dashboardSidebar(
menuItem("生成數(shù)據(jù)",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
將其放在app.R相同的文件夾中.
# First title
> 引用
## 二級標(biāo)題
```{r}
1+12
```
## head
```{r}
head(dat)
```
## summary
```{r}
summary(dat)
```
## plot
```{r}
plot(dat)
```
下載的html界面:
可以看到, 雖然不支持中文, 但是該有的markdown還是都有的. 可以用于自動化報表的生成.