A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://tony-aw.github.io/tinycodet/articles/tinycodet.html below:

• tinycodet

Introduction

The tinycodet R-package adds some functions to help in your coding etiquette. It primarily focuses on 4 aspects:

  1. Safer decimal (in)equality testing, standard-evaluated alternatives to with() and aes(), and other functions for safer coding.
  2. A new package import system, that attempts to combine the benefits of using a package without attaching, with the benefits of attaching a package.
  3. Extending the string manipulation capabilities of the stringi R package.
  4. Reducing repetitive code.

The tinycodet R-package has only one dependency, namely stringi. Most functions in this R-package are fully vectorized and optimized, and have been well documented.

Here I’ll give a quick glimpse of what is possible in this R package.

Safer functionality

‘tinycodet’ adds some functions to help in coding more safely:

One example of aes_pro():

New import system

One can use a package without attaching the package (for example using ::), or one can attach a package (for example using library() or require()). The advantages and disadvantages of using without attaching a package versus attaching a package - at least those relevant for now - can be compactly presented in the following table:

aspect :: attach 1 prevent masking functions from other packages Yes (+) No (-) 2 prevent masking core R functions Yes (+) No (-) 3 clarify which function came from which package Yes (+) No (-) 4 place/expose functions only in current environment instead of globally Yes (+) No (-) 5 prevent namespace pollution Yes (+) No (-) 6 minimize typing - especially for infix operators
(i.e. typing package::`%op%`(x, y) instead of x %op% y is cumbersome) No (-) Yes (+) 7 use multiple related packages,
without constantly switching between package prefixes No (-) Yes (+) NOTE: + = advantage, - = disadvantage

What tinycodet attempts to do with its import system, is to somewhat find the best of both worlds. It does this by introducing the following functions:

Here is an example using tinycodet's new import system; note that the following code is run without attaching a single R package (besides tinycodet itself of course):

# importing "tidytable" + "data.table" under alias "tdt.":
import_as( 
  ~ tdt., "tidytable", dependencies = "data.table"
)
## Importing packages and registering methods...
## Done
## You can now access the functions using `tdt.$`
## For conflicts report, packages order, and other attributes, run `attr.import(tdt.)`
# exposing operators from `magrrittr` to current environment:
import_inops("magrittr")
## Checking for conflicting infix operators in the current environment...
## Placing infix operators in current environment...
## Done
# directly assigning the "starwars" dataset to object "d":
d <- import_data("dplyr", "starwars") 

# see it in action:
d %>% tdt.$filter(species == "Droid") %>%
  tdt.$select(name, tdt.$ends_with("color"))
## # A tidytable: 6 × 4
##   name   hair_color skin_color  eye_color
##   <chr>  <chr>      <chr>       <chr>    
## 1 C-3PO  NA         gold        yellow   
## 2 R2-D2  NA         white, blue red      
## 3 R5-D4  NA         white, red  red      
## 4 IG-88  none       metal       red      
## 5 R4-P17 none       silver, red red, blue
## 6 BB8    none       none        black
Extending the string manipulation capabilities of stringi

‘tinycodet’ adds some additional functionality to ‘stringi’ (the primary package for string manipulation):

Reduce repetitive code
# in base R:
ifelse( # repetitive, and gives unnecessary warning
  is.na(object>0), -Inf,
  ifelse(
    object>0,  log(object), object^2
  )
)
mtcars$mpg[mtcars$cyl>6] <- (mtcars$mpg[mtcars$cyl>6])^2 # long

# with tinycodet:
object |> transform_if(\(x)x>0, log, \(x)x^2, \(x) -Inf) # compact & no warning
mtcars$mpg[mtcars$cyl>6] %:=% \(x)x^2 # short

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4