如何解决是否有用于填写在线表格的 R 函数?
我正在尝试填写在线 form 并抓取结果。 使用 Rselenium 我可以为一行填充数据:
library(RSelenium)
library(xml2)
library(tidyverse)
library(rvest)
# Start Selenium Server --------------------------------------------------------
# https://docs.ropensci.org/RSelenium/articles/basics.html#connecting-to-a-selenium-server-1
# https://www.akipredictor.com/en/aki_predictor/
rD <- rsDriver(browser="firefox",port=4545L,verbose=F)
remDr <- rD[["client"]]
# form ------------------------------------------------------------------
remDr$navigate('https://www.akipredictor.com/en/aki_predictor/')
remDr$findElement(using = "name",value = "agree_to_legal_terms")$clickElement()
#Pre-admission information
webElemAge <- remDr$findElement(using = "name",value = "age")
webElemAge$sendKeystoElement(list("70"))
webElemBaselinesCreat <- remDr$findElement(using = "name",value = "baseline_screat")
webElemBaselinesCreat$sendKeystoElement(list("1"))
webElemIsDiabetic <- remDr$findElement(using = "name",value = "is_diabetic")
webElemIsDiabetic$sendKeystoElement(list("Yes"))
webElemIsElectiveAdmited <- remDr$findElement(using = "name",value = "is_elective_admitted")
webElemIsElectiveAdmited$sendKeystoElement(list("Unplanned admission"))
webElemTypeOfSurgery <- remDr$findElement(using = "name",value = "type_of_surgery")
webElemTypeOfSurgery$sendKeystoElement(list("Transplant surgery"))
# ICU admission information
remDr$findElement(using = "name",value = "show_admission")$clickElement()
webElemBloodglucose <- remDr$findElement(using = "name",value = "blood_glucose")
webElemBloodglucose$sendKeystoElement(list("200"))
webElemHasSUSPECTedSepsis <- remDr$findElement(using = "name",value = "has_SUSPECTed_sepsis")
webElemHasSUSPECTedSepsis$sendKeystoElement(list("Yes"))
webElemHDSupport <- remDr$findElement(using = "name",value = "hd_support")
webElemHDSupport$sendKeystoElement(list("Pharmacological"))
# Day 1 information
remDr$findElement(using = "name",value = "show_day1")$clickElement()
webElemCreatinineD1 <- remDr$findElement(using = "name",value = "creatinine_d1")
webElemCreatinineD1$sendKeystoElement(list("1.2"))
webElemApacheIID1 <- remDr$findElement(using = "name",value = "apacheII_d1")
webElemApacheIID1$sendKeystoElement(list("30"))
webElemmaxLactateD1 <- remDr$findElement(using = "name",value = "max_lactate_d1")
webElemmaxLactateD1$sendKeystoElement(list("10"))
webElemBilirrubinD1 <- remDr$findElement(using = "name",value = "bilirubin_d1")
webElemBilirrubinD1$sendKeystoElement(list("2"))
webElemHoursOfICUStay <- remDr$findElement(using = "name",value = "hours_of_icu_stay")
webElemHoursOfICUStay$sendKeystoElement(list("24"))
remDr$findElement(using = "name",value = "predict_day1_dev")$clickElement()
# extract HTML -----------------
Sys.sleep(5) # give the page time to fully load
html <- remDr$getPageSource()[[1]]
results <- read_html(html) %>% # parse HTML
html_nodes("div") %>% # extract node"
.[12] %>%
.[[1]] %>%
html_text() # string
results <- results %>%
str_replace_all("\n","") %>%
str_replace_all(" ","") %>%
str_replace_all("RiskofdevelopingAKIduringthefirstweekofICUstay","") %>%
str_replace_all("AdvancedoptionsChoosetheclassificationthresholdClickonthequestionmarkforadditionalexplanationSincethepredictedriskisabovethechosenclassificationthreshold,thepatientisclassifiedasdevelopingAKIwithinthefirstweekofICUstay.DependingontheintendeduSEOftheAKIpredictor,theusermaychoosetoadapttheclassificationthreshold,andevaluatetheeffectonthestatisticsbelow.Thedefaultclassificationthresholdof14.5maximizedbothsensitivityandspecificityinthestudieddatabase.Sensitivity:63.8%specificity:81.9%PPV:38.0%NPV:92.8%ΔNetbenefitNone:6.8%ΔNetbenefitAll:6.4%Atthechosenclassificationthreshold,AKIpredictorcorrectlyidentifies63.8%ofthepatientswhodevelopedAKIinthestudieddatabaseAtthechosenclassificationthreshold,AKIpredictorcorrectlyidentifies81.9%ofthepatientswhodidnotdevelopAKIinthestudieddatabaseInthestudieddatabase,38.0%ofthepatientswhodevelopedAKIhadapredictedriskabovethechosenclassificationthresholdInthestudieddatabase,92.8%ofthepatientswhodidnotdevelopAKIhadapredictedriskbelowthechosenclassificationthresholdAtthechosenclassificationthreshold,AKIpredictorincreasesthepercentageofcorrectlyidentifiedAKIby6.8%inthestudieddatabase,withoutincreasingfalseclassifications,ascomparedtoconsideringnopatientwilldevelopAKI.OnlyuseaclassificationthresholdthatresultsinaΔNetbenefitNone>0Atthechosenclassificationthreshold,AKIpredictordecreasesthepercentageofmisclassifiedAKIby6.4%inthestudieddatabase,whilekeepingthesamenumberofcorrectclassifications,ascomparedtoconsideringallpatientswilldevelopAKIOnlyuseaclassificationthresholdthatresultsinaΔNetbenefitAll>0Clickonthestatisticsfordetails","")
results
remDr$close()
我需要使用数据框中的数据执行相同的过程。我尝试了以下代码:
rD <- rsDriver(browser="firefox",port=4560L,verbose=F)
remDr <- rD[["client"]]
remDr$navigate('https://www.akipredictor.com/en/aki_predictor/')
scrape.AKIpredictor <- function(age,baselinesCreat,IsDiabetic,IsElectiveAdmited,TypeOfSurgery,glucose,SUSPECTedSepsis,HDSupport,CreatinineD1,ApacheIID1,MaxLactateD1,BilirrubinD1,HoursOfICUStay) {
remDr$findElement(using = "name",value = "agree_to_legal_terms")$clickElement()
#Pre-admission information
webElemAge <- remDr$findElement(using = "name",value = "age")
webElemAge$sendKeystoElement(list(age))
webElemBaselinesCreat <- remDr$findElement(using = "name",value = "baseline_screat")
webElemBaselinesCreat$sendKeystoElement(list(baselinesCreat))
webElemIsDiabetic <- remDr$findElement(using = "name",value = "is_diabetic")
webElemIsDiabetic$sendKeystoElement(list(IsDiabetic))
webElemIsElectiveAdmited <- remDr$findElement(using = "name",value = "is_elective_admitted")
webElemIsElectiveAdmited$sendKeystoElement(list(IsElectiveAdmited))
webElemTypeOfSurgery <- remDr$findElement(using = "name",value = "type_of_surgery")
webElemTypeOfSurgery$sendKeystoElement(list(TypeOfSurgery))
# ICU admission information
remDr$findElement(using = "name",value = "show_admission")$clickElement()
webElemBloodglucose <- remDr$findElement(using = "name",value = "blood_glucose")
webElemBloodglucose$sendKeystoElement(list(glucose))
webElemHasSUSPECTedSepsis <- remDr$findElement(using = "name",value = "has_SUSPECTed_sepsis")
webElemHasSUSPECTedSepsis$sendKeystoElement(list(SUSPECTedSepsis))
webElemHDSupport <- remDr$findElement(using = "name",value = "hd_support")
webElemHDSupport$sendKeystoElement(list(HDSupport))
# Day 1 information
remDr$findElement(using = "name",value = "show_day1")$clickElement()
webElemCreatinineD1 <- remDr$findElement(using = "name",value = "creatinine_d1")
webElemCreatinineD1$sendKeystoElement(list(CreatinineD1))
webElemApacheIID1 <- remDr$findElement(using = "name",value = "apacheII_d1")
webElemApacheIID1$sendKeystoElement(list(ApacheIID1))
webElemmaxLactateD1 <- remDr$findElement(using = "name",value = "max_lactate_d1")
webElemmaxLactateD1$sendKeystoElement(list(MaxLactateD1))
webElemBilirrubinD1 <- remDr$findElement(using = "name",value = "bilirubin_d1")
webElemBilirrubinD1$sendKeystoElement(list(BilirrubinD1))
webElemHoursOfICUStay <- remDr$findElement(using = "name",value = "hours_of_icu_stay")
webElemHoursOfICUStay$sendKeystoElement(list(HoursOfICUStay))
remDr$findElement(using = "name",value = "predict_day1_dev")$clickElement()
Sys.sleep(5) # give the page time to fully load
html <- remDr$getPageSource()[[1]]
results <- read_html(html) %>% # parse HTML
html_nodes("div") %>% # extract node"
.[12] %>%
.[[1]] %>%
html_text() # string
results <- results %>% #trim trim trim
str_replace_all("\n","") %>%
str_replace_all(" ","") %>%
str_replace_all("RiskofdevelopingAKIduringthefirstweekofICUstay","") %>%
str_replace_all("AdvancedoptionsChoosetheclassificationthresholdClickonthequestionmarkforadditionalexplanationSincethepredictedriskisabovethechosenclassificationthreshold,"")
remDr$findElement(using = "name",value = "empty_form")$clickElement()
return(results)
}
#data frame
age <- c(50,70,80)
baselinesCreat <- c(1,1.5,1.1)
IsDiabetic <- c("Yes","No","Yes")
IsElectiveAdmited <- c("Planned admission","Unplanned admission","Planned admission")
TypeOfSurgery <- c("Transplant surgery","Cardiovascular surgery (non transplant)","Abdominal surgery")
glucose <- c(200,180,140)
SUSPECTedSepsis <- c("Yes","Yes")
HDSupport <- c("None","Mechanical","Pharmacological")
CreatinineD1 <- c(1.1,1.6,1.2)
ApacheIID1 <- c(30,40,10)
MaxLactateD1 <-c(10,15,5)
BilirrubinD1 <- c(2,3,4)
HoursOfICUStay <- c(24,24,24)
data <- data.frame(age,HoursOfICUStay)
scrape.AKIpredictor(data$age,data$baselinesCreat,data$IsDiabetic,data$IsElectiveAdmited,data$TypeOfSurgery,data$glucose,data$SUSPECTedSepsis,data$HDSupport,data$CreatinineD1,data$ApacheIID1,data$MaxLactateD1,data$BilirrubinD1,data$HoursOfICUStay)
但是我收到以下错误:
处理命令时发生未知的服务器端错误。
使用 for 循环尝试了以下代码
remDr$navigate('https://www.akipredictor.com/en/aki_predictor/')
remDr$findElement(using = "name",value = "agree_to_legal_terms")$clickElement()
Sys.sleep(10)
#Set start,end and steps
for(row in 1:nrow(data)) {
#Pre-admission information
webElemAge <- remDr$findElement(using = "name",value = "age")
webElemAge$sendKeystoElement(list(data[row,"age"]))
webElemBaselinesCreat <- remDr$findElement(using = "name",value = "baseline_screat")
webElemBaselinesCreat$sendKeystoElement(list(data[row,"baselinesCreat"]))
webElemIsDiabetic <- remDr$findElement(using = "name",value = "is_diabetic")
webElemIsDiabetic$sendKeystoElement(list(data[row,"IsDiabetic"]))
webElemIsElectiveAdmited <- remDr$findElement(using = "name",value = "is_elective_admitted")
webElemIsElectiveAdmited$sendKeystoElement(list(data[row,"IsElectiveAdmited"]))
webElemTypeOfSurgery <- remDr$findElement(using = "name",value = "type_of_surgery")
webElemTypeOfSurgery$sendKeystoElement( list(data[row,"TypeOfSurgery"]))
# ICU admission information
remDr$findElement(using = "name",value = "show_admission")$clickElement()
Sys.sleep(5)
webElemBloodglucose <- remDr$findElement(using = "name",value = "blood_glucose")
webElemBloodglucose$sendKeystoElement(list(data[row,"glucose"]))
webElemHasSUSPECTedSepsis <- remDr$findElement(using = "name",value = "has_SUSPECTed_sepsis")
webElemHasSUSPECTedSepsis$sendKeystoElement(list(data[row,"SUSPECTedSepsis"]))
webElemHDSupport <- remDr$findElement(using = "name",value = "hd_support")
webElemHDSupport$sendKeystoElement(list(data[row,"HDSupport"]))
# Day 1 information
remDr$findElement(using = "name",value = "show_day1")$clickElement()
Sys.sleep(5)
webElemCreatinineD1 <- remDr$findElement(using = "name",value = "creatinine_d1")
webElemCreatinineD1$sendKeystoElement(list(data[row,"CreatinineD1"]))
webElemApacheIID1 <- remDr$findElement(using = "name",value = "apacheII_d1")
webElemApacheIID1$sendKeystoElement(list(data[row,"ApacheIID1"]))
webElemmaxLactateD1 <- remDr$findElement(using = "name",value = "max_lactate_d1")
webElemmaxLactateD1$sendKeystoElement(list(data[row,"MaxLactateD1"]))
webElemBilirrubinD1 <- remDr$findElement(using = "name",value = "bilirubin_d1")
webElemBilirrubinD1$sendKeystoElement(list(data[row,"BilirrubinD1"]))
webElemHoursOfICUStay <- remDr$findElement(using = "name",value = "hours_of_icu_stay")
webElemHoursOfICUStay$sendKeystoElement(list(data[row,"HoursOfICUStay"]))
remDr$findElement(using = "name",value = "predict_day1_dev")$clickElement()
Sys.sleep(5) # give the page time to fully load
html <- remDr$getPageSource()[[1]]
results <- read_html(html) %>% # parse HTML
html_nodes("div") %>% # extract node"
.[12] %>%
.[[1]] %>%
html_text() # string
results[row,] <- results %>% #trim trim trim
str_replace_all("\n","") %>%
str_replace_all(" ","") %>%
str_replace_all("RiskofdevelopingAKIduringthefirstweekofICUstay","") %>%
str_replace_all("AdvancedoptionsChoosetheclassificationthresholdClickonthequestionmarkforadditionalexplanationSincethepredictedriskisabovethechosenclassificationthreshold,"")
remDr$findElement(using = "name",value = "empty_form")$clickElement()
results
}
这给了我以下错误:
Selenium 消息:类 java.lang.Long 不能转换为类 java.lang.CharSequence(java.lang.Long 和 java.lang.CharSequence 位于加载程序 'bootstrap' 的模块 java.base 中)
解决方法
Rselenium 只接受字符串,使用 for 循环解决了问题。
library(RSelenium)
library(xml2)
library(tidyverse)
library(rvest)
age <- c(50,70,80)
baselineSCreat <- c(1,1.5,1.1)
IsDiabetic <- c("Yes","No","Yes")
IsElectiveAdmited <- c("Planned admission","Unplanned admission","Planned admission")
TypeOfSurgery <- c("Transplant surgery","Cardiovascular surgery (non transplant)","Abdominal surgery")
Glucose <- c(200,180,140)
SuspectedSepsis <- c("Yes","Yes")
HDSupport <- c("None","Mechanical","Pharmacological")
CreatinineD1 <- c(1.1,1.6,1.2)
ApacheIID1 <- c(30,40,10)
MaxLactateD1 <-c(10,15,5)
BilirrubinD1 <- c(2,3,4)
HoursOfICUStay <- c(24,24,24)
data <- data.frame(age,baselineSCreat,IsDiabetic,IsElectiveAdmited,TypeOfSurgery,Glucose,SuspectedSepsis,HDSupport,CreatinineD1,ApacheIID1,MaxLactateD1,BilirrubinD1,HoursOfICUStay)
data <- sapply(data,as.character)
rD <- rsDriver(browser="firefox",port=4560L,verbose=F)
remDr <- rD[["client"]]
remDr$navigate('https://www.akipredictor.com/en/aki_predictor/')
remDr$findElement(using = "name",value = "agree_to_legal_terms")$clickElement()
output <- matrix(ncol=1,nrow=nrow(data))
Sys.sleep(5)
#Set start,end and steps
for(row in 1:nrow(data)) {
#Pre-admission information
webElemAge <- remDr$findElement(using = "name",value = "age")
webElemAge$sendKeysToElement(list(data[row,"age"]))
webElemBaselineSCreat <- remDr$findElement(using = "name",value = "baseline_screat")
webElemBaselineSCreat$sendKeysToElement(list(data[row,"baselineSCreat"]))
webElemIsDiabetic <- remDr$findElement(using = "name",value = "is_diabetic")
webElemIsDiabetic$sendKeysToElement(list(data[row,"IsDiabetic"]))
webElemIsElectiveAdmited <- remDr$findElement(using = "name",value = "is_elective_admitted")
webElemIsElectiveAdmited$sendKeysToElement(list(data[row,"IsElectiveAdmited"]))
webElemTypeOfSurgery <- remDr$findElement(using = "name",value = "type_of_surgery")
webElemTypeOfSurgery$sendKeysToElement( list(data[row,"TypeOfSurgery"]))
# ICU admission information
remDr$findElement(using = "name",value = "show_admission")$clickElement()
Sys.sleep(1)
webElemBloodGlucose <- remDr$findElement(using = "name",value = "blood_glucose")
webElemBloodGlucose$sendKeysToElement(list(data[row,"Glucose"]))
webElemHasSuspectedSepsis <- remDr$findElement(using = "name",value = "has_suspected_sepsis")
webElemHasSuspectedSepsis$sendKeysToElement(list(data[row,"SuspectedSepsis"]))
webElemHDSupport <- remDr$findElement(using = "name",value = "hd_support")
webElemHDSupport$sendKeysToElement(list(data[row,"HDSupport"]))
# Day 1 information
remDr$findElement(using = "name",value = "show_day1")$clickElement()
Sys.sleep(1)
webElemCreatinineD1 <- remDr$findElement(using = "name",value = "creatinine_d1")
webElemCreatinineD1$sendKeysToElement(list(data[row,"CreatinineD1"]))
webElemApacheIID1 <- remDr$findElement(using = "name",value = "apacheII_d1")
webElemApacheIID1$sendKeysToElement(list(data[row,"ApacheIID1"]))
webElemMaxLactateD1 <- remDr$findElement(using = "name",value = "max_lactate_d1")
webElemMaxLactateD1$sendKeysToElement(list(data[row,"MaxLactateD1"]))
webElemBilirrubinD1 <- remDr$findElement(using = "name",value = "bilirubin_d1")
webElemBilirrubinD1$sendKeysToElement(list(data[row,"BilirrubinD1"]))
webElemHoursOfICUStay <- remDr$findElement(using = "name",value = "hours_of_icu_stay")
webElemHoursOfICUStay$sendKeysToElement(list(data[row,"HoursOfICUStay"]))
remDr$findElement(using = "name",value = "predict_day1_dev")$clickElement()
Sys.sleep(1) # give the page time to fully load
html <- remDr$getPageSource()[[1]]
output[row,] <- read_html(html) %>% # parse HTML
html_nodes("div") %>% # extract node"
.[12] %>%
.[[1]] %>%
html_text() # string
output[row,] <- output[row,] %>% #trim trim trim
str_replace_all("\n","") %>%
str_replace_all(" ","") %>%
str_replace_all("RiskofdevelopingAKIduringthefirstweekofICUstay","") %>%
str_replace_all("AdvancedoptionsChoosetheclassificationthresholdClickonthequestionmarkforadditionalexplanationSincethepredictedriskisabovethechosenclassificationthreshold,thepatientisclassifiedasdevelopingAKIwithinthefirstweekofICUstay.DependingontheintendeduseoftheAKIpredictor,theusermaychoosetoadapttheclassificationthreshold,andevaluatetheeffectonthestatisticsbelow.Thedefaultclassificationthresholdof14.5maximizedbothsensitivityandspecificityinthestudieddatabase.Sensitivity:63.8%Specificity:81.9%PPV:38.0%NPV:92.8%ΔNetbenefitNone:6.8%ΔNetbenefitAll:6.4%Atthechosenclassificationthreshold,AKIpredictorcorrectlyidentifies63.8%ofthepatientswhodevelopedAKIinthestudieddatabaseAtthechosenclassificationthreshold,AKIpredictorcorrectlyidentifies81.9%ofthepatientswhodidnotdevelopAKIinthestudieddatabaseInthestudieddatabase,38.0%ofthepatientswhodevelopedAKIhadapredictedriskabovethechosenclassificationthresholdInthestudieddatabase,92.8%ofthepatientswhodidnotdevelopAKIhadapredictedriskbelowthechosenclassificationthresholdAtthechosenclassificationthreshold,AKIpredictorincreasesthepercentageofcorrectlyidentifiedAKIby6.8%inthestudieddatabase,withoutincreasingfalseclassifications,ascomparedtoconsideringnopatientwilldevelopAKI.OnlyuseaclassificationthresholdthatresultsinaΔNetbenefitNone>0Atthechosenclassificationthreshold,AKIpredictordecreasesthepercentageofmisclassifiedAKIby6.4%inthestudieddatabase,whilekeepingthesamenumberofcorrectclassifications,ascomparedtoconsideringallpatientswilldevelopAKIOnlyuseaclassificationthresholdthatresultsinaΔNetbenefitAll>0Clickonthestatisticsfordetails","")
remDr$findElement(using = "name",value = "empty_form")$clickElement()
}
output
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。