|
5 | 5 | #' @param base The URL prefix e.g. `https://google.com/search?q=` |
6 | 6 | #' @param unencoded_query An unencoded string that must be encoded with [utils::URLencode()]. |
7 | 7 | #' @param encoded_query An encoded string that satisifies [utils::URLencode()]. |
8 | | -#' @param open_browser Should the URL be opened in a web browser. |
9 | | -#' @inheritParams utils::browseURL |
| 8 | +#' @param open_browser Should the URL be opened in a web browser. |
10 | 9 | #' |
11 | | -#' @return A `character` object containing the query URL |
| 10 | +#' @return |
| 11 | +#' A `character` object containing the query URL |
| 12 | +#' |
| 13 | +#' @seealso |
| 14 | +#' [utils::browseURL()] |
12 | 15 | #' |
13 | | -#' @seealso [utils::browseURL()], [utils::URLencode()] |
14 | 16 | #' @examples |
15 | 17 | #' # Query Google |
16 | 18 | #' browse_url("https://google.com/search?q=", "rstats is great") |
|
25 | 27 | #' @noRd |
26 | 28 | browse_url = function(base, |
27 | 29 | unencoded_query, encoded_query = "", |
28 | | - browser = getOption("browser"), |
29 | 30 | open_browser = interactive()) { |
30 | 31 |
|
31 | | - encodedURL = paste0(base, utils::URLencode(unencoded_query), encoded_query) |
32 | | - |
33 | | - |
| 32 | + url = encode_url(base, unencoded_query, encoded_query) |
34 | 33 | if (open_browser) { |
35 | | - message("Searching query in web browser ... ") |
36 | | - |
37 | | - Sys.sleep(getOption("searcher.launch_delay")) |
38 | | - utils::browseURL(encodedURL) |
39 | | - |
| 34 | + if (is_rstudio() && getOption("searcher.use_rstudio_viewer")) { |
| 35 | + open_rstudio_viewer(url) |
| 36 | + } else { |
| 37 | + open_browser(url) |
| 38 | + } |
40 | 39 | } else { |
41 | | - message("Please type into your browser: \n", invisible(encodedURL)) |
| 40 | + message("Please type into your browser:\n", invisible(url)) |
42 | 41 | } |
43 | 42 |
|
44 | | - invisible(encodedURL) |
| 43 | + invisible(url) |
45 | 44 | } |
46 | 45 |
|
| 46 | +open_rstudio_viewer = function(url) { |
| 47 | + message("Searching query in RStudio's Viewer panel ... ") |
| 48 | + Sys.sleep(getOption("searcher.launch_delay")) |
| 49 | + |
| 50 | + # If in RStudio, this should be set. |
| 51 | + viewer <- getOption("viewer") |
| 52 | + viewer(url) |
| 53 | +} |
47 | 54 |
|
| 55 | +open_browser = function(url) { |
| 56 | + message("Searching query in a web browser ... ") |
| 57 | + Sys.sleep(getOption("searcher.launch_delay")) |
| 58 | + utils::browseURL(url) |
| 59 | +} |
| 60 | + |
| 61 | +#' Form Encoded URL |
| 62 | +#' |
| 63 | +#' Creates a URL with appropriate encoding |
| 64 | +#' |
| 65 | +#' @param base The URL prefix e.g. `https://google.com/search?q=` |
| 66 | +#' @param unencoded_query An unencoded string that must be encoded with [utils::URLencode()]. |
| 67 | +#' @param encoded_query An encoded string that satisifies [utils::URLencode()]. |
| 68 | +#' |
| 69 | +#' @return |
| 70 | +#' A properly formatted URL. |
| 71 | +#' |
| 72 | +#' @seealso |
| 73 | +#' [utils::URLencode()] |
| 74 | +#' @noRd |
| 75 | +encode_url = function(base, unencoded_query, encoded_query = "") { |
| 76 | + paste0(base, utils::URLencode(unencoded_query), encoded_query) |
| 77 | +} |
48 | 78 |
|
49 | 79 | #' Validate search query |
50 | 80 | #' |
@@ -100,3 +130,20 @@ append_r_suffix = function(query, rlang = TRUE, suffix = "r programming") { |
100 | 130 | else |
101 | 131 | query |
102 | 132 | } |
| 133 | + |
| 134 | +#' Check if in RStudio |
| 135 | +#' |
| 136 | +#' Verifies whether the user is using the RStudio IDE. |
| 137 | +#' |
| 138 | +#' @return |
| 139 | +#' A `logical` value indicating whether _R_ is being accessed from the RStudio |
| 140 | +#' IDE. |
| 141 | +#' |
| 142 | +#' @examples |
| 143 | +#' # Check if in RStudio |
| 144 | +#' is_rstudio() |
| 145 | +#' |
| 146 | +#' @noRd |
| 147 | +is_rstudio = function() { |
| 148 | + Sys.getenv("RSTUDIO") == "1" |
| 149 | +} |
0 commit comments