Show a table and a chart in a notebook
Read a CSV file into a phibble data frame, summarize it in one notebook cell, and plot it in the next.
Requires the php kernel from Installation. The pipe tab needs PHP 8.5, and the method chain runs on PHP 8.3 and later.
Setup
Create a Composer project and start JupyterLab in it:
mkdir mtcars && cd mtcarscomposer require phpstats/phibble phpstats/phplyr \ phpstats/phiz~/.venvs/juphyter/bin/jupyter labCreate a notebook with the PHP kernel from the launcher. The data is mtcars from R’s datasets package: fuel economy (mpg, miles per gallon), cylinders (cyl), weight (wt, in 1,000 lb), and other design measures of 32 cars tested by Motor Trend in 1974. The notebook reads it from the PHPstats datasets repository by URL.
Cells
- A
usestatement applies to its own cell and every later one. - phplyr’s
round()replaces PHP’s built-inround()in the cells after the import.
use Phibble\DataFrame;use Phiz\Plot;use function Phplyr\{phplyr, group_by, summarize};use function Phplyr\{arrange, desc, n, mean, round};A cell’s last value is its output. A phibble data frame is shown as a table. The summary counts the cars with each number of cylinders and averages their fuel economy.
$mtcars = DataFrame::fromCsv( 'https://raw.githubusercontent.com/' .'phpstats/datasets/main/csv/mtcars.csv');$mtcars |> group_by('cyl') |> summarize(cars: n(), mpg: round(mean('mpg'), 1)) |> arrange(desc('mpg'));| cyl <int> | cars <int> | mpg <dbl> | |
|---|---|---|---|
| 1 | 4 | 11 | 26.7 |
| 2 | 6 | 7 | 19.7 |
| 3 | 8 | 14 | 15.1 |
$mtcars = DataFrame::fromCsv( 'https://raw.githubusercontent.com/' .'phpstats/datasets/main/csv/mtcars.csv');phplyr($mtcars) ->groupBy('cyl') ->summarize(cars: n(), mpg: round(mean('mpg'), 1)) ->arrange(desc('mpg')) ->get();| cyl <int> | cars <int> | mpg <dbl> | |
|---|---|---|---|
| 1 | 4 | 11 | 26.7 |
| 2 | 6 | 7 | 19.7 |
| 3 | 8 | 14 | 15.1 |
Variables persist across cells, so the next cell plots $mtcars from the cell before. A phiz plot is shown as a chart, here fuel economy against weight.
Plot::of($mtcars) ->aes(x: 'wt', y: 'mpg') ->point() ->labs(x: 'Weight (1,000 lb)', y: 'Miles per gallon');See also: Rich output, JupyterLab, phplyr, and phiz.