Error importing xml file with fileInput

Hello,

I'm using fileInput() to read and then parse an xml file (with the xml2 package), and then output the selected data. My code works in a normal R script, but when I run it in shiny, after I upload the file, I get the error: no applicable method for 'read_xml' applied to an object of class "NULL".

Below is a simplified example of my app structure:

library(shiny)
library(xml2)

# make a file to upload
h <- read_html("<p>Hi!</p>")
write_xml(h, "mydata.xml", options = "format", encoding = "UTF-8")

ui <- fluidPage(
  fileInput("File", "Choose file"),
  tableOutput("Data")
)

server <- function(input, output, session) {
  Data <- eventReactive(input$File, {
    read_xml(input$File$datapath)

  })
  
  output$Data <- renderTable({
    head(xml_text(Data()))
  })
  
}

shinyApp(ui, server)

Do you get this error before or after selecting the file?

If it is before, you could try changing your eventReactive statement to this:

raw.data <- reactive({
  if (!is.null(input$file1)){
    read_xml(input$File1$datapath)
  }
})

By default inputs are NULL values. This will prevent it from trying to read the file unless there is one selected

Thanks tbradley,
It is after I upload the file, unfortunately. I will change my question to mention that.

I got it working. Nothing to do with xml I had an error in my script - "file" in fileInput and "File" in eventReactive. Definitely feel silly about that. I edited the code in the initial question to the working version.