2016-06-13 1 views
2

Shiny App을 구축하고 입력 된 코드를 가져 와서 데이터베이스에서 데이터를 가져 와서 일련의 그래프를 생성하는 회사의 Shiny 서버에 호스트하려고합니다. 마크 다운 (.md) 또는 워드 (.doc) 형식의 표가 있습니다. 이상적으로, 나는이 응용 프로그램에 대해 세 가지 파일, 즉 server, ui 및 r markdown 템플릿을 갖게됩니다.Shiny Server를 통해 R markdown에 빛나는 입력 전달

현재 SWeave를 사용하는 작동 App이 있지만 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} 

을 통과 할 수있는 방법이 있나요 신으로부터의 입력 y 입력에있는 것처럼 RMD에 직접 응용 프로그램. SWeave (주식 코드 < - 입력 $ stockcode)로 SWeave로 주식을 보유하고 있습니까?

답변

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