2016-06-13 10 views
2

私はシャイニー・アプリケーションを構築し、入力されたコードを入力してデータベースからデータを引き出し、一連のグラフを生成する私の会社のシャイニー・サーバーでホストしようとしていますマークダウン(.md)形式またはワード(.doc)形式の表があります。理想的には、私はこのアプリケーションのための3つのファイルを持っています:server、ui、r markdownテンプレート。シャイニー・サーバーを介してRマークダウンに光沢のある入力を渡す

私は現在、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} 

合格する方法はあります新からの入力私はSWeave(株コード< - 入力$株コード)とinput.Rnwで持っているようにyアプリケーションを直接RMDに?

答えて

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

関連する問題