Book Image

Learning Shiny

By : Hernan Resnizky
Book Image

Learning Shiny

By: Hernan Resnizky

Overview of this book

Table of Contents (19 chapters)
Learning Shiny
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Introducing R, RStudio, and Shiny
Index

The lapply, vapply, sapply, and apply functions


These functions are equivalent to a for-each loop with the advantage that they are much more efficient in terms of performance. Basically, the function is applied over every item in a vectorized object. Its main structure is:

function(object_to_iterate_on, function, additional_arguments(separated by commas))

vapply() and apply() have additional arguments that will be covered in detail in the expanded explanations of these functions.

The function argument can be an already defined function (with its arguments), as follows:

sample.list <- list(a=runif(100,0,1), b=runif(500,0,100),
c=runif(35,0,200))
sapply(sample.list, quantile, probs=0.75)
##       a.75%       b.75%       c.75% 
##   0.7145661  77.4817679 158.9351519

Also, the function can be defined within the same apply-function call (these types of functions are called anonymous functions), as follows:

sapply(sample.list, function(x) round(sum(x+2)))
##     a     b     c 
##   250 27235...