# NOT RUN {
x <- rray(1:8, c(2, 2, 2))
# `rray_subset()` powers `[` so these are identical
rray_subset(x, 1)
x[1]
# Trailing dots are ignored, so these are identical
x[1]
x[1,]
# Missing arguments are treated as selecting the
# entire dimension, consistent with base R.
# This selects all of the rows, and the first column.
x[,1]
# Notice that you can't actually do the above with base
# R. It requires you to fully specify the dimensions of `x`.
# This would throw an error.
x_arr <- as_array(x)
try(x_arr[,1])
# To get the same behavior, you have to do:
x_arr[, 1, , drop = FALSE]
# Note that you can use base R arrays with `rray_subset()`
rray_subset(x_arr, , 1)
# For higher dimensional objects, `pad()` can be
# useful for automatically adding commas. The
# following are equivalent:
x[pad(), 1]
x[, , 1]
# You can assign to index locations with
# `x[...] <- value`
# This assigns 99 to the entire first row
x[1] <- 99
x
# First row in the first
# element of the 3rd dimension
x[1, , 1] <- 100
x
# Note that `value` is broadcast to the shape
# of `x[...]`. So this...
x[,1] <- matrix(5)
# ...becomes the same as
x[,1] <- array(5, c(2, 1, 2))
# You can also use `rray_subset<-()` directly to
# use these semantics with base R
rray_subset(x_arr, , 1) <- matrix(5)
x_arr
# }
Run the code above in your browser using DataLab