Documentation for Eidos function sapply, which is a method of Eidos. Note that the R function is a stub, it does not do anything in R (except bring up this documentation). It will only do anything useful when used inside a slim_block function further nested in a slim_script function call, where it will be translated into valid SLiM code as part of a full SLiM script.

eidos_sapply(x, lambdaSource, simplify)

Arguments

x

An object of type any or string or string. See details for description.

lambdaSource

An object of type any or string or string. Must be of length 1 (a singleton). See details for description.

simplify

An object of type any or string or string. Must be of length 1 (a singleton). The default value is "vector". See details for description.

Value

An object of type any.

Details

Documentation for this function can be found in the official SLiM manual: page NA.

Named apply() prior to Eidos 1.6 / SLiM 2.6 Applies a block of Eidos code to the elements of x. This function is sort of a hybrid between c() and executeLambda(); it might be useful to consult the documentation for both of those functions to better understand what sapply() does. For each element in x, the lambda defined by lambdaSource will be called. For the duration of that callout, a variable named applyValue will be defined to have as its value the element of x currently being processed. The expectation is that the lambda will use applyValue in some way, and will return either NULL or a new value (which need not be a singleton, and need not be of the same type as x). The return value of sapply() is generated by concatenating together all of the individual vectors returned by the lambda, in exactly the same manner as the c() function (including the possibility of type promotion). Since this function can be hard to understand at first, here is an example: sapply(1:10, "if (applyValue 49 81. The sapply() operation begins with the vector 1:10. For each element of that vector, the lambda is called and applyValue is defined with the element value. In this respect, sapply() is actually very much like a for loop. If applyValue is even (as evaluated by the modulo operator, the if statement is F and so NULL is returned by the lambda; this must be done explicitly, since a void return is not allowed by sapply(). If applyValue is odd, on the other hand, the lambda returns its square (as calculated by the exponential operator, ^). Just as with the c() function, NULL values are dropped during concatenation, so the final result contains only the squares of the odd values. This example illustrates that the lambda can "drop" values by returning NULL, so sapply() can be used to select particular elements of a vector that satisfy some condition, much like the subscript operator, []. The example also illustrates that input and result types do not have to match; the vector passed in is integer, whereas the result vector is float. Beginning in Eidos 1.6, a new optional parameter named simplify allows the result of sapply() to be a matrix or array in certain cases, better organizing the elements of the result. If the simplify 82 parameter is "vector", the concatenated result value is returned as a plain vector in all cases; this is the default behavior, for backward compatibility. Two other possible values for simplify are presently supported. If simplify is "matrix", the concatenated result value will be turned into a matrix with one column for each non-NULL value returned by the lambda, as if the values were joined together with cbind(), as long as all of the lambda's return values are either (a) NULL or (b) the same length as the other non-NULL values returned. If simplify is "match", the concatenated result value will be turned into a vector, matrix, or array that exactly matches the dimensions as x, with a one-toone correspondence between x and the elements of the return value just like a unary operator, as long as all of the lambda's return values are singletons (with no NULL values). Both "matrix" and "match" will raise an error if their preconditions are not met, to avoid unexpected behavior, so care should be taken that the preconditions are always met when these options are used. As with executeLambda(), all defined variables are accessible within the lambda, and changes made to variables inside the lambda will persist beyond the end of the sapply() call; the lambda is executing in the same scope as the rest of your code. The sapply() function can seem daunting at first, but it is an essential tool in the Eidos toolbox. It combines the iteration of a for loop, the ability to select elements like operator [], and the ability to assemble results of mixed type together into a single vector like c(), all with the power of arbitrary Eidos code execution like executeLambda(). It is relatively fast, compared to other ways of achieving similar results such as a for loop that accumulates results with c(). Like executeLambda(), sapply() is most efficient if it is called multiple times with a single string script variable, rather than with a newly constructed string for lambdaSource each time. Prior to Eidos 1.6 (SLiM 2.6), sapply() was instead named apply(); it was renamed to sapply() in order to more closely match the naming of functions in R. This renaming allowed a new apply() function to be added to Eidos that operates on the margins of matrices and arrays, similar to the apply() function of R (see apply(), above).

See also

Other Eidos: Eidos, eidos_abs(), eidos_acos(), eidos_all(), eidos_any(), eidos_apply(), eidos_array(), eidos_asFloat(), eidos_asInteger(), eidos_asLogical(), eidos_asString(), eidos_asin(), eidos_assert(), eidos_atan2(), eidos_atan(), eidos_beep(), eidos_catn(), eidos_cat(), eidos_cbind(), eidos_ceil(), eidos_citation(), eidos_clock(), eidos_cmColors(), eidos_color2rgb(), eidos_colors(), eidos_cor(), eidos_cos(), eidos_cov(), eidos_createDirectory(), eidos_cumProduct(), eidos_cumSum(), eidos_c(), eidos_date(), eidos_dbeta(), eidos_debugIndent(), eidos_defineConstant(), eidos_defineGlobal(), eidos_deleteFile(), eidos_dexp(), eidos_dgamma(), eidos_diag(), eidos_dim(), eidos_dmvnorm(), eidos_dnorm(), eidos_drop(), eidos_elementType(), eidos_exists(), eidos_exp(), eidos_fileExists(), eidos_filesAtPath(), eidos_findInterval(), eidos_float(), eidos_floor(), eidos_flushFile(), eidos_format(), eidos_functionSignature(), eidos_functionSource(), eidos_getSeed(), eidos_getwd(), eidos_heatColors(), eidos_hsv2rgb(), eidos_identical(), eidos_ifelse(), eidos_integerDiv(), eidos_integerMod(), eidos_integer(), eidos_isFinite(), eidos_isFloat(), eidos_isInfinite(), eidos_isInteger(), eidos_isLogical(), eidos_isNAN(), eidos_isNULL(), eidos_isObject(), eidos_isString(), eidos_length(), eidos_license(), eidos_log10(), eidos_log2(), eidos_logical(), eidos_log(), eidos_lowerTri(), eidos_ls(), eidos_match(), eidos_matrixMult(), eidos_matrix(), eidos_max(), eidos_mean(), eidos_min(), eidos_nchar(), eidos_ncol(), eidos_nrow(), eidos_object(), eidos_order(), eidos_paste0(), eidos_paste(), eidos_pmax(), eidos_pmin(), eidos_pnorm(), eidos_print(), eidos_product(), eidos_qnorm(), eidos_quantile(), eidos_rainbow(), eidos_range(), eidos_rank(), eidos_rbeta(), eidos_rbind(), eidos_rbinom(), eidos_rcauchy(), eidos_rdunif(), eidos_readCSV(), eidos_readFile(), eidos_repEach(), eidos_rep(), eidos_rev(), eidos_rexp(), eidos_rf(), eidos_rgamma(), eidos_rgb2color(), eidos_rgb2hsv(), eidos_rgeom(), eidos_rlnorm(), eidos_rmvnorm(), eidos_rm(), eidos_rnbinom(), eidos_rnorm(), eidos_round(), eidos_rpois(), eidos_runif(), eidos_rweibull(), eidos_sample(), eidos_sd(), eidos_seqAlong(), eidos_seqLen(), eidos_seq(), eidos_setDifference(), eidos_setIntersection(), eidos_setSeed(), eidos_setSymmetricDifference(), eidos_setUnion(), eidos_setwd(), eidos_sin(), eidos_size(), eidos_sortBy(), eidos_sort(), eidos_source(), eidos_sqrt(), eidos_stop(), eidos_strcontains(), eidos_strfind(), eidos_string(), eidos_strprefix(), eidos_strsplit(), eidos_strsuffix(), eidos_str(), eidos_substr(), eidos_sumExact(), eidos_sum(), eidos_suppressWarnings(), eidos_sysinfo(), eidos_system(), eidos_tabulate(), eidos_tan(), eidos_tempdir(), eidos_terrainColors(), eidos_time(), eidos_trunc(), eidos_ttest(), eidos_type(), eidos_t(), eidos_unique(), eidos_upperTri(), eidos_usage(), eidos_var(), eidos_version(), eidos_whichMax(), eidos_whichMin(), eidos_which(), eidos_writeFile(), eidos_writeTempFile()

Author

Benjamin C Haller (bhaller@benhaller.com) and Philipp W Messer (messer@cornell.edu)