将 XML 数据从 SQL 服务器导出到 R

Export XML data from SQL Server to R

我需要从 table Person.Person -- 在 XML -- 从 SQL 服务器数据库 AdventureWorks2014 导出属性 Demographics 到 R 中进行一些统计分析.我想使用 XML 包,但当我导出文件时,R 似乎无法将我的文件识别为 XML。

有人知道用一些包直接从 SQL 到 R 的方法吗?之前没有将数据导出到 CSV?

我在 SQL 中的一个元组如下所示:

<IndividualSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey">
    <TotalPurchaseYTD>-31</TotalPurchaseYTD>
    <DateFirstPurchase>2003-11-01Z</DateFirstPurchase>
    <BirthDate>1962-07-26Z</BirthDate>
    <MaritalStatus>S</MaritalStatus>
    <YearlyIncome>0-25000</YearlyIncome>
    <Gender>M</Gender>
    <TotalChildren>1</TotalChildren>
    <NumberChildrenAtHome>0</NumberChildrenAtHome>
    <Education>Graduate Degree</Education>
    <Occupation>Manual</Occupation>
    <HomeOwnerFlag>0</HomeOwnerFlag>
    <NumberCarsOwned>0</NumberCarsOwned>
    <CommuteDistance>0-1 Miles</CommuteDistance>
</IndividualSurvey>

我想在 R 中有这样的不同属性:

TotalPurchaseYTD  DateFirstPurchase  BirthDate   MaritalStatus  YearlyIncome...
-31                  2003-11-01    1962-07-26      S            0-25000     ....

我们可以使用:

library(xml2)
df <- read_xml(x) %>% as_list %>% sapply(rbind) %>% as.data.frame

df
# TotalPurchaseYTD DateFirstPurchase   BirthDate MaritalStatus YearlyIncome Gender TotalChildren NumberChildrenAtHome Education Occupation HomeOwnerFlag NumberCarsOwned CommuteDistance
# 1              -31       2003-11-01Z 1962-07-26Z             S      0-25000      M             1                    0 1 Graduate Degree     Manual             0               0       0-1 Miles