RSiteSearch不适用于多字查询

时间:2019-02-16 17:06:57

标签: r

我是R语言编程的新手,并且正在关注"Learning R" by Richard Cotton这本书。但是,当我尝试使用搜索功能RSiteSearch时,查询中的空格将替换为“ +”。例如,"Naive Bayes"将变成"Naive+Bayes",这导致搜索引擎找到0个结果。谁能帮我这个?我已经尝试将这些单词括在大括号中,例如:"{Naive Bayes}"作为书中和互联网显示中的示例,但这似乎无济于事。

1 个答案:

答案 0 :(得分:1)

我同意,它无法按照文档中所述进行操作。如果您查看该函数的源代码,则似乎有一个错误。

function (string, restrict = c("functions", "vignettes", "views"), 
  format = c("normal", "short"), sortby = c("score", "date:late", 
    "date:early", "subject", "subject:descending", "from", 
    "from:descending", "size", "size:descending"), matchesPerPage = 20) 
{
  string <- paste0("http://search.r-project.org/cgi-bin/namazu.cgi?query=", 
    URLencode(gsub(" ", "+", string), reserved = TRUE))
  mpp <- paste0("max=", matchesPerPage)
  format <- paste0("result=", match.arg(format))
  restrictVALS <- c("functions", "vignettes", "views")
  restr <- match.arg(restrict, choices = restrictVALS, several.ok = TRUE)
  restr <- paste(paste0("idxname=", restr), collapse = "&")
  sortby <- match.arg(sortby)
  sortby <- paste0("sort=", switch(sortby, score = , `date:late` = , 
    `date:early` = sortby, subject = "field:subject:ascending", 
    `subject:descending` = "field:subject:descending", from = "field:from:ascending", 
    `from:descending` = "field:from:descending", size = "field:size:ascending", 
    `size:descending` = "field:size:descending"))
  qstring <- paste(string, mpp, format, sortby, restr, sep = "&")
  browseURL(qstring)
  cat(gettextf("A search query has been submitted to %s", 
    "http://search.r-project.org"), "\n", sep = "")
  cat(gettext("The results page should open in your browser shortly\n"))
  invisible(qstring)
}

URLencode(gsub(" ", "+", string), reserved = TRUE)似乎有问题。这样会添加+,但是会在我们不需要的查询中转义符号。

我也没有在源代码中看到任何用于处理花括号的代码,这似乎很奇怪。我在这里更改了功能,该功能似乎可以按照文档中的说明工作。

new_search = function (string, restrict = c("functions", "vignettes", "views"), 
          format = c("normal", "short"), sortby = c("score", "date:late", 
                                                    "date:early", "subject", "subject:descending", "from", 
                                                    "from:descending", "size", "size:descending"), matchesPerPage = 20) 
{

  if(grepl("[{}]",string)){
    string = gsub("[{}]","",string)
    reserved <- TRUE
  }else{
    reserved <- FALSE
  }

  string <- paste0("http://search.r-project.org/cgi-bin/namazu.cgi?query=", 
                   URLencode(string, reserved = reserved))

  mpp <- paste0("max=", matchesPerPage)
  format <- paste0("result=", match.arg(format))
  restrictVALS <- c("functions", "vignettes", "views")
  restr <- match.arg(restrict, choices = restrictVALS, several.ok = TRUE)
  restr <- paste(paste0("idxname=", restr), collapse = "&")
  sortby <- match.arg(sortby)
  sortby <- paste0("sort=", switch(sortby, score = , `date:late` = , 
                                   `date:early` = sortby, subject = "field:subject:ascending", 
                                   `subject:descending` = "field:subject:descending", from = "field:from:ascending", 
                                   `from:descending` = "field:from:descending", size = "field:size:ascending", 
                                   `size:descending` = "field:size:descending"))
  qstring <- paste(string, mpp, format, sortby, restr, sep = "&")
  browseURL(qstring)
  cat(gettextf("A search query has been submitted to %s", 
               "http://search.r-project.org"), "\n", sep = "")
  cat(gettext("The results page should open in your browser shortly\n"))
  invisible(qstring)
}

这似乎很奇怪。如果我们在这里做错了,请告诉我们,我会回答下来。如果这突出了一个错误,我们需要对其进行处理。