0

I am trying to use Shiny to build an app with a function that output a pdf file. Specifically, the function I am trying to use is the msaPrettyPrint function from the msa package. It uses the texi2pdf function from the tools package to generate a pdf file. For example, if you run the following code, you will generate a pdf called "myFirstAlignment.pdf" with an amino acid sequence alignment in your working directory.

# 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)

I was wondering if there is a way to make the following code works? I think the problem might be because the output is already a pdf file. I want to see the pdf output on screen if possible. If not possible to see it on screen, where is the pdf file and if I can download it?

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

One thing to mention is that this code needs LaTeX to work. You would need LaTeX to run the example. Thanks a lot!

l0110
  • 859
  • 1
  • 7
  • 17

3 Answers3

1

I was having issues running your example but this should work

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

  }
))
JackStat
  • 1,593
  • 1
  • 11
  • 17
  • It is not working. When I executed your code and hit the download button, it just popped out a new window with a download button. And I hit the download button again, another new window with a download button popped out. One thing to mention is that this code needs LaTeX to work. You would need LaTeX to run the example. Thanks for trying! – l0110 Sep 05 '16 at 17:11
  • Added some edits that got it closer but something in still missing – JackStat Sep 05 '16 at 17:32
1

Maybe you can use the msaPrettyPrint file argument to store the pdf locally, and then use this solution displaying a pdf from a local drive in shiny to put a pdf viewer in your application.

Community
  • 1
  • 1
Malanche
  • 191
  • 8
  • Hi Malanche, is it a way to see where the file after it is generated? Thanks for your help! – l0110 Sep 07 '16 at 04:45
  • Yes, there is. By default, any file saved with relative path will be saved in the same folder where ui.R and server.R are located. For example, using the `save` function to create RData Environments: `save(myVariable, file = "myData.RData")` and then `datapath <- paste(getwd(),"myData.RData",sep="/")`. In your case you use `msaPrettyPrint` instead of `save` and you pass the proper name of your pdf file instead of myData.RData. PS: Sorry for all those edits, I just noticed comments only admit mini-Makrdown format. – Malanche Sep 07 '16 at 06:25
  • Thanks a lot, Malanche. I will try your method soon. – l0110 Sep 07 '16 at 06:35
1

Thanks so much for the help from JackStat and Malanche. The following works for downloading the result!

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'
     )
  }
))
l0110
  • 859
  • 1
  • 7
  • 17