knitr::opts_chunk$set(collapse = TRUE, comment = "#>") library(flifo)
The flifo package provides a few functions to create and manipulate FIFO (First In First Out), LIFO (Last In First Out), and NINO (Not In or Never Out) stacks in R.
Functions fifo
, lifo
, and nino
are made to
create empty stacks. For instance:
# Create an empty LIFO s <- lifo() print(s) is.empty(s) is.fifo(s) is.lifo(s)
Then push
and pop
enable one to add elements to and retrieve
elements from the stack, respectively.
# Add values to 's' push(s, 0.3) push(s, data.frame(x=1:2, y=2:3)) print(s) size(s)# in bytes # Retrive the last element inserted pop(s) size(s)
A maximum number of elements can be specified at the creation of the stack (no limit in the number of elements is the default).
s <- fifo(max_length = 3) max_length(s) #> [1] 3 # max_length can be changed max_length(s) <- 2 push(s, 1) push(s, 2) push(s, 3) # generates an error #> Error in push(s, 3) : '.stack' is full
If an object exists in the current environment e
and is pushed into the stack, it disappears from e
:
s <- lifo() x <- 3.14 exists("x") push(s, x) exists("x")
The nino
function creates a stack from which we cannot retrieve anything:
s <- nino() push(s, "foo") print(s) #> NINO stack: no element can be reached pop(s) # generates an error #> Error in pop(s) : cannont retrieve elements from a 'nino' stack
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.