R2-14 2阶段2

lucyylx 2018-01-26 15:41:45 阅读: 1285

任务一:

> library(DBI)

> library(RMySQL)

> con <- dbConnect(MySQL(),dbname="trymore",username="root",password="...")

> t_demo<-data.frame(a=seq(1:10), b=letters[1:10], c=rnorm(10))

> dbWriteTable(con, "t_demo", t_demo)

[1] TRUE

> dbListTables(con)

[1] "article" "t_demo"  "word"


任务二:

> con <- dbConnect(MySQL(),dbname="trymore",username="root",password="...")

> library(RISmed)

> cell2017<-EUtilsSummary("cell[TA] AND 2017[DP]")

> data<-QueryId(cell2017)

summary(cell2017)

Result count:  562

> pmid<- as.numeric(data)

> mylist<- list(id=1:562,pmid=pmid)

> mylist=as.data.frame(mylist)

> dbWriteTable(con,"article",mylist,append=T,row.names=F)

图片_1.jpg




任务三:

> library(RMySQL)

> library(xml2)

> library(httr)

> library(stringr)

> con <- dbConnect(MySQL(),dbname="rdbb",username="root",password="199221")

> dbSendQuery(con,'SET NAMES utf8')

<MySQLResult:1364425037,580,0>

> rs <- dbSendQuery(con, "SELECT * FROM article WHERE isdone=0")

> while (!dbHasCompleted(rs)){

+     chunk<-dbFetch(rs,10)

+     pmid<- chunk[,3]

+     pmidstr<-paste(pmid,sep = "",collapse = ",")

+     postFetchUrl='https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi'

+     r2 <- POST(postFetchUrl,body=list(db='pubmed',id=pmidstr,retmode='xml'))

+     stop_for_status(r2)

+     data2=content(r2, "parsed", "application/xml")

+     article=xml_children(data2)

+     count=length(article)

+     cnt=1

+     while(cnt<=count){

+         title=xml_text(xml_find_first(article[cnt],".//ArticleTitle"))

+         abstract=xml_text(xml_find_first(article[cnt],".//AbstractText"))

+         pmid=xml_text(xml_find_first(article[cnt],".//PMID"))

+         title=str_replace_all(title,"\'", "")

+         abstract=str_replace_all(abstract,"\'", "")

+         sql=paste("UPDATE article SET title='",title,"',abstract='",abstract,"',isdone='1'"," where pmid='",pmid,"'",sep="")

+         con2 <- dbConnect(MySQL(),host="localhost",dbname="rdbb",user="root",password="199221")

+         dbSendQuery(con2,'SET NAMES utf8')

+         dbSendQuery(con2,sql)

+         dbDisconnect(con2)

+         cnt = cnt + 1

+         Sys.sleep(1)

+     }

+ }


结果:

1.jpg

报错:

2.jpg




 

 

 

 
邀请讨论

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
{{item.nick_name}} 受邀请回答 {{item.create_time}}
{{item.refer_comment.nick_name}} {{item.refer_comment.create_time}}

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
切换到完整回复 发送回复
赞({{item.count_zan}}) 踩({{item.count_cai}}) 删除 回复 关闭
科研狗©2015-2024 科研好助手,京ICP备20005780号-1 建议意见

服务热线

178 0020 3020

微信服务号