#' search in "NHANES" database
#'
#' @param ... one or more keywords, use look() function to search.
#' @param cat logical. whether to show results number
#' @return a dataframe
#' @export
#'
#' @examples
#' # x <- nhs_search('diabetes')
nhs_search <- function(...,cat=TRUE,fileds=NULL){
key <- c(...)
vartext <- paste0(get_config_path(),'/varLabel.txt')
if (!file.exists(vartext)) build_varLabel() # build
varLabel <- data.table::fread(vartext,data.table = FALSE)
if (is.null(key)) return(varLabel)
if (is.null(fileds)){
fileds <- set::not(colnames(varLabel),'url')
}else{
ck <- sapply(set::not(colnames(varLabel),'url'), function(i) any(sapply(fileds, function(j) do::left_equal(i,j))))
fileds <- colnames(varLabel)[ck]
}
index <- paste0_columns(varLabel[,fileds,drop=FALSE],'------')
ck <- lookl(index,tolower(key),ignore.case = TRUE)
df <- varLabel[ck,]
if (nrow(df)==0){
cat('results: ',nrow(df))
return()
}
nchar <- df
for (i in 1:ncol(df)) {
for (j in 1:nrow(df)) {
nchar[j,i] <- tryCatch(nchar(df[j,i]),error=function(e) 1)
}
nchar[,i] <- as.numeric(nchar[,i])
}
if (is.data.frame(nchar) | is.matrix(nchar)){
df <- df[, nchar |> colSums() > 0,drop=FALSE]
}else{
df <- df[, nchar > 0,drop=FALSE]
}
class(df) <- c('nhs_search','data.frame')
if (cat) cat('results: ',nrow(df))
keys <- strsplit(key,'\\|') |> unlist() |> unique() |> do::Trim_left(c(' ','~','!','=')) |> unique()
attr(df,'nhs_search') <- keys
df
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.