substitue(groups) is the name "my_groups". Since my_groups is not a column of pixelData(x) nor a name in the environment e, the tryCatch() statement in .try_eval() tries to find my_groups in p, which is the execution environment of the Cardinal::image() function. Since there is no variable named my_groups in p, we get an error.
For reference, here's the call stack printed by calling lobstr::cst() within .try_eval() just before the error is thrown:
# slightly different wrapper function around Cardinal::image()
my_image <- function(mse, groups) {
Cardinal::image(mse, groups = groups)
}
my_image(mse, groups = "circle")
#> Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character'
This time, substitue(groups) is the name "groups". Although groups is not a column of pixelData(x), it is a name in the environment e. Specifically, with the debugger paused inside .try_eval(), printing groups gives
function (x)
{
UseMethod("groups")
}
<bytecode: 0x00000209231eab18>
<environment: namespace:dplyr>
So the first line below actually returns without error, with the groups variable now referencing the function dplyr::groups(). Consequently, an error is thrown when trying to convert groups to a factor.
Consider the following code snippet.
Cardinal::image()
works fine if called from the global environment.However, calling
Cardinal::image()
within a user-defined function can lead to errors. Below are 2 examples.Example 1
The bug occurs in the following line:
https://github.com/kuwisdelu/Cardinal/blob/9a61aeb6b43bdb8d5f5dec3b55105be77befa1c7/R/image2-SparseImagingExperiment.R#L66-L67
Note that
e
was previously defined in the same functionhttps://github.com/kuwisdelu/Cardinal/blob/9a61aeb6b43bdb8d5f5dec3b55105be77befa1c7/R/image2-SparseImagingExperiment.R#L36
and
.try_eval()
is defined ashttps://github.com/kuwisdelu/Cardinal/blob/9a61aeb6b43bdb8d5f5dec3b55105be77befa1c7/R/utils2.R#L71-L75
substitue(groups)
is the name "my_groups". Sincemy_groups
is not a column ofpixelData(x)
nor a name in the environmente
, thetryCatch()
statement in.try_eval()
tries to findmy_groups
inp
, which is the execution environment of theCardinal::image()
function. Since there is no variable namedmy_groups
inp
, we get an error.For reference, here's the call stack printed by calling
lobstr::cst()
within.try_eval()
just before the error is thrown:Example 2
This time,
substitue(groups)
is the name "groups". Althoughgroups
is not a column ofpixelData(x)
, it is a name in the environmente
. Specifically, with the debugger paused inside.try_eval()
, printinggroups
givesSo the first line below actually returns without error, with the
groups
variable now referencing the functiondplyr::groups()
. Consequently, an error is thrown when trying to convertgroups
to a factor.https://github.com/kuwisdelu/Cardinal/blob/9a61aeb6b43bdb8d5f5dec3b55105be77befa1c7/R/image2-SparseImagingExperiment.R#L66-L70