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.
You can install flifo from GitHub with:
# install.packages("devtools")
::install_github("paulponcet/flifo") devtools
Functions fifo
, lifo
, and nino
are provided to create empty stacks. For instance:
library(flifo)
# Create an empty LIFO
<- lifo()
s print(s)
is.empty(s) # TRUE
is.fifo(s) # FALSE
is.lifo(s) # TRUE
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
pop(s) # get the last element inserted
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).
<- fifo(max_size = 3)
s max_size(s)
# max_size can be changed
max_size(s) <- 2
push(s, 1)
push(s, 2)
push(s, 3) # generates an error (the stack is full)
If an object exists in the current environment e
and is
pushed into the stack, it disappears from e
:
<- lifo()
s <- 3.14
x exists("x") # TRUE
push(s, x)
exists("x") # FALSE
The nino
function creates a stack from which we cannot
not retrieve anything:
<- nino()
s push(s, "foo")
print(s)
pop(s) # generates an error