使用用户输入文件生成PDF报告 - 闪亮

时间:2016-04-26 11:03:34

标签: r pdf latex shiny knitr

我创建了R代码,允许我转换和分析数据,然后以表格的形式将结果输出到PDF报告中。最近,我决定与不熟悉R的大学分享我的工作。因此,我想创建一个Shiny应用程序,允许他们在没有安装R的情况下使用我的工作。 Shiny应用程序将作为上传数据然后下载报告的平台。不幸的是,我无法将结果输出为PDF格式。

我的闪亮代码看起来如下:

def up 
  change_column_null :gardens, :name, false, ""
end

def down
  change_column_null :gardens, :name, true
end

我的<!doctype html> <html> <head> </head> <body> <header> <nav> <ul> <li><a href="#info">Info</a></li> <li><a href="#portfolio">Portfolio</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <section id="info"> <p class="info"> <br> <br> <br> My name is Abdulah Hamzic. I am a beginner front-end web developer with not a lot of experience, but passionate about web-development and ready to learn new things everyday. I have completed the <a href="https://www.coursera.org/account/accomplishments/certificate/A5QYJS26WJ4E" target="_blank"><i>HTML, CSS and JavaScript</i></a> course by the Hong Kong University of Science and Technology and I'm currently doing the University of Michigan <a href="https://www.coursera.org/specializations/web-design" target="_blank"><i>Web Design for Everybody</i></a> specialization, plus the <a href="http://freecodecamp.com/" target="_blank">Free Code Camp</a> path. I haven't built many projects, but you can check some of my first pens here on CodePen below. </p> <img id="myself" src="https://scontent-fra3-1.cdninstagram.com/t51.2885-15/e15/10453774_522901187836495_1728850695_n.jpg"/> </section> <section id="portfolio"> <h3>This is my portfolio: </h3> <img src=""> </section> </body> </html> 文件如下所示:

library(knitr)

ui <- fluidPage(
  fluidRow(
    column(3,
           wellPanel(
             fileInput(inputId = "files",
                       label = "Choose csv files",
                       accept=c('text/csv', 
                                'text/comma-separated-values,text/plain',
                                '.csv'),
                       multiple = TRUE),
             textInput('filename', 'File name', value = ''),
             downloadButton('report', label = 'Download PDF')
             )
           )
    )
  )

server <- function(input, output) {

  data_set <- reactive({if(!is.null(input$files)) {
    max_table = length(input$files[,1])

    lst <- list()
    for(i in 1:length(input$files[,1])){
      lst[[i]] <- read.csv(input$files[[i, 'datapath']], sep = ",", header = TRUE, skip = 4, dec = ".")
    }

    lst <- lapply(lst, function(x) xtable(x))}})



  output$report = downloadHandler(
    filename = reactive({paste0(input$filename,'.pdf')}),

    content = function(file) {
      out = knit2pdf('pdf_shell.Rnw', clean = TRUE)
      file.rename(out, file) # move pdf to file for downloading
    },

    contentType = 'application/pdf'
  )
  }


shinyApp(ui = ui, server = server)

显然加载.Rnw不起作用,我不知道如何继续前进。 现在我想输出我加载到Shiny中的数据创建的表。

我真的很感谢你的帮助。

旁注: csv文件是外部简单的数据框,包含标题和填充数据的列。

当在R中输入时,它们看起来应该是这样的:

\documentclass{article}
\usepackage{tabularx}

\begin{document}

\begin{table}[H]
\begin{tabularx}
\textbf{Test Name:}& \Sexpr{input$filename} \\
\textbf{Date:}&  \\
\end{tabularx}
\end{table}

<<echo = FALSE , message = F, >>=
data_set
@


\end{document}

1 个答案:

答案 0 :(得分:3)

问题因为在shiny

data_setreactive所以你不能像

一样使用它
<<echo = FALSE , message = F, >>=
data_set
@

.Rnw

所以试试

.Rnw

\documentclass{article}
\usepackage{tabularx}

\begin{document}

\begin{table}[H]
\begin{tabularx}
\textbf{Test Name:}& \Sexpr{input$filename} \\
\textbf{Date:}&  \\
\end{tabularx}
\end{table}

<<echo = FALSE , message = F, >>=
data_set_1
@


\end{document}

的处理程序

downloadHandler(
    filename = reactive({paste0(input$filename,'.pdf')}),

    content = function(file) {
      data_set_1=data_set()
      out = knit2pdf('pdf_shell.Rnw', clean = TRUE)
      file.rename(out, file) # move pdf to file for downloading
    },

    contentType = 'application/pdf'
  )

data_set()

中可能很简单.Rnw