Skip to content

Commit

Permalink
Revert "switched NA to NA_character_"
Browse files Browse the repository at this point in the history
This reverts commit 04a1399.
  • Loading branch information
Aariq committed Jun 12, 2020
1 parent 04a1399 commit 82e13d6
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions R/chemid.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ci_query <- function(query, from = c('name', 'rn', 'inchikey', 'cas'),
on.exit(suppressWarnings(closeAllConnections()))
if (is.na(query)) {
message('query is NA! Returning NA.\n')
return(NA_character_)
return(NA)
}
query <- URLencode(query)
baseurl <- switch(
Expand All @@ -82,14 +82,14 @@ ci_query <- function(query, from = c('name', 'rn', 'inchikey', 'cas'),
ttt <- try(read_html(qurl), silent = TRUE)
if (inherits(ttt, 'try-error')) {
message('Not found! Returning NA.\n')
return(NA_character_)
return(NA)
}

tit <- xml_text(xml_find_all(ttt, "//head/title"))
no <- xml_text(xml_find_all(ttt, "//h3"))
if (length(no) != 0 && 'The following query produced no records:' %in% no) {
message('Not found! Returning NA.\n')
return(NA_character_)
return(NA)
}

# handle multiple inputs
Expand Down Expand Up @@ -124,7 +124,7 @@ ci_query <- function(query, from = c('name', 'rn', 'inchikey', 'cas'),
if (match == 'na') {
if (verbose)
message("Returning NA. \n")
return(NA_character_)
return(NA)
}

if (match == 'ask') {
Expand All @@ -133,7 +133,7 @@ ci_query <- function(query, from = c('name', 'rn', 'inchikey', 'cas'),
message("\nEnter rownumber of compounds (other inputs will return 'NA'):\n") # prompt
take <- as.numeric(scan(n = 1, quiet = TRUE))
if (length(take) == 0) {
return(NA_character_)
return(NA)
}
if (take %in% seq_len(nrow(tochoose))) {
hit_cas <- hit_cas[take]
Expand All @@ -146,7 +146,7 @@ ci_query <- function(query, from = c('name', 'rn', 'inchikey', 'cas'),
if (is.na(hit_cas)) {
if (verbose)
message('CAS not found! Returning NA.\n')
return(NA_character_)
return(NA)
}

# retry with CAS-API
Expand All @@ -163,55 +163,55 @@ ci_query <- function(query, from = c('name', 'rn', 'inchikey', 'cas'),
}

if(is.na(xml_find_first(ttt, "//h3[contains(., 'Name of Substance')]/following-sibling::div[1]//li"))){
name <- NA_character_
name <- NA
}else{
name <- xml_text(xml_find_all(ttt, "//h3[contains(., 'Name of Substance')]/following-sibling::div[1]//li"))
}

if(is.na(xml_find_first(ttt, "//h3[contains(., 'Synonyms')]/following-sibling::div[1]//li"))){
synonyms <- NA_character_
synonyms <- NA
}else{
synonyms <- xml_text(xml_find_all(ttt, "//h3[contains(., 'Synonyms')]/following-sibling::div[1]//li"))
}

if(is.na(xml_find_first(ttt, "//h3[contains(., 'CAS Registry')]/following-sibling::ul[1]//li"))){
cas <- NA_character_
cas <- NA
} else {
cas <- xml_text(xml_find_all(ttt, "//h3[contains(., 'CAS Registry')]/following-sibling::ul[1]//li"))
}

if(is.na(xml_find_first(ttt, "//h3[contains(., 'InChI')]/following-sibling::text()[1]"))){
inchi <- NA_character_
inchi <- NA
} else {
inchi <- gsub('\\n|\\t', '',
xml_text(xml_find_all(ttt, "//h3[contains(., 'InChI')]/following-sibling::text()[1]"))[1]
)
}

if(is.na(xml_find_first(ttt, "//h3[contains(., 'InChIKey')]/following-sibling::text()[1]"))){
inchikey <- NA_character_
inchikey <- NA
} else {
inchikey <- gsub('\\n|\\t|\\r', '',
xml_text(xml_find_all(ttt, "//h3[contains(., 'InChIKey')]/following-sibling::text()[1]"))
)
}

if(is.na(xml_find_first(ttt, "//h3[contains(., 'Smiles')]/following-sibling::text()[1]"))){
smiles <- NA_character_
smiles <- NA
} else {
smiles <- gsub('\\n|\\t|\\r', '',
xml_text(xml_find_all(ttt, "//h3[contains(., 'Smiles')]/following-sibling::text()[1]"))
)
}

if(is.na(xml_find_first(ttt, "//h2[contains(., 'Toxicity')]/following-sibling::div//table"))){
toxicity <- NA_character_
toxicity <- NA
} else {
toxicity <- html_table(xml_find_all(ttt, "//h2[contains(., 'Toxicity')]/following-sibling::div//table"))[[1]]
}

if(is.na(xml_find_first(ttt, "//h2[contains(., 'Physical Prop')]/following-sibling::div//table"))){
physprop <- NA_character_
physprop <- NA
} else {
physprop <- html_table(xml_find_all(ttt, "//h2[contains(., 'Physical Prop')]/following-sibling::div//table"))[[1]]
physprop[ , 'Value'] <- as.numeric(physprop[ , 'Value'])
Expand Down

0 comments on commit 82e13d6

Please sign in to comment.