|
| 1 | +#' Open a Web Browser with URL |
| 2 | +#' |
| 3 | +#' Open a web browser with a given URL. |
| 4 | +#' |
| 5 | +#' @param base The URL prefix e.g. `https://google.com/search?q=` |
| 6 | +#' @param unencoded_query An unencoded string that must be encoded with [utils::URLencode]. |
| 7 | +#' @param encoded_query An encoded string that satisifies [utils::URLencode]. |
| 8 | +#' @importFrom utils browseURL URLencode |
| 9 | +#' @seealso [utils::browseURL], [utils::URLencode] |
| 10 | +open_browser = function(base, unencoded_query, encoded_query = "") { |
| 11 | + |
| 12 | + encodedURL = paste0(base, URLencode(unencoded_query), encoded_query) |
| 13 | + |
| 14 | + browseURL(encodedURL) |
| 15 | +} |
| 16 | + |
| 17 | +#' Search a Message Using a Search Engine |
| 18 | +#' |
| 19 | +#' Searches message using a search engine. |
| 20 | +#' @param site Name of site to search on. Supported options: |
| 21 | +#' `"google"` (default), `"stackoverflow"`, `"github"`, `"bing"`, |
| 22 | +#' `"bitbucket"` |
| 23 | +#' @param query Contents of string to search. Default is the error message. |
| 24 | +#' @param rlang Search for results written in R. Default is `TRUE` |
| 25 | +#' @rdname search_error |
| 26 | +#' @export |
| 27 | +#' @seealso [search_google], [search_stackoverflow], [search_github], |
| 28 | +#' [search_bing], [search_bitbucket], [search_error], [open_browser] |
| 29 | +#' @examples |
| 30 | +#' \dontrun{ |
| 31 | +#' # On error, automatically search the message on google |
| 32 | +#' options(error = search_google) |
| 33 | +#' |
| 34 | +#' # Search in a generic way |
| 35 | +#' search_site("google", "r-project") |
| 36 | +#' |
| 37 | +#' # Search Google |
| 38 | +#' search_google("r-project") |
| 39 | +#' |
| 40 | +#' # Search Bing |
| 41 | +#' search_bing("Microsoft R") |
| 42 | +#' |
| 43 | +#' # Search DuckDuckGo |
| 44 | +#' search_duckduckgo("R language") |
| 45 | +#' |
| 46 | +#' # Search StackOverflow for Convolutions in the r tag |
| 47 | +#' search_stackoverflow("convolutions") |
| 48 | +#' |
| 49 | +#' # Search all languages on StackOverflow for convolutions |
| 50 | +#' search_stackoverflow("convolutions", rlang = FALSE) |
| 51 | +#' |
| 52 | +#' # Search GitHub Issues for bivariate normal in the language:r |
| 53 | +#' search_github("bivariate normal") |
| 54 | +#' |
| 55 | +#' # Search all languages on GitHub Issues for bivariate normal |
| 56 | +#' search_github("bivariate normal", rlang = FALSE) |
| 57 | +#' |
| 58 | +#' # Search BitBucket for assertions |
| 59 | +#' search_bitbucket("assertions") |
| 60 | +#' } |
| 61 | +search_site = function(site, query, rlang = TRUE){ |
| 62 | + switch(tolower(site), |
| 63 | + "google" = search_google(query), |
| 64 | + "stackoverflow" = search_stackoverflow(query, rlang), |
| 65 | + "github" = search_github(query, rlang), |
| 66 | + "bitbucket" = search_bitbucket(query, rlang), |
| 67 | + "bing" = search_bing(query), |
| 68 | + "duckduckgo" = search_duckduckgo(query), |
| 69 | + search_google(query) |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +#' @rdname search_error |
| 74 | +#' @export |
| 75 | +#' @section Generic Error Search: |
| 76 | +#' The `search_error` function grabs the last error message and |
| 77 | +#' tries to search it. This function will ensure that R language |
| 78 | +#' is the primary search context. |
| 79 | +search_error = function(site, query = geterrmessage()) { |
| 80 | + search_site(site, query, rlang = TRUE) |
| 81 | +} |
| 82 | + |
| 83 | +#' @rdname search_error |
| 84 | +#' @export |
| 85 | +#' @section Google Search: |
| 86 | +#' The `search_google` function searches [Google](https://google.com) using: |
| 87 | +#' `https://google.com/search?q=<Error>` |
| 88 | +#' See <https://moz.com/blog/the-ultimate-guide-to-the-google-search-parameters> |
| 89 | +#' for details. |
| 90 | +search_google = function(query = geterrmessage()) { |
| 91 | + open_browser("https://google.com/search?q=", query) |
| 92 | +} |
| 93 | + |
| 94 | +#' @rdname search_error |
| 95 | +#' @export |
| 96 | +#' @section Bing Search: |
| 97 | +#' The `search_bing` function searches [Bing](https://bing.com) using: |
| 98 | +#' `https://bing.com/search?q=<Error>` |
| 99 | +search_bing = function(query = geterrmessage()) { |
| 100 | + open_browser("https://bing.com/search?q=", query) |
| 101 | +} |
| 102 | + |
| 103 | +#' @rdname search_error |
| 104 | +#' @export |
| 105 | +#' @section DuckDuckGo Search: |
| 106 | +#' The `search_duckduckgo` function searches [DuckDuckGo](https://duckduckgo.com) using: |
| 107 | +#' `https://duckduckgo.com/?q=<Error>` |
| 108 | +search_duckduckgo = function(query = geterrmessage()) { |
| 109 | + open_browser("https://duckduckgo.com/?q=", query) |
| 110 | +} |
| 111 | + |
| 112 | +#' @rdname search_error |
| 113 | +#' @export |
| 114 | +#' @section StackOverflow Search: |
| 115 | +#' The `search_stackoverflow` function searches [StackOverflow](https://stackoverflow.com) using: |
| 116 | +#' `https://stackoverflow.com/search?q=%5Br%5D+<Error>` |
| 117 | +#' See <https://stackoverflow.com/help/advanced-search-parameters-jobs> |
| 118 | +search_stackoverflow = function(query = geterrmessage(), rlang = TRUE) { |
| 119 | + query = if(rlang) paste(query, "[r]") else query |
| 120 | + open_browser("https://stackoverflow.com/search?q=", query) |
| 121 | +} |
| 122 | + |
| 123 | +#' @rdname search_error |
| 124 | +#' @export |
| 125 | +#' @section GitHub Search: |
| 126 | +#' The `search_github` function searches [GitHub](https://github.com) using: |
| 127 | +#' `https://github.com/search?q=<Error>+language%3Ar+type%3Aissue&type=Issues` |
| 128 | +#' See <https://help.github.com/categories/searching-for-information-on-github/> |
| 129 | +#' and <https://help.github.com/articles/searching-code/> |
| 130 | +search_github = function(query = geterrmessage(), rlang = TRUE) { |
| 131 | + query = if(rlang) paste(query, "language:r type:issue") else query |
| 132 | + |
| 133 | + open_browser("https://github.com/search?q=", query, "&type=Issues") |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +#' @rdname search_error |
| 138 | +#' @export |
| 139 | +#' @section BitBucket Search: |
| 140 | +#' The `search_bitbucket` function searches [BitBucket](https://bitbucket.com) using: |
| 141 | +#' `https://bitbucket.com/search?q=lang%3Ar+<Error>` |
| 142 | +#' See <https://confluence.atlassian.com/bitbucket/code-search-in-bitbucket-873876782.html> |
| 143 | +search_bitbucket = function(query = geterrmessage(), rlang = TRUE) { |
| 144 | + query = if(rlang) paste(query, "lang:r") else query |
| 145 | + open_browser("https://bitbucket.com/search?q=", query) |
| 146 | +} |
0 commit comments