-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
108 lines (78 loc) · 2.61 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
<!-- badges: start -->
[![Travis build status](https://travis-ci.org/LucyMcGowan/tidycode.svg?branch=master)](https://travis-ci.org/LucyMcGowan/tidycode)
[![Codecov test coverage](https://codecov.io/gh/LucyMcGowan/tidycode/branch/master/graph/badge.svg)](https://codecov.io/gh/LucyMcGowan/tidycode?branch=master)
[![CRAN status](https://www.r-pkg.org/badges/version/tidycode)](https://cran.r-project.org/package=tidycode)
<!-- badges: end -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# tidycode
The goal of tidycode is to allow users to analyze R expressions in a tidy way.
## Installation
You can install tidycode from CRAN with:
```{r cran-installation, eval = FALSE}
install.packages("tidycode")
```
You can install the development version of tidycode from github with:
```{r gh-installation, eval = FALSE}
# install.packages("remotes")
remotes::install_github("LucyMcGowan/tidycode")
```
## Example
### Read in existing code
Using the matahari package, we can read in existing code, either as a string or a file, and turn it into a matahari tibble using `matahari::dance_recital()`.
```{r}
code <- "
library(broom)
library(glue)
m <- lm(mpg ~ am, data = mtcars)
t <- tidy(m)
glue_data(t, 'The point estimate for term {term} is {estimate}.')
"
m <- matahari::dance_recital(code)
```
Alternatively, you may already have a matahari tibble that was recorded during an R session.
Load the tidycode library.
```{r}
library(tidycode)
```
We can use the expressions from this matahari tibble to extract the names of the packages included. We can also create a data frame that will include _all_ functions of the packages included.
```{r}
(pkg_names <- ls_packages(m$expr))
pkg_functions <- get_package_functions(m$expr)
```
Create a data frame of your expressions, splitting each into individual functions.
```{r}
u <- unnest_calls(m, expr)
```
Merge in the package names
```{r}
u <- u %>%
dplyr::left_join(pkg_functions) %>%
dplyr::select(func, args, line, package)
u
```
Add in the function classifications!
```{r}
u %>%
dplyr::inner_join(
get_classifications("crowdsource", include_duplicates = FALSE)
)
```
We can also remove a list of "stopwords". We have a function, `get_stopfuncs()` that lists common "stopwords", frequently used operators, like `%>%` and `+`.
```{r}
u %>%
dplyr::inner_join(
get_classifications("crowdsource", include_duplicates = FALSE)
) %>%
dplyr::anti_join(get_stopfuncs()) %>%
dplyr::select(func, classification)
```