【问题标题】:Passing a Shiny input to R markdown via Shiny Server通过 Shiny Server 将 Shiny 输入传递给 R markdown
【发布时间】:2016-10-13 09:22:27
【问题描述】:

我正在尝试构建一个 Shiny 应用程序并将其托管在我公司的 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')
))

输入.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}

有什么方法可以像我在 input.Rnw 中使用 SWeave (stockcode

【问题讨论】:

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


    【解决方案1】:

    您不必将它传递给 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)
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 2015-11-09
      • 2019-11-14
      • 2015-05-12
      • 1970-01-01
      • 2017-08-31
      • 2016-05-06
      相关资源
      最近更新 更多