This function creates a 1-4 dimensional grid in long format, with the cell positions encoded in the x, y, z, and t columns. A long_cell object is the base class for the tidy interface to ambient, and allows a very flexible approach to pattern generation at the expense of slightly lower performance than the noise_* functions that maps directly to the underlying C++ code.

long_grid(x, y = NULL, z = NULL, t = NULL)

grid_cell(grid, dim, ...)

# S3 method for long_grid
as.array(x, value, ...)

# S3 method for long_grid
as.matrix(x, value, ...)

# S3 method for long_grid
as.raster(x, value, ...)

slice_at(grid, ...)

Arguments

x, y, z, t

For long_grid() vectors of grid cell positions for each dimension. The final dimensionality of the object is determined by how many arguments are given. For slice_at() an integer defining the index at the given dimension to extract.

grid

A long_grid object

dim

The dimension to get the cell index at, either as an integer or string.

...

Arguments passed on to methods (ignored)

value

The unquoted value to use for filling out the array/matrix

Examples

grid <- long_grid(1:10, seq(0, 1, length = 6), c(3, 6))

# Get which row each cell belongs to
grid_cell(grid, 2) # equivalent to grid_cell(grid, 'y')
#>   [1] 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6 1
#>  [38] 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6 1 1
#>  [75] 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2
#> [112] 2 3 3 4 4 5 5 6 6

# Convert the long_grid to an array and fill with the x position
as.array(grid, x)
#> , , 1
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    1    1    1    1    1    1
#>   [2,]    1    1    1    1    1    1
#> 
#> , , 2
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    2    2    2    2    2    2
#>   [2,]    2    2    2    2    2    2
#> 
#> , , 3
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    3    3    3    3    3    3
#>   [2,]    3    3    3    3    3    3
#> 
#> , , 4
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    4    4    4    4    4    4
#>   [2,]    4    4    4    4    4    4
#> 
#> , , 5
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    5    5    5    5    5    5
#>   [2,]    5    5    5    5    5    5
#> 
#> , , 6
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    6    6    6    6    6    6
#>   [2,]    6    6    6    6    6    6
#> 
#> , , 7
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    7    7    7    7    7    7
#>   [2,]    7    7    7    7    7    7
#> 
#> , , 8
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    8    8    8    8    8    8
#>   [2,]    8    8    8    8    8    8
#> 
#> , , 9
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    9    9    9    9    9    9
#>   [2,]    9    9    9    9    9    9
#> 
#> , , 10
#> 
#>       y
#> z      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]   10   10   10   10   10   10
#>   [2,]   10   10   10   10   10   10
#> 

# Extract the first column
slice_at(grid, x = 1)
#> # A tibble: 12 × 3
#>        x     y     z
#>    <int> <dbl> <dbl>
#>  1     1   0       3
#>  2     1   0       6
#>  3     1   0.2     3
#>  4     1   0.2     6
#>  5     1   0.4     3
#>  6     1   0.4     6
#>  7     1   0.6     3
#>  8     1   0.6     6
#>  9     1   0.8     3
#> 10     1   0.8     6
#> 11     1   1       3
#> 12     1   1       6

# Convert the first column to a matrix filled with y position
as.matrix(slice_at(grid, x = 1), y)
#>       z
#> y      [,1] [,2] [,3] [,4] [,5] [,6]
#>   [1,]    0  0.2  0.4  0.6  0.8    1
#>   [2,]    0  0.2  0.4  0.6  0.8    1