'con' is not a connection error

I am getting the error: Warning: Error in readLines: 'con' is not a connection
[No stack trace available]. Here's my R shiny code:

library(shiny)
library(seqinr)
#>
#> Attaching package: 'seqinr'
#> The following object is masked from 'package:shiny':
#>
#> a

ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput("fasta_file", "Choose a Fasta File",
accept = c(".fasta",
".fa",
".fna"),
multiple = FALSE)
),
mainPanel(

  textOutput("length")
)

)
)
server <- function(input, output) {
file_name <- reactive({
inFile <- input$fasta_file
return(inFile$datapath)
})
output$length <- renderPrint({
example_file.fasta <- file_name()
df <- read.fasta(example_file.fasta, as.string = TRUE)
len <- getLength(df)
len
})

}

shinyApp (ui, server)

here's an example fasta file to test this code;
>a1

actcgtggt

>a2

ccttgg

>a3

cgcgcttttttttt

I am pretty sure this is an error message received when read.fasta tries to load a NULL file as the shiny app starts (see reprex below).

For advice on how to handle this check out Shiny's help articles. For example, Help users upload files to your app has good advice on this. Note the app.R example code, (just a snippet below) recommends using the req function (req: Ensure that values are available ("truthy"–see Details) before proceeding with a calculation or action. If any of the given values is not truthy, the operation is stopped by raising a "silent" exception (not logged by Shiny, nor displayed in the Shiny app's UI).)

You might also consider giving a default .fasta file to start out with.

    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.

    req(input$file1)


library(seqinr)
read.fasta(NULL, as.string = TRUE)
#> Error in readLines(file): 'con' is not a connection

Created on 2018-08-13 by the reprex package (v0.2.0.9000).

1 Like

Thanks, I fixed this error with this null statement.

  if (is.null(inFile))
    return(NULL)