【发布时间】:2015-09-16 06:53:37
【问题描述】:
我正在尝试根据从 PubMed 下载的关键字数据制定研究时间表。感谢许多优秀的例子,在这个和其他网站上,我已经走了很远。附加的代码会根据输入查询下载一个 excel 文件,然后对其进行解析并将其转换为 r 可用的数据帧。唯一的问题是元素“KeywordList”拒绝合作。注释掉后,一切都按预期运行。但是,当包含时,R 会产生以下错误:
Error in FUN("pubmed_MedEng/MedEng01.xml"[[1L]], ...) :
object 'Keyword1' not found
3 FUN("pubmed_MedEng/MedEng01.xml"[[1L]], ...)
2 lapply(myxml.path, function(x) {
myxml <- xmlParse(x, useInternalNodes = TRUE)
padXML <- function(x, xstr) {
res <- xpathSApply(x, xstr, xmlValue) ...
1 pubmed_download("'medical English'", "MedEng")
我使用适当的 XML 开发工具生成了这个 XPath 和其他 XPath,所以我相当肯定它们是可以的。我也以同样的方式检查了 XML 文件。这是一个相关的sn-p:
<?xml version="1.0"?>
<!DOCTYPE PubmedArticleSet PUBLIC "-//NLM//DTD PubMedArticle, 1st January 2015//EN" "http://www.ncbi.nlm.nih.gov/corehtml/query/DTD/pubmed_150101.dtd">
<PubmedArticleSet>
<PubmedArticle>
<MedlineCitation Status="Publisher" Owner="NLM">
<PMID Version="1">26269536</PMID>
<DateCreated>
<Year>2015</Year>
<Month>8</Month>
<Day>13</Day>
</DateCreated>
<DateRevised>
<Year>2015</Year>
<Month>8</Month>
<Day>14</Day>
</DateRevised>
<Article PubModel="Print-Electronic">
<Journal>
<ISSN IssnType="Electronic">1527-974X</ISSN>
<JournalIssue CitedMedium="Internet">
<PubDate>
<Year>2015</Year>
<Month>Aug</Month>
<Day>11</Day>
</PubDate>
</JournalIssue>
<Title>Journal of the American Medical Informatics Association : JAMIA</Title>
<ISOAbbreviation>J Am Med Inform Assoc</ISOAbbreviation>
</Journal>
<ArticleTitle>Assessing the readability of clinicaltrials.gov.</ArticleTitle>
<Pagination>
<MedlinePgn/>
</Pagination>
<ELocationID EIdType="pii">ocv062</ELocationID>
<ELocationID EIdType="doi">10.1093/jamia/ocv062</ELocationID>
<Abstract>
<AbstractText Label="OBJECTIVE" NlmCategory="OBJECTIVE">ClinicalTrials.gov serves critical functions of disseminating trial information to the public and helping the trials recruit participants. This study assessed the readability of trial descriptions at ClinicalTrials.gov using multiple quantitative measures.</AbstractText>
<AbstractText Label="MATERIALS AND METHODS" NlmCategory="METHODS">The analysis included all 165 988 trials registered at ClinicalTrials.gov as of April 30, 2014. To obtain benchmarks, the authors also analyzed 2 other medical corpora: (1) all 955 Health Topics articles from MedlinePlus and (2) a random sample of 100 000 clinician notes retrieved from an electronic health records system intended for conveying internal communication among medical professionals. The authors characterized each of the corpora using 4 surface metrics, and then applied 5 different scoring algorithms to assess their readability. The authors hypothesized that clinician notes would be most difficult to read, followed by trial descriptions and MedlinePlus Health Topics articles.</AbstractText>
<AbstractText Label="RESULTS" NlmCategory="RESULTS">Trial descriptions have the longest average sentence length (26.1 words) across all corpora; 65% of their words used are not covered by a basic medical English dictionary. In comparison, average sentence length of MedlinePlus Health Topics articles is 61% shorter, vocabulary size is 95% smaller, and dictionary coverage is 46% higher. All 5 scoring algorithms consistently rated CliniclTrials.gov trial descriptions the most difficult corpus to read, even harder than clinician notes. On average, it requires 18 years of education to properly understand these trial descriptions according to the results generated by the readability assessment algorithms.</AbstractText>
<AbstractText Label="DISCUSSION AND CONCLUSION" NlmCategory="CONCLUSIONS">Trial descriptions at CliniclTrials.gov are extremely difficult to read. Significant work is warranted to improve their readability in order to achieve CliniclTrials.gov's goal of facilitating information dissemination and subject recruitment.</AbstractText>
<CopyrightInformation>Published by Oxford University Press on behalf of the American Medical Informatics Association 2015. This work is written by US Government employees and is in the public domain in the US.</CopyrightInformation>
</Abstract>
<AuthorList>
<Author>
<LastName>Wu</LastName>
<ForeName>Danny Ty</ForeName>
<Initials>DT</Initials>
<AffiliationInfo>
<Affiliation>School of Information, University of Michigan, Ann Arbor, MI, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>Hanauer</LastName>
<ForeName>David A</ForeName>
<Initials>DA</Initials>
<AffiliationInfo>
<Affiliation>School of Information, University of Michigan, Ann Arbor, MI, USA Department of Pediatrics, University of Michigan, Ann Arbor, MI, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>Mei</LastName>
<ForeName>Qiaozhu</ForeName>
<Initials>Q</Initials>
<AffiliationInfo>
<Affiliation>School of Information, University of Michigan, Ann Arbor, MI, USA Department of Electrical Engineering and Computer Science, University of Michigan, Ann Arbor, MI, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>Clark</LastName>
<ForeName>Patricia M</ForeName>
<Initials>PM</Initials>
<AffiliationInfo>
<Affiliation>School of Nursing, University of Michigan, Ann Arbor, MI, USA Center for Health Communication Research, University of Michigan, Ann Arbor, MI, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>An</LastName>
<ForeName>Lawrence C</ForeName>
<Initials>LC</Initials>
<AffiliationInfo>
<Affiliation>Center for Health Communication Research, University of Michigan, Ann Arbor, MI, USA Department of Internal Medicine, University of Michigan, Ann Arbor, MI, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>Proulx</LastName>
<ForeName>Joshua</ForeName>
<Initials>J</Initials>
<AffiliationInfo>
<Affiliation>Department of Biomedical Informatics, University of Utah, Salt Lake City, UT, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>Zeng</LastName>
<ForeName>Qing T</ForeName>
<Initials>QT</Initials>
<AffiliationInfo>
<Affiliation>Department of Biomedical Informatics, University of Utah, Salt Lake City, UT, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>Vydiswaran</LastName>
<ForeName>Vg Vinod</ForeName>
<Initials>VV</Initials>
<AffiliationInfo>
<Affiliation>School of Information, University of Michigan, Ann Arbor, MI, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>Collins-Thompson</LastName>
<ForeName>Kevyn</ForeName>
<Initials>K</Initials>
<AffiliationInfo>
<Affiliation>School of Information, University of Michigan, Ann Arbor, MI, USA Department of Electrical Engineering and Computer Science, University of Michigan, Ann Arbor, MI, USA.</Affiliation>
</AffiliationInfo>
</Author>
<Author>
<LastName>Zheng</LastName>
<ForeName>Kai</ForeName>
<Initials>K</Initials>
<AffiliationInfo>
<Affiliation>School of Information, University of Michigan, Ann Arbor, MI, USA School of Public Health Department of Health Management and Policy, University of Michigan, Ann Arbor, MI, USA kzheng@umich.edu.</Affiliation>
</AffiliationInfo>
</Author>
</AuthorList>
<Language>ENG</Language>
<PublicationTypeList>
<PublicationType UI="">JOURNAL ARTICLE</PublicationType>
</PublicationTypeList>
<ArticleDate DateType="Electronic">
<Year>2015</Year>
<Month>8</Month>
<Day>11</Day>
</ArticleDate>
</Article>
<MedlineJournalInfo>
<MedlineTA>J Am Med Inform Assoc</MedlineTA>
<NlmUniqueID>9430800</NlmUniqueID>
<ISSNLinking>1067-5027</ISSNLinking>
</MedlineJournalInfo>
<KeywordList Owner="NOTNLM">
<Keyword MajorTopicYN="N">CliniclTrials.gov</Keyword>
<Keyword MajorTopicYN="N">clinical trial</Keyword>
<Keyword MajorTopicYN="N">comprehension</Keyword>
<Keyword MajorTopicYN="N">electronic health records</Keyword>
<Keyword MajorTopicYN="N">natural language processing</Keyword>
<Keyword MajorTopicYN="N">readability</Keyword>
</KeywordList>
</MedlineCitation>
<PubmedData>
<History>
<PubMedPubDate PubStatus="entrez">
<Year>2015</Year>
<Month>8</Month>
<Day>14</Day>
<Hour>6</Hour>
<Minute>0</Minute>
</PubMedPubDate>
<PubMedPubDate PubStatus="pubmed">
<Year>2015</Year>
<Month>8</Month>
<Day>14</Day>
<Hour>6</Hour>
<Minute>0</Minute>
</PubMedPubDate>
<PubMedPubDate PubStatus="medline">
<Year>2015</Year>
<Month>8</Month>
<Day>14</Day>
<Hour>6</Hour>
<Minute>0</Minute>
</PubMedPubDate>
</History>
<PublicationStatus>aheadofprint</PublicationStatus>
<ArticleIdList>
<ArticleId IdType="pii">ocv062</ArticleId>
<ArticleId IdType="doi">10.1093/jamia/ocv062</ArticleId>
<ArticleId IdType="pubmed">26269536</ArticleId>
</ArticleIdList>
</PubmedData>
</PubmedArticle>
<PubmedArticle>
<MedlineCitation Owner="NLM" Status="MEDLINE">
<PMID Version="1">5819388</PMID>
<DateCreated>
<Year>1969</Year>
<Month>08</Month>
<Day>22</Day>
</DateCreated>
<DateCompleted>
<Year>1969</Year>
<Month>08</Month>
<Day>22</Day>
</DateCompleted>
<DateRevised>
<Year>2007</Year>
<Month>11</Month>
<Day>15</Day>
</DateRevised>
<Article PubModel="Print">
<Journal>
<ISSN IssnType="Print">0026-1270</ISSN>
<JournalIssue CitedMedium="Print">
<Volume>8</Volume>
<Issue>2</Issue>
<PubDate>
<Year>1969</Year>
<Month>Apr</Month>
</PubDate>
</JournalIssue>
<Title>Methods of information in medicine</Title>
<ISOAbbreviation>Methods Inf Med</ISOAbbreviation>
</Journal>
<ArticleTitle>Identification and transformation of terminal morphemes in medical English.</ArticleTitle>
<Pagination>
<MedlinePgn>84-90</MedlinePgn>
</Pagination>
<AuthorList CompleteYN="Y">
<Author ValidYN="Y">
<LastName>Pratt</LastName>
<ForeName>A W</ForeName>
<Initials>AW</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Pacak</LastName>
<ForeName>M</ForeName>
<Initials>M</Initials>
</Author>
</AuthorList>
<Language>eng</Language>
<PublicationTypeList>
<PublicationType UI="D016428">Journal Article</PublicationType>
</PublicationTypeList>
</Article>
<MedlineJournalInfo>
<Country>GERMANY, WEST</Country>
<MedlineTA>Methods Inf Med</MedlineTA>
<NlmUniqueID>0210453</NlmUniqueID>
<ISSNLinking>0026-1270</ISSNLinking>
</MedlineJournalInfo>
<CitationSubset>IM</CitationSubset>
<MeshHeadingList>
<MeshHeading>
<DescriptorName MajorTopicYN="Y" UI="D000043">Abstracting and Indexing as Topic</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="Y" UI="D003201">Computers</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="Y" UI="D008037">Linguistics</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N" UI="D009316">National Institutes of Health (U.S.)</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N" Type="Geographic" UI="D014481">United States</DescriptorName>
</MeshHeading>
</MeshHeadingList>
</MedlineCitation>
<PubmedData>
<History>
<PubMedPubDate PubStatus="pubmed">
<Year>1969</Year>
<Month>4</Month>
<Day>1</Day>
</PubMedPubDate>
<PubMedPubDate PubStatus="medline">
<Year>1969</Year>
<Month>4</Month>
<Day>1</Day>
<Hour>0</Hour>
<Minute>1</Minute>
</PubMedPubDate>
<PubMedPubDate PubStatus="entrez">
<Year>1969</Year>
<Month>4</Month>
<Day>1</Day>
<Hour>0</Hour>
<Minute>0</Minute>
</PubMedPubDate>
</History>
<PublicationStatus>ppublish</PublicationStatus>
<ArticleIdList>
<ArticleId IdType="pubmed">5819388</ArticleId>
<ArticleId IdType="pii">69020084</ArticleId>
</ArticleIdList>
</PubmedData>
</PubmedArticle>
</PubmedArticleSet>
这是有问题的部分代码(仅)。如果此摘录有轻微的语法错误或 2,请原谅我。提前感谢您的任何/所有建议。我希望这里的解决方案对任何被 PubMed for JBosses 欺负的人有用。
##########################
# PUBMED XML TO DATAFRAME
########################
library(XML)
library(reshape)
# initializes save path to XML files
dir <- paste0("pubmed_",input.dir)
myxml.path = file.path(dir, dir(dir, ".xml"))
## function to process xml files in the given save path
pub.data <- lapply(myxml.path, function(x){
myxml <- xmlParse(x, useInternalNodes = TRUE)
## function(s) to pad XML columns with ""
padXML <-function(x,xstr){
res<-xpathSApply(x,xstr,xmlValue)
if(length(res)==0){
out<-""
}else{
out<-res
}
out
}
# padXML <-function(x, xstr){
# res <- xpathSApply(x, xstr, xmlValue)
# ifelse(try(length(res)==0, silent=TRUE), "NA",
# ifelse(length(res)==0, "",
# ifelse(length(res)>1, paste(res, collapse=", "), res)))
# }
# padXML <-function(x, xstr){
# res <- tryCatch(xpathSApply(x, xstr, xmlValue), error=function(e) print(""))
# return(res)
# }
## Extract XML paths
PMID <- xpathSApply(myxml,"//*/MedlineCitation",padXML,xstr='./PMID/text()')
ArticleTitle <- xpathSApply(myxml,"//*/MedlineCitation",padXML,xstr='./Article[1]/ArticleTitle[1]/text()')
DateCreated <- xpathSApply(myxml,"//*/MedlineCitation",padXML,xstr='./DateCreated/Year/text()')
Keyword1 < - xpathSApply(myxml,"//*/MedlineCitation",padXML,xstr='./KeywordList[1]/Keyword[1]/text()')
Keyword2 < - xpathSApply(myxml,"//*/MedlineCitation",padXML,xstr='./KeywordList[1]/Keyword[2]/text()')
Keyword3 < - xpathSApply(myxml,"//*/MedlineCitation",padXML,xstr='./KeywordList[1]/Keyword[3]/text()')
## builds dataframe from extracted XML paths
pub.data <- data.frame(
PMID,
DateCreated,
ArticleTitle,
Keyword1,
Keyword2,
Keyword3,
stringsAsFactors=FALSE)
})
pub.data <- merge_all(pub.data)
return(pub.data)
参考资料:
【问题讨论】:
-
你写这个问题显然付出了很多努力。你能把它归结为问题的本质吗?例如,您能否确定一个重现问题的
myxml实例,然后发布该问题,而不是所有这些代码。 -
您好,感谢您的回复。我根据您的评论编辑了我的帖子。代码现在更短了,但结果我不得不添加生成的 XML。但是,我希望这种安排更易于管理。提前致谢。
-
这可能是微不足道的。在这三个作业中,
<和-之间有一个空格(如< -而不是<-)。当我修复它运行时。 -
@jlhoward 哇。非常感谢你。令人惊讶的是,经验丰富的程序员和新手在发现此类事情时的区别。我希望,至少,这篇文章对其他人有用。
-
其实没那么神奇。在偶然发现之前,我花了很多时间弄乱 xPath...
标签: r xpath error-handling xml-parsing dataframe