---
title: "Safer functionality"
output:
  rmarkdown::html_vignette:
    toc: true
    number_sections: true
vignette: >
  %\VignetteIndexEntry{Safer functionality}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(tinycodet)
```


# Decimal (in)equality testing operators

This package adds the `%d==%, %d!=% %d<%, %d>%, %d<=%, %d>=%` (in)equality operators, which perform safer decimal number truth testing. They are virtually equivalent to the regular (in)equality operators,
`==, !=, <, >, <=, >=`,
except for 2 aspects:

 1) The `%d...%` operators assume that if the absolute difference between any two numbers x and y is smaller than the Machine tolerance, sqrt(.Machine$double.eps), then x and y should be consider to be equal.
 For example: `0.1*7 == 0.7` returns `FALSE`, even though they are equal, due to the way decimal numbers are stored in programming languages like 'R' and 'Python'. But `0.1*7 %d==% 0.7` returns `TRUE`.

 2) Only numeric input is allowed, so characters are not coerced to numbers.
 I.e. `1 < "a"` gives `TRUE` , whereas `1 %d<% "a"` gives an error.
 For character equality testing, see %s==% from the 'stringi' package.


Thus these provide safer decimal number (in)equality operators.



Some examples:

```{r}
x <- c(0.3, 0.6, 0.7)
y <- c(0.1*3, 0.1*6, 0.1*7)
print(x); print(y)
x == y # gives FALSE, but should be TRUE
x!= y # gives TRUE, should be FALSE
x > y # not wrong
x < y # gives TRUE, should be FALSE
x %d==% y # here it's done correctly
x %d!=% y
x %d<% y # correct
x %d>% y # correct
x %d<=% y # correct
x %d>=% y # correct

```

&nbsp;

There are also the `x %d{}% bnd` and `x %d!{}% bnd` operators, where `bnd` is a vector of length 2, or a 2-column matrix `(nrow(bnd)==length(x) or nrow(bnd)==1)`.
The `x %d{}% bnd` operator checks if `x` is within the **closed** interval with bounds defined by `bnd`.
The `x %d!{}% bnd` operator checks if `x` is outside the **closed** interval with bounds defined by `bnd`.


Examples:

```{r}

x <- c(0.3, 0.6, 0.7)
bnd <- cbind(x-0.1, x+0.1)
x %d{}% bnd
x %d!{}% bnd

```


&nbsp;

# with_pro and aes_pro

'tinycodet' provides standard-evaluated versions of the common quoting functions `with()` and `ggplot2::aes()`: `with_pro()` and `aes_pro()`, respectively. See example below.

```{r fig.width = 8}

requireNamespace("ggplot2")
d <- import_data("ggplot2", "mpg")

# mutate data:
myform <- ~ displ + cyl + cty + hwy
d$mysum <- with_pro(d, myform)
summary(d)

# plotting data:
x <- ~ cty
y <- ~ sqrt(hwy)
color <- ~ drv

ggplot2::ggplot(d, aes_pro(x, y, color = color)) +
  ggplot2::geom_point()


```

&nbsp;


# Locked constants

One can re-assign the values `T` and `F`. One can even run something like `T <- FALSE` and `F <- TRUE`! `tinycodet` adds the `lock_TF()` function that forces `T` to stay `TRUE` and `F` to stay `FALSE`. Essentially, the `lock_TF()` function creates the locked constant `T` and `F`, assigned to `TRUE` and `FALSE` respectively, to prevent the user from re-assigning them. Removing the created `T` and `F` constants allows re-assignment again.

&nbsp;

The `X %<-c% A` operator creates a `constant` `X` with assignment `A`. Constants cannot be changed, only accessed or removed. So if you have a piece of code that requires some unchangeable constant, use this operator to create said constant.


&nbsp;

# Safer Partial Matching

The `safer_partialmatch()`  forces 'R' to give a warning when partial matching occurs when using the dollar ($) operator, or when other forms of partial matching occurs. It simply calls the following:

```{r echo = TRUE, eval = FALSE}

options(
   warnPartialMatchDollar = TRUE,
   warnPartialMatchArgs = TRUE,
   warnPartialMatchAttr = TRUE
 )

```


&nbsp;

