liveBook · Manning

Namespace operator

As explained in Statistics Slam Dunk

The R namespace operator accesses an exported object from a specified package without attaching that package to the search path.

The R namespace operator, written as a pair of colons (::), calls an exported function from a named package without attaching that package to the search path. For example, colorDF::highlight() both identifies the package and runs highlight(); colorDF must already be installed. section 19.3.1

Highlighting a season in NBA data

After filtering dat2 for George Mikan's records by year, run colorDF::highlight(test4, test4$Year == 1950). The returned table highlights the 1950 row while preserving the grouped NBA records. The same technique supports a one-line comparison of package-qualified functions without first calling library(colorDF). section 19.3.1

dat2 %>%
 group_by(Year) %>%
 filter(Player == 'George Mikan*') -> test4
colorDF::highlight(test4, test4$Year == 1950)
## # Tibble (class tbl_df) 6 x 6:
## # Groups: Year [6]
##  │Year │Player       │Pos  │Tm   │G    │PTS  
## 1│1950 │George Mikan*│C    │MNL  │   68│ 1865
## 2│1951 │George Mikan*│C    │MNL  │   68│ 1932
## 3│1952 │George Mikan*│C    │MNL  │   64│ 1523
## 4│1953 │George Mikan*│C    │MNL  │   70│ 1442
## 5│1954 │George Mikan*│C    │MNL  │   72│ 1306
## 6│1956 │George Mikan*│C    │MNL  │   37│  390

Inspect the complete package-qualified highlight call and its highlighted George Mikan output. section 19.3.1

arsenal::comparedf(test4, test5)
## Compare Object
## 
## Function Call: 
## arsenal::comparedf(x = test4, y = test5)
## 
## Shared: 5 non-by variables and 6 observations.
## Not shared: 1 variables and 0 observations.
## 
## Differences found in 0/5 variables compared.
## 0 variables compared have non-identical attributes.

Compare the related arsenal::comparedf call, which qualifies another package function on two data sets. section 19.3.1

How Statistics Slam Dunk covers it

How deep it goes
Defines the operator and demonstrates it in executable calls, but does not descend into namespace internals or export mechanisms.
Examples
Uses colorDF::highlight() on George Mikan records and arsenal::comparedf() on two NBA data sets.
What it assumes
Assumes basic R package and data-wrangling familiarity; teaches package-qualified function calls without library().
What you can do after
Apply it via R package-qualified calls to run exported functions without first attaching their packages.
How it connects
Connects package functions with library(), the search path, data frames, tibbles, and data-set comparison.

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.