Skip to content

Commit 5e49e0e

Browse files
authored
Add R suffix for search engines to ensure R relevant results (close #10) (#11)
* Adds `r programming` as suffix for all major search engines to ensure relevant _R_ search results. * Add ixquick to README and roll a new point release * Improve test coverage and remove default switch case due to match.arg call
1 parent 8bcc19c commit 5e49e0e

File tree

9 files changed

+125
-35
lines changed

9 files changed

+125
-35
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Package: searcher
22
Title: Query Search Interfaces
3-
Version: 0.0.2.1000
3+
Version: 0.0.2.2000
44
Authors@R: c(person("James", "Balamuta",
55
email = "[email protected]", role = c("aut", "cre")))
66
Description: Provides a search interface to look up terms
7-
on 'Google', 'Bing', 'DuckDuckGo', 'StackOverflow', 'GitHub', and 'BitBucket'.
7+
on 'Google', 'Bing', 'ixquick', 'DuckDuckGo', 'StackOverflow', 'GitHub', and 'BitBucket'.
88
Upon searching, a browser window will open with the aforementioned search
99
results.
1010
URL: https://github.com/coatless/searcher

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
## Features
44

5-
- Added search portal `search_ixquick()`: Searches with [ixquick](https://www.ixquick.com/). (#6)
5+
- Added search portal `search_ixquick()`: Searches with [ixquick](https://www.ixquick.com/). (#8, #6)
66

77
## Changes
88

9-
- Add `ixquick` as a valid engine to `search_site()`. (#6)
9+
- Append `r programming` to major search engines by default (#11, #10)
10+
- Add `ixquick` as a valid engine to `search_site()`. (#8, #6)
1011
- Included link to repository and bug tracker to `DESCRIPTION`.
1112

1213
# searcher 0.0.2

R/search-functions.R

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ search_site = function(query,
7171

7272
switch(
7373
site,
74-
google = search_google(query),
74+
google = search_google(query, rlang),
7575
stackoverflow = ,
7676
# empty case carried below
7777
so = search_stackoverflow(query, rlang),
@@ -81,12 +81,11 @@ search_site = function(query,
8181
bitbucket = ,
8282
# empty case carried below
8383
bb = search_bitbucket(query, rlang),
84-
bing = search_bing(query),
84+
bing = search_bing(query, rlang),
8585
duckduckgo = ,
8686
# empty case carried below
87-
ddg = search_duckduckgo(query),
88-
ixquick = search_ixquick(query),
89-
search_google(query)
87+
ddg = search_duckduckgo(query, rlang),
88+
ixquick = search_ixquick(query, rlang)
9089
)
9190
}
9291

@@ -131,7 +130,7 @@ searcher = function(site = c(
131130
"ixquick"
132131
),
133132
rlang = TRUE) {
134-
function(query = geterrmessage()) {
133+
function(query = geterrmessage(), rlang = rlang) {
135134
search_site(query, site, rlang = rlang)
136135
}
137136
}
@@ -147,12 +146,14 @@ rlang = TRUE) {
147146
#'
148147
#' See \url{https://moz.com/blog/the-ultimate-guide-to-the-google-search-parameters}
149148
#' for details.
150-
search_google = function(query = geterrmessage()) {
149+
search_google = function(query = geterrmessage(), rlang = TRUE) {
151150
if (!valid_query(query)) {
152151
message("Please provide only 1 `query` term that is not empty.")
153152
return(invisible(""))
154153
}
155154

155+
query = append_r_suffix(query, rlang = rlang)
156+
156157
browse_url("https://google.com/search?q=", query)
157158
}
158159

@@ -161,12 +162,14 @@ search_google = function(query = geterrmessage()) {
161162
#' @section Bing Search:
162163
#' The `search_bing()` function searches [Bing](https://bing.com) using:
163164
#' `https://bing.com/search?q=<query>`
164-
search_bing = function(query = geterrmessage()) {
165+
search_bing = function(query = geterrmessage(), rlang = TRUE) {
165166
if (!valid_query(query)) {
166167
message("Please provide only 1 `query` term that is not empty.")
167168
return(invisible(""))
168169
}
169170

171+
query = append_r_suffix(query, rlang = rlang)
172+
170173
browse_url("https://bing.com/search?q=", query)
171174
}
172175

@@ -175,12 +178,14 @@ search_bing = function(query = geterrmessage()) {
175178
#' @section DuckDuckGo Search:
176179
#' The `search_duckduckgo()` and `search_ddg()` functions both search
177180
#' [DuckDuckGo](https://duckduckgo.com) using: `https://duckduckgo.com/?q=<query>`
178-
search_duckduckgo = function(query = geterrmessage()) {
181+
search_duckduckgo = function(query = geterrmessage(), rlang = TRUE) {
179182
if (!valid_query(query)) {
180183
message("Please provide only 1 `query` term that is not empty.")
181184
return(invisible(""))
182185
}
183186

187+
query = append_r_suffix(query, rlang = rlang)
188+
184189
browse_url("https://duckduckgo.com/?q=", query)
185190
}
186191

@@ -199,12 +204,14 @@ search_ddg = search_duckduckgo
199204
#' For additional details regarding [ixquick](https://ixquick.com)'s
200205
#' search interface please see:
201206
#' \url{https://support.ixquick.com/index.php?/Knowledgebase/Article/View/201/0/how-do-i-make-startpage-by-ixquick-my-default-search-engine-in-chrome}
202-
search_ixquick = function(query = geterrmessage()) {
207+
search_ixquick = function(query = geterrmessage(), rlang = TRUE) {
203208
if (!valid_query(query)) {
204209
message("Please provide only 1 `query` term that is not empty.")
205210
return(invisible(""))
206211
}
207212

213+
query = append_r_suffix(query, rlang = rlang)
214+
208215
browse_url("https://ixquick.com/do/dsearch?query=", query)
209216
}
210217

@@ -229,10 +236,8 @@ search_stackoverflow = function(query = geterrmessage(), rlang = TRUE) {
229236
return(invisible(""))
230237
}
231238

232-
query = if (rlang)
233-
paste(query, "[r]")
234-
else
235-
query
239+
query = append_r_suffix(query, rlang = rlang, "[r]")
240+
236241
browse_url("https://stackoverflow.com/search?q=", query)
237242
}
238243

@@ -257,10 +262,7 @@ search_github = function(query = geterrmessage(), rlang = TRUE) {
257262
return(invisible(""))
258263
}
259264

260-
query = if (rlang)
261-
paste(query, "language:r type:issue")
262-
else
263-
query
265+
query = append_r_suffix(query, rlang = rlang, "language:r type:issue")
264266

265267
browse_url("https://github.com/search?q=", query, "&type=Issues")
266268
}
@@ -286,10 +288,8 @@ search_bitbucket = function(query = geterrmessage(), rlang = TRUE) {
286288
return(invisible(""))
287289
}
288290

289-
query = if (rlang)
290-
paste(query, "lang:r")
291-
else
292-
query
291+
query = append_r_suffix(query, rlang = rlang, "lang:r")
292+
293293
browse_url("https://bitbucket.com/search?q=", query)
294294
}
295295

R/utilities.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,30 @@ valid_query = function(query) {
7070
TRUE
7171
}
7272
}
73+
74+
75+
#' Append R Search Term Suffix
76+
#'
77+
#' Customizes the query string with an appropriate _R_ specific suffix.
78+
#'
79+
#' @param query Search terms to verify
80+
#' @param rlang Append value to query.
81+
#' @param suffix Value to be added to search query. Default `r programming`.
82+
#'
83+
#' @return Query string returned as-is or modified with a suffix that
84+
#' ensure results are _R_ oriented.
85+
#'
86+
#' @examples
87+
#'
88+
#' # Add suffix
89+
#' append_r_suffix("testing", rlang = TRUE, suffix = "toad")
90+
#'
91+
#' # Retain original search query
92+
#' append_r_suffix("testing", rlang = FALSE, suffix = "toad")
93+
#' @noRd
94+
append_r_suffix = function(query, rlang = TRUE, suffix = "r programming") {
95+
if (rlang)
96+
paste(query, suffix)
97+
else
98+
query
99+
}

README.Rmd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ library(searcher)
5353

5454
The `search_*()` functions can be used to search a query directly from _R_ on
5555
major search engines, code repositories, and help websites. The following search
56-
platforms are supported: Google, Bing, DuckDuckGo, StackOverflow, GitHub, and BitBucket.
56+
platforms are supported: Google, Bing, ixquick, DuckDuckGo, StackOverflow, GitHub,
57+
and BitBucket. By default, an appropriate suffix for each platform that ensures
58+
relevant results to _R_ is appended to all queries. This behavior can be
59+
disabled by using `rlang = FALSE`.
5760

5861
```r
5962
# Searching R project on major search engines
6063
search_google("R project")
6164
search_bing("R project")
65+
search_ixquick("R project")
6266
search_duckduckgo("R project") # or search_ddg(...)
6367

6468
# Searching for linear regression questions for R and in general

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ library(searcher)
4747

4848
The `search_*()` functions can be used to search a query directly from
4949
*R* on major search engines, code repositories, and help websites. The
50-
following search platforms are supported: Google, Bing, DuckDuckGo,
51-
StackOverflow, GitHub, and BitBucket.
50+
following search platforms are supported: Google, Bing, ixquick,
51+
DuckDuckGo, StackOverflow, GitHub, and BitBucket. By default, an
52+
appropriate suffix for each platform that ensures relevant results to
53+
*R* is appended to all queries. This behavior can be disabled by using
54+
`rlang = FALSE`.
5255

5356
``` r
5457
# Searching R project on major search engines
5558
search_google("R project")
5659
search_bing("R project")
60+
search_ixquick("R project")
5761
search_duckduckgo("R project") # or search_ddg(...)
5862

5963
# Searching for linear regression questions for R and in general

man/search_site.Rd

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

man/searcher-package.Rd

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

tests/testthat/test-searcher.R

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,47 @@ test_that("Check link generation", {
66

77
expect_identical(
88
search_google("toad"),
9+
"https://google.com/search?q=toad%20r%20programming"
10+
)
11+
12+
expect_identical(
13+
search_google("toad", rlang = FALSE),
914
"https://google.com/search?q=toad"
1015
)
1116

1217
##### Bing
1318

1419
expect_identical(
1520
search_bing("toad"),
21+
"https://bing.com/search?q=toad%20r%20programming"
22+
)
23+
24+
expect_identical(
25+
search_bing("toad", rlang = FALSE),
1626
"https://bing.com/search?q=toad"
1727
)
1828

1929
##### DDG
2030

2131
expect_identical(
2232
search_duckduckgo("toad"),
33+
"https://duckduckgo.com/?q=toad%20r%20programming"
34+
)
35+
36+
expect_identical(
37+
search_duckduckgo("toad", rlang = FALSE),
2338
"https://duckduckgo.com/?q=toad"
2439
)
2540

2641
##### ixquick
2742

2843
expect_identical(
2944
search_ixquick("toad"),
45+
"https://ixquick.com/do/dsearch?query=toad%20r%20programming"
46+
)
47+
48+
expect_identical(
49+
search_ixquick("toad", rlang = FALSE),
3050
"https://ixquick.com/do/dsearch?query=toad"
3151
)
3252

@@ -75,6 +95,25 @@ test_that("Validate selection", {
7595
"https://bitbucket.com/search?q=toad"
7696
)
7797

98+
expect_error(
99+
search_site("toad", "", rlang = FALSE)
100+
)
101+
102+
expect_identical(
103+
search_site("toad", "ixquick", rlang = FALSE),
104+
"https://ixquick.com/do/dsearch?query=toad"
105+
)
106+
107+
expect_identical(
108+
search_site("toad", "bing", rlang = FALSE),
109+
"https://bing.com/search?q=toad"
110+
)
111+
112+
expect_identical(
113+
search_site("toad", "ddg", rlang = FALSE),
114+
"https://duckduckgo.com/?q=toad"
115+
)
116+
78117
expect_identical(
79118
search_site("", rlang = FALSE),
80119
"",
@@ -84,6 +123,21 @@ test_that("Validate selection", {
84123
})
85124

86125

126+
127+
128+
test_that("Verify search handler generation", {
129+
expect_identical(
130+
searcher("bing", rlang = TRUE)(""),
131+
search_bing("")
132+
)
133+
134+
expect_identical(
135+
searcher("bing", rlang = FALSE)(""),
136+
search_bing("", rlang = FALSE)
137+
)
138+
})
139+
140+
87141
test_that("Malformed search query validation", {
88142

89143
expect_identical(

0 commit comments

Comments
 (0)