通过Shiny Server将闪亮输入传递给R markdown

时间:2016-06-13 03:55:40

标签: r shiny r-markdown shiny-server sweave

我正在尝试构建一个Shiny App并将其托管在我公司的Shiny服务器上,该服务器接收输入的股票代码并使用它从我们的数据库中提取数据并在markdown中生成一系列图形和表格(。 md)或word(.doc)格式。理想情况下,我会为此App提供三个文件:server,ui和r markdown模板。

我目前有一个使用SWeave的应用程序,但TeX文件有渲染中文字符的问题,所以我想使用RMD。

server.r:

library(knitr)
shinyServer(function(input, output) {
 output$report = downloadHandler(
      filename = 'myreport.pdf',
      content = function(file) {
           out = knit2pdf('input.Rnw', clean = TRUE)
           file.rename(out, file) # move pdf to file for downloading
      },
      contentType = 'application/pdf'
 )
})

ui.r:

library(shiny)
shinyUI(basicPage(
 textInput('stockcode', 'Stock Code:', value = '600340.SH'),
 downloadButton('report')
))

input.Rnw:

 \documentclass{article}

 \begin{document}
 \SweaveOpts{concordance=TRUE}

 <<initialize, echo = FALSE, results = 'hide'>>=
 library(ggplot2); library(RJDBC); library(gridExtra)
 Sys.setlocale("LC_CTYPE", "chinese")
 o.drv <- JDBC("oracle.jdbc.OracleDriver", classPath="C:/Oracle/instantclient_11_2/ojdbc5.jar", " ")
 o.con <- dbConnect(o.drv, "database_address", "database_user", "database_pw")

 stockcode <- input$stockcode

 x <- dbGetQuery(o.con, "some_query")

 pointLinePlot <- function(df) {
      plotdata <- gather(df, metric, measure, -reportDate)
      ggplot() + geom_line(data = plotdata, aes(x = reportDate, y = measure, color = metric)) +
           geom_point(data = plotdata, aes(x = reportDate, y = measure, color = metric)) + 
           theme(legend.position="bottom", legend.title = element_blank()) + 
           scale_color_manual(name = "", values = c("darkred", "darkgreen", "darkblue", "orange"), 
                              breaks = unique(plotdata$metric), labels = unique(plotdata$metric))
 }

 data_1.1.1 <- data.frame(reportDate = x$REPORT_PERIOD,
                          net_assets_f = x$`TOT_ASSETS-TOT_LIAB` / 1E4,
                          monetary_cap_f = x$MONETARY_CAP / 1E4,
                          net_cash_f = (x$MONETARY_CAP - x$ST_BORROW) / 1E4)
 p1 <- pointLinePlot(data_1.1.1)
 @


 \begin{figure}
   \centering
 <<fig = TRUE, echo = FALSE>>=
 print(p1)
 @
 \caption{Here goes the caption.}
 \label{fig:p1}
 \end{figure}


 \begin{figure}
   \centering
 <<fig = TRUE, echo = FALSE>>=
 print(grid.table(data_1.1.1, rows = NULL))
 @
 \caption{Here goes the caption.}
 \label{fig:p1}
 \end{figure}


 \end{document}

有没有办法将Shiny App中的输入直接传递给RMD,就像我在input.Rnw中使用SWeave(stockcode&lt; - input $ stockcode)?

1 个答案:

答案 0 :(得分:0)

您不必将其传递给Rmd文件。渲染Rmd时,变量input$stockcode取自当前环境,就像使用Rnw版本一样。

<强> server.R

library(knitr)
library(rmarkdown)
shinyServer(function(input, output) {

  output$report = downloadHandler(
    filename = 'myreport.pdf',
    content = function(file) {
      out = render('outout.Rmd')
      file.rename(out, file) # move pdf to file for downloading
    },
    contentType = NA
  )
})

<强> input.Rmd

---
title: "Output"
output: pdf_document
---

# Test 

```{r}
print(input$stockcode)
```

enter image description here