knitr::opts_chunk$set(echo = TRUE) knitr::opts_knit$set(root.dir = system.file("extdata/testing", package = "scriptsearch")) library(scriptsearch)
Scriptsearch is an R package intended to help people search through their previous work. Poorly documented code can be hard to look back on, preventing you from reusing the solutions you've already written. This package contains two functions to help you search through your scripts. The source code can be found on github at https://github.com/carlos-r-git/scriptsearch.
This set of exercises is intended to show off the features of the package and how they can be used. The exercises can be completed by typing commands into the console or by making a new R script and running the commands line by line. At the end of this document is a link to a google form where you are welcome to give any feedback relating to the package.
The scriptsearch package contains a folder of example scripts that will be used in these exercises. These files can be found in the path produced by system.file("extdata/testing", package = "scriptsearch")
fpath <- system.file("extdata/testing", package = "scriptsearch") list.files(fpath)
The function scriptsearch()
defaults to searching the present working directory, so you will need to set the argument dir
to match the path containing the example files (alternatively you can set your working directory to the folder containing the example files). Check that you can find the files with list.files()
Click to reveal answer
fpath <- system.file("extdata/testing", package = "scriptsearch")
list.files(fpath)
Let's search for some text in the example R scripts. scriptsearch()
takes 4 arguments:
1) searchterms
- Text you will search for in the target files
2) dir
- Path to directory containing target files
3) rm
- Logical for removing files with no hits from the output
4) filetypes
- File endings used to filter target files
5) escape
- Logical for adding escape characters to special characters in searchterms
Arguments 2,3,4,5 have default values, however searchterms
must always be supplied manually. Make sure to use library(scriptsearch)
so that the scriptsearch functions are available.
scriptsearch("text", dir = fpath)
will take "text" as the value for searchterms
and search in fpath
for .R and .Rmd files that contain the word text
.
Use scriptsearch()
to search for the function "t.test" and see which files show up. Which argument would you change so that files which don't contain "t.test" also appear in the output?
Click to reveal answer
fpath <- "."
scriptsearch("t.test", dir = fpath, rm = FALSE)
Because "t.test" contains the special character ".", an escape character has been added - you can see this in the searchterms column. How are the results changed if you set the escape
argument to FALSE?
Click to reveal answer
# There are more hits now. "." stands for "any character" so "t-test" and "t test" are also recognised.
scriptsearch("t.test", dir = fpath, rm = FALSE, escape = FALSE)
The default value for filetypes
selects for .R and .Rmd files. What would you enter for filetypes
so that only the "exampleprac.Rmd" files would be searched?
Click to reveal answer
scriptsearch("t.test", dir = fpath, filetypes = "Rmd")
We can now use the showlines()
function to find where the "t.test" hits were. showlines()
takes the output of scriptsearch()
as its input. There are several ways to achieve this:
1) Assign the output of scriptsearch()
to an object using <-
and use showlines()
on that object
data <- scriptsearch("example") showlines(data, 1)
2) Pipe the output of scriptsearch()
into showlines()
using %>%
(requires library(magrittr)
or library(tidyverse)
)
library(tidyverse) scriptsearch("example") %>% showlines(1)
3) Nest the scriptsearch()
function inside showlines()
showlines(scriptsearch("example"), 1)
Alternative methods:
Use showlines()
to find where "t.test" appears in the file exampleprac5.Rmd
(It may help to use the scriptsearch function first to find the index of the file you want to check)
Click to reveal answer
data <- scriptsearch("t.test", dir = fpath, rm = FALSE)
data
showlines(data, 12)
showlines(data, 12, open = FALSE)
scriptsearch("t.test", dir = fpath, rm = FALSE) %>% showlines(., 12)
showlines(scriptsearch("t.test", dir = fpath, rm = FALSE), 12)
Once the output of scriptsearch()
is in an object it can be read just like any other data frame.
Can you find the total number of times "ggplot" appears in the example scripts?
Which file contains "ggplot" the most?
Click to reveal answer
data <- scriptsearch("ggplot", dir = fpath)
# total "ggplot"
sum(data$Hits)
# file with most "ggplot"
data$Path[which.max(data$Hits)]
What happens if you search for a string which does not appear in any of the files? Try searching for a long nonsense string with and without rm = FALSE
.
Click to reveal answer
scriptsearch("zzzz")
scriptsearch("zzzz", rm = FALSE)
Now try using showlines()
to read a file which didn't contain any hits (you will need to use rm = FALSE
for scriptsearch()
).
Click to reveal answer
no_files <- scriptsearch("zzzz")
showlines(no_files, 1)
no_hits <- scriptsearch("zzzz", rm = FALSE)
showlines(no_hits, 1)
Please find below a short google form (5 questions) where you can send feedback relating to scriptsearch:
Thanks for testing scriptsearch!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.