【问题标题】:Shiny to output a function that generates a pdf file itselfShiny 输出一个生成 pdf 文件本身的函数
【发布时间】:2025-12-11 06:05:02
【问题描述】:

我正在尝试使用 Shiny 构建一个具有输出 pdf 文件功能的应用程序。具体来说,我尝试使用的函数是 msa 包中的 msaPrettyPrint 函数。它使用tools 包中的texi2pdf 函数来生成pdf 文件。 例如,如果您运行以下代码,您将在工作目录中生成一个名为“myFirstAlignment.pdf”的 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 来运行这个例子。 非常感谢!

【问题讨论】:

    标签: r pdf shiny latex


    【解决方案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'
         )
      }
    ))
    

    【讨论】:

      【解决方案2】:

      也许您可以使用msaPrettyPrint file 参数在本地存储pdf,然后使用此解决方案displaying a pdf from a local drive in shiny 在您的应用程序中放置一个pdf查看器。

      【讨论】:

      • 嗨 Malanche,有没有一种方法可以查看文件生成后的位置?感谢您的帮助!
      • 是的,有。默认情况下,使用相对路径保存的任何文件都将保存在 ui.R 和 server.R 所在的同一文件夹中。例如,使用 save 函数创建 RData 环境:save(myVariable, file = "myData.RData"),然后是 datapath &lt;- paste(getwd(),"myData.RData",sep="/")。在您的情况下,您使用msaPrettyPrint 而不是save,并且您传递pdf文件的正确名称而不是myData.RData。 PS:抱歉所有这些编辑,我只是​​注意到 cmets 只接受 mini-Makrdown 格式。
      • 非常感谢,马兰奇。我会尽快试试你的方法。
      【解决方案3】:

      我在运行您的示例时遇到了问题,但这应该可以工作

      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'
          )
      
        }
      ))
      

      【讨论】:

      • 它不工作。当我执行你的代码并点击下载按钮时,它只是弹出一个带有下载按钮的新窗口。我再次点击下载按钮,弹出另一个带有下载按钮的新窗口。值得一提的是,这段代码需要 LaTeX 才能工作。您将需要 LaTeX 来运行该示例。感谢您的尝试!
      • 添加了一些编辑,使其更接近,但仍然缺少一些东西