tidycode v0.1.0

0

Monthly downloads

0th

Percentile

Analyze Lines of R Code the Tidy Way

Analyze lines of R code using tidy principles. This allows you to input lines of R code and output a data frame with one row per function included. Additionally, it facilitates code classification via included lexicons.

Readme

Travis build
status Codecov test
coverage

tidycode

The goal of tidycode is to allow users to analyze R expressions in a tidy way.

Installation

You can install tidycode from github with:

# install.packages("devtools")
devtools::install_github("LucyMcGowan/tidycode")

Example

Read in exisiting 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().

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.

library(tidycode)

We can use the expressions from this matahari tibble to extract the names of the packages included.

(pkg_names <- ls_packages(m$expr))
#> [1] "broom" "glue"

Create a data frame of your expressions, splitting each into individual functions.

u <- unnest_calls(m, expr)

Add in the function classifications!

u %>%
  dplyr::inner_join(
    get_classifications("crowdsource", include_duplicates = FALSE)
    )
#> Joining, by = "func"
#> # A tibble: 8 x 8
#>   result     error  output   warnings messages func   args   classification
#>   <list>     <list> <list>   <list>   <list>   <chr>  <list> <chr>         
#> 1 <chr [8]>  <NULL> <chr [1… <chr [1… <chr [0… libra… <list… setup         
#> 2 <chr [9]>  <NULL> <chr [1… <chr [1… <chr [0… libra… <list… setup         
#> 3 <S3: lm>   <NULL> <chr [1… <chr [0… <chr [0… <-     <list… data cleaning 
#> 4 <S3: lm>   <NULL> <chr [1… <chr [0… <chr [0… lm     <list… modeling      
#> 5 <S3: lm>   <NULL> <chr [1… <chr [0… <chr [0… ~      <list… modeling      
#> 6 <tibble [… <NULL> <chr [1… <chr [0… <chr [0… <-     <list… data cleaning 
#> 7 <tibble [… <NULL> <chr [1… <chr [0… <chr [0… tidy   <list… modeling      
#> 8 <S3: glue> <NULL> <chr [1… <chr [0… <chr [0… glue_… <list… communication

We can also remove a list of “stopwords”. We have a function, get_stopfuncs() that lists common “stopwords”, frequently used operators, like %>% and +.

u %>%
  dplyr::inner_join(
    get_classifications("crowdsource", include_duplicates = FALSE)
    ) %>%
  dplyr::anti_join(get_stopfuncs()) %>%
  dplyr::select(func, classification)
#> Joining, by = "func"
#> Joining, by = "func"
#> # A tibble: 5 x 2
#>   func      classification
#>   <chr>     <chr>         
#> 1 library   setup         
#> 2 library   setup         
#> 3 lm        modeling      
#> 4 tidy      modeling      
#> 5 glue_data communication

Functions in tidycode

Name Description
%>% Pipe operator
tidycode_example Get path to example file
get_classifications Get a tidy data frame of classifications of all functions used in your analysis
ls_packages List packages
unnest_calls Unnest R calls
get_stopfuncs Get a tidy data frame of a "stopword" lexicon for R functions
read_rfiles Read R file(s) as a tidy data frame
No Results!

Vignettes of tidycode

Name
tidycode.Rmd
No Results!

Last month downloads

Details

Type Package
License MIT + file LICENSE
Encoding UTF-8
LazyData true
RoxygenNote 6.0.1
URL https://github.com/LucyMcGowan/tidycode
BugReports https://github.com/LucyMcGowan/tidycode/issues
VignetteBuilder knitr
NeedsCompilation no
Packaged 2019-05-17 23:52:37 UTC; lucymcgowan
Repository CRAN
Date/Publication 2019-05-20 09:10:03 UTC

Include our badge in your README

[![Rdoc](http://www.rdocumentation.org/badges/version/tidycode)](http://www.rdocumentation.org/packages/tidycode)