0

I am building a Shiny app that automates running of one of my R packages. The intent is to include the app in my package itself, as well as through shiny apps.io.

I think I am near a solution, but am experiencing one issue.

In my package, I have the line

seqs <- ape::read.dna(file = file.choose(), format = "fasta")

which works locally, but not through the browser (as expected).

My attempt to fix this is by using shiny::FileInput().

I now have included the following lines within my R package:

if (interactive()) {
    seqs <- ape::read.dna(file = file.choose(), format = "fasta")
} else {
    seqs <- ape::read.dna(file = some.path, format = "fasta")
}

Here's the code specific code snippet in ui.R:

 fileInput(inputId = "fastafile",
           label = "Choose a FASTA file", 
           accept = c(".fas", ".fasta"),
           buttonLabel = "Browse...")

and that for server.R:

fasta <- input$fastafile
ext <- tools::file_ext(fasta$datapath)
req(fasta)
validate(need(ext == c("fas", "fasta"), "Please upload a FASTA file"))
seqs <- ape::read.dna(file = fasta$datapath, format = "fasta")

However, when I publish the app online, I get the error

character(0)
        An error has occurred. Check your logs or contact the app author for clarification. 

The specific error in the logs is:

Warning in file(con, "rb") : cannot open file '��A�U': No such file or directory
Warning: Error in file: cannot open the connection 

Why doesn't this solution work?

Does anyone have any ideas on how to solve the problem?

compbiostats
  • 909
  • 7
  • 22
  • Is your server code inside of a reactive context? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Can you get any more details from the error logs? – MrFlick Mar 02 '22 at 23:06
  • @MrFlick I have added the error in the logs to my post. It's difficult to create a reproducible example since the package that runs the app has a lot of complexities. However, I will see what I can do. In the meanwhile, hopefully the log errors help shed some light on the issue. – compbiostats Mar 02 '22 at 23:25
  • Your package code seems to be `some.path` but that doesn't appear in the shiny code. The error log is quite odd. It's unclear how that name could pass the check for file extension. Are you checking to make sure a file is uploaded first? `req(input$fastafile)`? There are so many things that could go wrong it's so hard to randomly guess where the problem may be. – MrFlick Mar 02 '22 at 23:30
  • Ahhh. I see. Sorry `some.path` was supposed to be replaced by `fasta$datapath`. I did do `req(fasta)` where `fasta <- input$fastafile`. Let me try deploying again. Seems likely there will still be a problem though. – compbiostats Mar 02 '22 at 23:52

0 Answers0