用户定义的函数在 dplyr 管道中不起作用

net*_*lak 5 r function bioinformatics ncbi dplyr

我有一个带有蛋白质登录号的数据集 (DataGranulomeTidy)。我在 r 中编写了一个函数 (extractInfo) 来从 ncbi 网站上抓取这些蛋白质的一些信息。当我在一个简短的“for”循环中运行它时,该函数按预期工作。

DataGranulomeTidy <- tibble(GIaccessionNumber = c("29436380", "4504165", "17318569"))

extractInfo <- function(GInumber){
    tempPage <- readLines(paste("https://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?id=", GInumber, "&db=protein&report=genpept&conwithfeat=on&withparts=on&show-cdd=on&retmode=html&withmarkup=on&tool=portal&log$=seqview&maxdownloadsize=1000000", sep = ""), skipNul = TRUE)
    tempPage  <- base::paste(tempPage, collapse = "")
    Accession <- str_extract(tempPage, "(?<=ACCESSION).{3,20}(?=VERSION)")
    Symbol    <- str_extract(tempPage, "(?<=gene=\").{1,20}(?=\")")
    GeneID    <- str_extract(tempPage, "(?<=gov/gene/).{1,20}(?=\">)")
    out       <- paste(Symbol, Accession, GeneID, sep = "---")
    return(out)
}


for(n in 1:3){
    print(extractInfo(GInumber = DataGranulomeTidy$GIaccessionNumber[n]))
}
 [1] "MYH9---   AAH49849---4627"
 [1] "GSN---   NP_000168---2934"
 [1] "KRT1---   NP_006112---3848"
Run Code Online (Sandbox Code Playgroud)

当我在 dplyr 管道中使用相同的功能时,我不起作用,我无法弄清楚为什么。

 > DataGranulomeTidy %>% mutate(NewVar = extractInfo(.$GIaccessionNumber))
 Error in file(con, "r") : argumento 'description' inválido
Run Code Online (Sandbox Code Playgroud)

在这一点上,我可以通过使用“for”运算符在不使用“管道”运算符的情况下使事情工作,但我非常想了解为什么该函数在 dplyr 管道中不起作用。

cut*_*h44 4

这就是你的UDF无法处理向量的原因。

vectorized_extractInfo <- Vectorize(extractInfo, "GInumber")

DataGranulomeTidy %>% 
  mutate(NewVar = vectorized_extractInfo(GIaccessionNumber))
Run Code Online (Sandbox Code Playgroud)