闪亮输出一个自己生成pdf文件的函数

时间:2016-09-05 14:45:57

标签: r pdf shiny latex

我正在尝试使用Shiny来构建一个具有输出pdf文件的功能的应用程序。具体来说,我尝试使用的函数是msaPrettyPrint包中的msa函数。它使用texi2pdf包中的tools函数生成pdf文件。 例如,如果您运行以下代码,您将生成一个名为" myFirstAlignment.pdf"在工作目录中使用氨基酸序列比对。

# source("http://www.bioconductor.org/biocLite.R")
# biocLite("msa")
library(msa)
mySequenceFile <- system.file("examples", "exampleAA.fasta", package="msa")
mySequences <- readAAStringSet(mySequenceFile)
myFirstAlignment <- msa(mySequences)
msaPrettyPrint(myFirstAlignment, output="pdf", showNames="left",showLogo="top",consensusColor="BlueRed", logoColors="accessible area", askForOverwrite=FALSE)

我想知道是否有办法让以下代码有效?我认为问题可能是因为输出已经是pdf文件了。我想在可能的情况下在屏幕上看到pdf输出。如果无法在屏幕上看到它,pdf文件在哪里,如果我可以下载它?

library(shiny)
runApp(list(
  #Load the exmaple from the msa package.
  mySequenceFile <- system.file("examples", "exampleAA.fasta", package="msa"),
  mySequences <- readAAStringSet(mySequenceFile),
  myFirstAlignment <- msa(mySequences),
  # A simple shiny app.
  # Is it possible to see the generated pdf file on screen?
  ui = fluidPage(plotOutput('plot')),
  server = function(input, output) {
    output$plot <- renderPlot(msaPrettyPrint(myFirstAlignment, output="pdf", showNames="left",showLogo="top",consensusColor="BlueRed", logoColors="accessible area", askForOverwrite=FALSE))
  }
))

有一点需要注意的是,此代码需要LaTeX才能工作。您需要LaTeX来运行该示例。 非常感谢!

3 个答案:

答案 0 :(得分:1)

我在运行您的示例时遇到问题,但这应该有效

library(shiny)
runApp(list(
  #Load the exmaple from the msa package.
  mySequenceFile <- system.file("examples", "exampleAA.fasta", package="msa"),
  mySequences <- readAAStringSet(mySequenceFile),
  myFirstAlignment <- msa(mySequences),
  # A simple shiny app.
  # Is it possible to see the generated pdf file on screen?
  ui = fluidPage(downloadButton('downloadPDF')),
  server = function(input, output) {

    output$downloadPDF = downloadHandler(
      filename = 'myreport.pdf',

      content = function(file) {
        out = msaPrettyPrint(
              myFirstAlignment
              , file = 'myreport.pdf'
              , output="pdf"
              , showNames="left"
              , showLogo="top"
              , consensusColor="BlueRed"
              , logoColors="accessible area"
              , askForOverwrite=FALSE)
        file.rename(out, file) # move pdf to file for downloading
      },

      contentType = 'application/pdf'
    )

  }
))

答案 1 :(得分:1)

也许您可以使用SELECT tag, count(tag) FROM people p, jsonb_array_elements(p.data->'tags') tag WHERE p.data->'tags' ? '#{tag}' GROUP BY tag msaPrettyPrint参数在本地存储pdf,然后使用此解决方案displaying a pdf from a local drive in shiny将pdf查看器放入您的应用程序中。

答案 2 :(得分:1)

非常感谢JackStat和Malanche的帮助。以下工作用于下载结果!

library(shiny)
runApp(list(
   #Load the exmaple from the msa package.
   mySequenceFile <- system.file("examples", "exampleAA.fasta", package="msa"),
   mySequences <- readAAStringSet(mySequenceFile),
   myFirstAlignment <- msa(mySequences),
   # A simple shiny app.
   # Is it possible to see the generated pdf file on screen?
   ui = fluidPage(downloadButton('downloadPDF')),
   server = function(input, output) {
       output$downloadPDF = downloadHandler(
       filename = 'myreport.pdf',
       content = function(file) {
            msaPrettyPrint(
                myFirstAlignment
              , file = 'myreport.pdf'
              , output="pdf"
              , showNames="left"
              , showLogo="top"
              , consensusColor="BlueRed"
              , logoColors="accessible area"
              , askForOverwrite=FALSE)
       file.rename("myreport.pdf", file) # move pdf to file for downloading
       },
       contentType = 'application/pdf'
     )
  }
))