在Shiny R中解析上传的XML文件

时间:2015-06-05 15:37:13

标签: xml r xml-parsing shiny

我正在尝试用Shiny解析用户上传的XML文件,并返回所有" time"值。在Shiny中有很多关于CSV文件的文档,但是我在使用XML文件做很简单的事情时遇到了问题。

当我的Shiny server.R脚本试图读取文件时,我收到一个错误:

错误

Error : XML content does not seem to be XML: 'inFile$datapath'

上传的文件

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1"
        xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
        xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
        xmlns:badelf="http://bad-elf.com/xmlschemas"
        version="1.1"
        creator="Bad Elf GPS Pro+ 2.1.26"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://bad-elf.com/xmlschemas http://bad-elf.com/xmlschemas/GpxExtensionsV1.xsd">

    <wpt lat="33.8783" lon="-84.470383">
        <ele>301.4</ele>
        <time>2015-06-03T14:25:14Z</time>
        <name>Start (2015-06-03T14:25:14Z)</name>
    </wpt>
    <wpt lat="33.878407" lon="-84.470512">
        <ele>308</ele>
        <time>2015-06-03T14:48:47Z</time>
        <name>End (2015-06-03T14:48:47Z)</name>
        <desc>distance: 5.54 miles</desc>
    </wpt>
    <trk>
        <name>6/3/15, 10:25 AM</name>
        <trkseg>
            <trkpt lat="33.8783" lon="-84.470383"><ele>301.4</ele><time>2015-06-03T14:25:14Z</time><hdop>0.8</hdop><extensions><badelf:speed>0.051444</badelf:speed><badelf:baroEle>282.2</badelf:baroEle><badelf:baroPress>980.46</badelf:baroPress></extensions></trkpt>
            <trkpt lat="33.8783" lon="-84.470383"><ele>301.4</ele><time>2015-06-03T14:25:15Z</time><hdop>0.8</hdop><extensions><badelf:speed>0</badelf:speed><badelf:baroEle>282.2</badelf:baroEle><badelf:baroPress>980.46</badelf:baroPress></extensions></trkpt>
            <trkpt lat="33.8783" lon="-84.470383"><ele>301.4</ele><time>2015-06-03T14:25:16Z</time><hdop>0.8</hdop><extensions><badelf:speed>0</badelf:speed><badelf:baroEle>282.2</badelf:baroEle><badelf:baroPress>980.46</badelf:baroPress></extensions></trkpt>
        </trkseg>
    </trk>
</gpx>

server.R

library(shiny)

shinyServer(function(input, output) {
  output$contents <- renderTable({

    # input$file will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.

    inFile <- input$file

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

    times <- xpathSApply(htmlTreeParse("inFile$datapath", error = function (...) {}, 
                                       useInternalNodes = T),
                         path = "//trkpt/time", xmlValue)

    print(times)

  })
})

ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("Uploading Files"),
    sidebarLayout(
      sidebarPanel(
        fileInput('file', 'Choose File', accept=NULL)
        ),
      mainPanel(
        textOutput('contents')
      )
    )
  )
)

我对Shiny的fileInput功能有什么误解?

0 个答案:

没有答案