knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/", out.width = "100%" ) library(tidyverse)
CopyCat is a small package to copy, paste, and view code snippets. All provided code snippets are minimal examples based on code that runs with implemented data, which helps R learner to see how the code works. CopyCat uses three different code sources. First, CopyCat comes with a small data frame that includes code snippets from the cheat sheets of the ggplot2
and the tidyr
package. Of course, you can use your own code snippets as a data source as well. Second, CopyCat can fetch and copy code that lives on Github. Ultimately, CopyCat searches also within the R help files and vignettes for code that illustrates how a function works. Thus, CopyCat was built as a personal package for the lazy cats, but it will also help new R learner to manage code snippets.
Install CopyCat from my github account with:
# install.packages("devtools") devtools::install_github("edgar-treischl/CopyCat")
I do not know how many times I have searched within my old scripts, especially for a ggplot
command that I have been used many times, but I have forgotten the details. For this reason I started to create this package. CopyCat comes with a small data set (CopyCatCode
) that contains minimal examples of several cheat sheets that run without further ado. For example, let's have a look at the minimal examples of the ggplot2 package. The CopyCatCode
data provides the package name, the function, and the code of the minimal example:
## load library and provide a data frame library(copycat) CopyCatCode %>% filter(package == "ggplot2") %>% arrange(fct)
Let's say you cannot remember how pivot_longer
from the tidyr
package works. Just search for the corresponding code snippet via the copycat()
function, it searches for the code snippet and saves the returned code to your clipboard. To see which code is returned, use the corresponding copycat_code()
function.
# copycat("pivot_longer") saves the returned code to the clipboad #>[1] "🐈 Your code: relig_income %>% tidyr::pivot_longer(!religion, names_to = #>'income', values_to = 'count')" # copycat_code() let us inspect what the function returned copycat_code("pivot_longer")
Since the code is based on implemented data -- as all examples listed in CopyCat -- you can see how it works just by pasting it into your console. Alternatively, set the run
option to TRUE
and copycat()
sends the code to your console.
copycat("pivot_longer", run = T )
relig_income %>% pivot_longer(!religion, names_to = 'income', values_to = 'count')
Of course, the minimal examples will only run if the package has been loaded and most of the time we know the package name. However, sometimes we have to look up the package name if we do not use a function often. The copycat_package()
function returns the corresponding package name and sends the code library(...)
directly to your console. The copycat_package()
function copies the code also to the clipboard, since you want to insert it into your script as well.
#search for a package name, copies the load and loads the library copycat_package("pivot_longer") #>[1] [1] "🐈 Mission accomplished, loaded and copied library: tidyr"
If you add typos by accident, if you are not sure whether the function is written in small or large caps, you might be lucky and a similar match is found in the data.
#typos and other mistakes copycat_package("bivot")
Unfortunately, this only works if a match is found at all.
copycat("bivatasa") #>[1] "💩 Sooorry, I've got no idea what you are looking for!"
CopyCat can also be connected to your Github repository to copy one of your old scripts. First, you have to setup the Github account details, that CopyCat will search. Provide the name of the author, the repository, and the branch name.
git_setup <- c(author = "edgar-treischl", repository = "Illustrations", branch = "master")
The copycat_gitsearch()
function uses the Github API to search within your repository and returns all R scripts that live within the repository
copycat_gitsearch()
The copycat_git()
function copies the code to your clipboard.
copycat_git("boxplot_illustration") #>[1] "🐈 Mission accomplished!"
Often, all we need is code that runs to see how a functions work. For this reason, CopyCat to search within the R help files and vignettes, but returns only only the code listed in the vignette or the examples of the help file. The copycat_helpsearch()
function list all help files of a package:
copycat_helpsearch("tidyr")
And the copycat_help()
function copies the examples section of a help file into your clipboard. Again, to see what we have actually copied, the corresponding copycat_helpcode
function return only the code.
#copycat_help() saves examples of the help files into your clipboard #copycat_help("tidyr", "drop_na") copycat_helpcode("tidyr", "drop_na")
The same function exist to search for and copy code from vignettes. The copycat_vigsearch()
returns all availabe vignettes.
copycat_vigsearch("tidyr")
And copycat_vignette()
copies the entire script.
copycat_vignette("tidyr", "pivot") #> "😎 copied that!"
To copy smaller code chunks to your clipboard is fine. Helpfiles and especially vignettes are much longer. Therefore, we have to create a new file and paste the code into your file. The copycat_helpscript()
create a new script with the examples of the help file, while copycat_vigscript()
does the same with the code from the vignette.
copycat_helpscript("tidyr", "pivot") copycat_vigscript("tidyr", "pivot")
CopyCat has started as a personal package. Feel free to use it or manage your own code snippets with it. Just add your data frame with the same variable names of the small example data (CopyCatCode
) and CopyCat handles your own snippets.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.