Programmatic Utilities for Manipulating Formulas, Expressions, Calls, Assignments and Other R Objects

These utilities facilitate the programmatic manipulations of formulas, expressions, calls, assignments and other R language objects. These objects all share the same structure: a left-hand side, operator and right-hand side. This packages provides methods for accessing and modifying this structures as well as extracting and replacing names and symbols from these objects.


** Utilities for Formulas, Expressions, Calls, Assignments and Other R Objects**

CRAN_Status_Badge License Downloads software impact

These utilities facilitate the programmatic manipulations of formulas, expressions, calls, names, symbols and other R language objects. These objects all share the same structure: a left-hand side (lhs), an operator (op) and right-hand side (rhs). This packages provides methods for accessing and modifying this structures as well as extracting and replacing names and symbols from these objects.

It is easiest to understand through the [#Examples].

Installation

Stable Version

install.packages('formula.tools')

Development Verison

devtools::install_github("decisionpatterns/formula.tools")

Overview

Manipulate parts of a formula/expression/call/assignment:

  • lhs: get/set the left-hand side of an object
  • rhs: get/set the right-hand side of an object
  • op: get/set the operator of an object

Get variables (names) in a formula/expression/call/assignment:

  • get.vars: like base::all.vars except all symbols (e.g. .) can be interpolated from data, so that things like y ~ . can return the correct values.
    • lhs.vars: get.vars on lhs of the object
    • rhs.vars: get.vars on rhs of the object

Get terms from formula or expression:

  • terms: get the terms from an object by interpolating special symbols (e.g. .) using a data set data. Extends stats::terms()

  • split_terms: split (mathematical) terms into an expression vector without interpolating special symbols (e.g. .)

Examples

Formula terms and operators can be accessed or changed.

 
## Get parts of formula 
 
form <- y ~ x 
 
lhs(form)                # y
lhs(form) <- quote(t)
form                     # t ~ x
 
rhs(form)                # x
rhs(form) <- 1
form                     # t ~ 1
 
 
## Get variables (names)
 
form <- y ~ m*x + b
 
get.vars(form)           # "y" "m" "x" "b"
lhs.vars(form)           # "y"
rhs.vars(form)           # "m" "x" "b"
 
 
## Split terms
 
terms(form)
 
split_terms(form)        # expression(Species ~ . - Sepal.Length)
split_terms( quote(a+(b+(c+d))), recursive=TRUE )   # expression(a, b, c, d)
 

News

2018-02-22 Version 1.7.0

  • Add split_terms function
  • Add toggle.sign method
  • catcont functions removed. Use the catcont package instead.

2017-09-21 Version 1.6.8

  • Fix issue with Rd files causing a rejection by CRAN
  • Update docs, README.md

2017-07-20 Version 1.6.4

  • Fixes doc problems with '<-' methods
  • Fixes #6

2017-02-26 Version 1.6.0

  • Improve documentation
  • Import stats
  • Fix tests

2015-06-23 Version 1.5.4

  • Fix issue with CRAN blocking
  • Expand tests for get.vars, lhs.vars, rhs.vars, lhs, rhs

2015-06-22 Version 1.5.3

  • Fix issue #5, get.vars(NULL, NULL) throws an error ... Thanks to Paul Bailey for poinint out the problem.

2014-03-11 Version 1.4.0

  • Fix documentaion for roxygen-4.0.0+ (#1)
  • No other functional fixes

2014-02-25 Version 1.3.3

  • Fix to prevent breakage from new testthat
  • Documentation moved to Roxygen2 format

2012-11-30 Version 1.3.1

  • Fixes documentation
  • Adds Namespace

2011-10-03 Version 1.3.0

Added: as.character.formula

2011-08-08 Version 1.2.2

[Modified] Added methods lhs, rhs, op, op.type for '<-' assignment class. Previosly it did not handle this and produced an error.

  • n.b. statements with '->' return '<-'

2010-06-16 Version 1.2.1

[Modified]

  • Fixed problem with NAMESPACE file

2010-03

  • Change maintainer info

2010-09-29:

  • R/which.class.R: Changed is.cat and is.cont to be based on S4 Methods. This could have been done with S3, however the whole package uses S4.

2010-08-24

  • R/zzz.R: simplified

2010-08-24 Version 1.1.0

COMPLETE REFACTOR!!!

  • Now dependent on oeprator.tools-1.1.0
  • R/is.one.sided.R: is.two.sided is fixed
  • R/lhs.R:
  • R/rhs.R:
  • R/op.R:
    • fun( expression ) now returns an expression
    • replacement handles multiple values for replacing on expressions.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("formula.tools")

1.7.1 by Christopher Brown, a year ago


https://github.com/decisionpatterns/formula.tools


Report a bug at https://github.com/decisionpatterns/formula.tools/issues


Browse source code at https://github.com/cran/formula.tools


Authors: Christopher Brown [aut, cre] , Decision Patterns [cph]


Documentation:   PDF Manual  


GPL-2 | file LICENSE license


Imports operator.tools, utils, methods

Suggests magrittr, testthat


Imported by BMisc, MachineLearning, ccfa, dLagM, factorMerger, hettx, metR, moderndive, qte, smicd, vcrpart.

Depended on by AutoStepwiseGLM, LEGIT, flexrsurv, longRPart2.


See at CRAN