Skip to content
Prev 393694 / 398503 Next

[EXTERNAL] Re: function doesn't exists but still runs..... (akshay kulkarni)

Hi Akshay,

I usually use debug (a function provided by R). When you are stepping through a function your environment is the one in which function code is being executed, so you can easily see everything that is visible to the function. If you single step into a function that the first function calls then you also see everything that is available to that function. Moreover, you don't see anything that is not visible to the function you are debugging, so you can really determine what any piece of code would do if called inside the function.

Note 1: Code in R is always executed in an environment. I show in the example below that in the empty environment (the ultimate ancestor of all other environments) R can't even add. Usually the current environment (e.g. .GlobalEnv at the command line or a fresh environment created by a function call) has the right contents and the right parent to do what you expect, but in some special cases you need to understand how environments work. Even evalq & with are functions (unavailable for example in the empty environment), and the environment argument has to be evaluated in the current environment before the main expression can be evaluated in the environment that you want.
[[1]]

<environment: R_EmptyEnv>
Error in 2 + 2 : could not find function "+"
Error in 2L + 2L : could not find function "+"

Note 2: Besides automatically showing what a function sees, using debug (instead of hand-executing lines of code from the function) gives you the correct call stack. Suppose that you run some code at the debug command line to make a sub-sub-?-function do what you want, and you create in that environment what you hope is the correct return value. You can then use parent.frame() to put that value into the environment of the caller and run the caller's remaining code in the correct environment to see what happens. If there are no other problems then you can work your way up to top level and confirm that your patch has the right effect before you even modify the actual code of the offending function. (Saving all environments in .GlobalEnv forces R to keep them even if you quit the debugger. Combining eval & parse is sometimes more convenient than using evalq.)

Regards,
Jorgen.

------------------------------

Message: 2
Date: Sun, 22 Jan 2023 14:25:59 +0000
From: akshay kulkarni <akshay_e4 at hotmail.com>
To: Jorgen Harmse <JHarmse at roku.com>, "r-help at r-project.org"
        <r-help at r-project.org>, "williamwdunlap at gmail.com"
        <williamwdunlap at gmail.com>
Subject: Re: [R] [EXTERNAL] Re: function doesn't exists but still
        runs..... (akshay kulkarni)
Message-ID:
        <PU4P216MB1568BDE0E4929806E096CC11C8CB9 at PU4P216MB1568.KORP216.PROD.OUTLOOK.COM>

Content-Type: text/plain; charset="utf-8"

Dear Jorgen,
                        regrets to reply this late....
I got into this issue because it threw an error, and it took more than 4 days to fix this. I  learnt a lot, and one things I learnt  is to debug the function, even when that is a public package function....instead of googling the error message...Any ideas on how to do this more efficiently?

THanking you,
Yours sincerely,
AKSHAY M KULKARNI