Skip to content

Commit 7ff1ddb

Browse files
authored
Enable search queries to show up in RStudio's Viewer Panel (close #21) (#22)
* Enable opening the URL inside of the RStudio window. * Update README file with new option * Add a NOTE * Switch default to `FALSE` as RStudio is not appropriately sandboxing the environment. rstudio/rstudio#2252
1 parent d248adf commit 7ff1ddb

File tree

6 files changed

+89
-21
lines changed

6 files changed

+89
-21
lines changed

NEWS.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
## Features
44

55
- Added ability to set default package actions.
6+
- Allow RStudio's Viewer pane to display search results.
7+
- Note: This feature requires a patch per [issue 2252](https://github.com/rstudio/rstudio/issues/2252).
8+
9+
## Changes
10+
11+
- Default options added.
612
- `searcher.launch_delay` controls how long the user remains in _R_ prior
7-
to the browser opening.
13+
to the browser opening. Default is `0.5` seconds.
14+
- `searcher.use_rstudio_viewer` specifies whether RStudio's viewer pane should
15+
open the link instead of a web browser. Default is `FALSE` until the issue
16+
is resolved..
17+
818

919
# searcher 0.0.4
1020

R/searcher-package.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
#' call to keep the function signatures small. By default, these options are given as:
66
#'
77
#' - `searcher.launch_delay`: Amount of time to remain in _R_ before opening
8-
#' a browser window. Default is 0.5 seconds.
8+
#' a browser window. Default is `0.5` seconds.
9+
#' - `searcher.use_rstudio_viewer`: Display search results in the RStudio
10+
#' viewer pane instead of a web browser. Default is `FALSE`.
911
#' - ...
1012
#'
1113
"_PACKAGE"
1214

1315
searcher_default_options = list(
14-
searcher.launch_delay = 0.5
16+
searcher.launch_delay = 0.5,
17+
searcher.use_rstudio_viewer = FALSE
1518
)
1619

1720
.onLoad = function(libname, pkgname) {

R/utilities.R

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
#' @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()].
77
#' @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.
109
#'
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()]
1215
#'
13-
#' @seealso [utils::browseURL()], [utils::URLencode()]
1416
#' @examples
1517
#' # Query Google
1618
#' browse_url("https://google.com/search?q=", "rstats is great")
@@ -25,26 +27,54 @@
2527
#' @noRd
2628
browse_url = function(base,
2729
unencoded_query, encoded_query = "",
28-
browser = getOption("browser"),
2930
open_browser = interactive()) {
3031

31-
encodedURL = paste0(base, utils::URLencode(unencoded_query), encoded_query)
32-
33-
32+
url = encode_url(base, unencoded_query, encoded_query)
3433
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+
}
4039
} else {
41-
message("Please type into your browser: \n", invisible(encodedURL))
40+
message("Please type into your browser:\n", invisible(url))
4241
}
4342

44-
invisible(encodedURL)
43+
invisible(url)
4544
}
4645

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+
}
4754

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+
}
4878

4979
#' Validate search query
5080
#'
@@ -100,3 +130,20 @@ append_r_suffix = function(query, rlang = TRUE, suffix = "r programming") {
100130
else
101131
query
102132
}
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+
}

README.Rmd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ Presently, the following options are available:
146146

147147
- `searcher.launch_delay`: Amount of time between launching the web browser
148148
from when the command was issued. Default is `0.5` seconds.
149+
- `searcher.use_rstudio_viewer`: Display search results in the RStudio
150+
viewer pane instead of a web browser. Default is `FALSE`.
149151

150152
To set one of these options, please create the `.Rprofile` by typing into _R_:
151153

@@ -158,7 +160,8 @@ From there, add:
158160
```r
159161
.First = function() {
160162
options(
161-
searcher.launch_delay = 0
163+
searcher.launch_delay = 0,
164+
searcher.use_rstudio_viewer = FALSE
162165
## Additional options.
163166
)
164167
}

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ Presently, the following options are available:
146146

147147
- `searcher.launch_delay`: Amount of time between launching the web
148148
browser from when the command was issued. Default is `0.5` seconds.
149+
- `searcher.use_rstudio_viewer`: Display search results in the RStudio
150+
viewer pane instead of a web browser. Default is `FALSE`.
149151

150152
To set one of these options, please create the `.Rprofile` by typing
151153
into *R*:
@@ -159,7 +161,8 @@ From there, add:
159161
``` r
160162
.First = function() {
161163
options(
162-
searcher.launch_delay = 0
164+
searcher.launch_delay = 0,
165+
searcher.use_rstudio_viewer = FALSE
163166
## Additional options.
164167
)
165168
}

man/searcher-package.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)