@@ -156,10 +156,11 @@ by pkgmatch.
156
156
157
157
pkgmatch can also be used when writing R functions or scripts, to answer the
158
158
question of which R packages may already exist which do what I am trying to
159
- code. The easiest way to illustrate this is with a concrete example:
159
+ code. The easiest way to illustrate this is with a concrete example, like the
160
+ following attempt to use [ the ` httr2 ` package] ( https://httr2.r-lib.org/ ) to
161
+ extract data from a search API:
160
162
161
163
``` {r search-code}
162
- library (httr2)
163
164
search_api_results <- function (q) {
164
165
url <- "https://mysearchapi.com/search"
165
166
q <- httr2::httr_request (url) |>
@@ -192,7 +193,8 @@ whether any of those packages might help develop our function. We can also
192
193
search for the closest matching _ functions_ instead of packages, although
193
194
function matching is restricted to the rOpenSci corpus only.
194
195
``` {r fn-fn-search, eval = FALSE}
195
- pkgmatch_similar_fns (s)
196
+ fns <- pkgmatch_similar_fns (s)
197
+ fns
196
198
```
197
199
``` {r fn-fn-search-out, echo = FALSE}
198
200
c (
201
203
"webmockr::build_httr_request"
202
204
)
203
205
```
206
+
207
+ Again, web pages for all of those functions can be opened by passing the ` fns `
208
+ result to [ the ` pkgmatch_browse() `
209
+ function] ( https://docs.ropensci.org/pkgmatch/reference/pkgmatch_browse.html ) .
210
+ That will open the documentation pages on
211
+ [ docs.ropensci.org] ( https://docs.ropensci.org ) , with all function documentation
212
+ entries include a link to the corresponding source code at the top of the page.
213
+ This demonstrates how pkgmatch can be used to identify similar source code to
214
+ any given code input.
215
+
216
+ ## Searches based on entire packages
217
+
218
+ Entire packages can also be used as input to pkgmatch functions. The simplest
219
+ way to do this is to submit the name of an installed package, like this:
220
+
221
+ ``` {r whole-pkg-search, eval = FALSE}
222
+ pkgs <- pkgmatch_similar_pkgs ("osmdata", corpus = "cran")
223
+ ```
224
+
225
+ As explained in [ the _ How does pkgmatch work?_
226
+ vignette] ( https://docs.ropensci.org/pkgmatch/articles/B_how-does-it-work.html ) ,
227
+ pkgmatch extracts all code and text from the nominated packages and uses these
228
+ to generate three sets of embeddings: of all package code, and of all text both
229
+ with and without text from individual function descriptions. Matches with other
230
+ packages are based on combinations of matches with these three sets of
231
+ embeddings, as well as matches based on inverse document frequencies for
232
+ package text (as also explained in [ the _ How does pkgmatch work?_
233
+ vignette] ( https://docs.ropensci.org/pkgmatch/articles/B_how-does-it-work.html#token-frequency-similarities ) ).
234
+ The above call yields this result:
235
+
236
+ ``` {r whole-pkg-search-output-fakey, eval = FALSE}
237
+ pkgs
238
+ ```
239
+ ``` {r whole-pkg-search-output, echo = FALSE}
240
+ list (
241
+ text = c ("factormodel", "crul", "healthequal", "httr2", "crumble"),
242
+ code = c ("shinyKGode", "crul", "sphereML", "irtawsi", "httr2")
243
+ )
244
+ ```
245
+
246
+ As expected, the ` crul ` package appears in the top results for matching both
247
+ text and code (although not in first place, for reasons described in [ the
248
+ _ Why are the results not what I expect?_
249
+ vignette] ( https://docs.ropensci.org/pkgmatch/articles/E_why-are-the-results-not-what-i-expect.html ) ).
250
+ Moreover, the only two packages matched on both code and text are ` crul `
251
+ itself, and the highly-similar [ ` httr2 ` package] ( https://httr2.r-lib.org/ ) .
0 commit comments