【问题标题】:Does R shiny allow to parse an XML file?R shiny 是否允许解析 XML 文件?
【发布时间】:2016-09-11 16:58:13
【问题描述】:

我在R-Shiny的server.R脚本的renderPlot函数中解析一个上传的XML文件

output$oscar <- renderPlot({
doc <- xmlParse("www/Human_body_front_and_side.svg.eps.xml")

#load the XML as a picture
body<-readPicture(saveXML(doc))

#plot it
grid.arrange(pictureGrob(body), ncol=1)
})

但解析时出现以下错误:

XML content does not seem to be XML

我已加载正确的库(shinyXML),并检查上传的 xml 文件是否正常。

有趣的是,如果在启动 R-Shiny 的“runApp”之前,我在 R 终端中执行:

doc <<- xmlParse("Human_body_front_and_side.svg.eps.xml")

没有错误,一切正常。所以看起来 R Shiny 在解析 xml 时有问题。我是 R-Shiny 的新手,因此有人知道识别 XML 文件需要什么吗?

【问题讨论】:

  • 文件不在正确的文件夹中
  • 如果我的 R-shiny 应用程序名为“test”,我将该 xml 文件保存在“test/www”中并使用 doc
  • 从路径中删除应用文件夹(测试)
  • 如果我用 doc
  • 这意味着文件在正确的文件夹中,但格式不正确

标签: r xml parsing shiny


【解决方案1】:

ui.R:

library(shiny)


    shinyUI(fluidPage(

      titlePanel("Human body"),


      sidebarLayout(
        sidebarPanel(

        ),
        mainPanel(
          plotOutput("humanBody")
        )
      )
    )) 

服务器.R

library(shiny)
    library(XML)
    library(grImport)
    library(gridExtra)
    # Define server logic to draw a histogram
    shinyServer(function(input, output){
      output$humanBody <- renderPlot({
              doc <- readPicture("./www/Human_body_front_and_side.svg.eps.xml")

        grid.arrange(pictureGrob(doc), ncol=1)
      })
    })

如果目标是从 xml 中绘制人体,则此代码有效。不需要解析 xml,因为 readPicture() 函数需要一个您的文件已经存在的 XML。让我知道这是否有帮助。

【讨论】:

  • 感谢您的帮助。你可以在那里找到xml文件s000.tinyupload.com/?file_id=30122562046327270600
  • 我添加了我的问题的 server.R 的一些细节。我用 books.xml 尝试了我的代码,但在步骤正文中得到“参数长度为零”
  • 您使用哪个包提供了 readPicture() 函数?
  • 包是grImport,用来绘制postscript图片cran.r-project.org/web/packages/grImport/index.html
  • 太好了,其实我后面用了grImport的“xpathSApply”和“xmlAttrs”函数来改变人体某些部位的颜色。它是一种魅力。谢谢瓦尔特!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多