Debian Codegraph @

man/find_globals.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/cache.R
3 \name{find_globals}
4 \alias{find_globals}
5 \alias{find_locals}
6 \title{Find global/local variables in R code}
7 \usage{
8 find_globals(code, envir = parent.frame())
9
10 find_locals(code)
11 }
12 \arguments{
13 \item{code}{Either a character vector of R source code, or an R expression.}
14
15 \item{envir}{The global environment in which global variables are to be
16 found.}
17 }
18 \value{
19 A character vector of the variable names. If the source code contains
20 syntax errors, an empty character vector will be returned.
21 }
22 \description{
23 Use \code{\link[codetools:findGlobals]{codetools::findGlobals()}} and \code{\link[codetools:findLocalsList]{codetools::findLocalsList()}} to find
24 global and local variables in a piece of code. Global variables are defined
25 outside the code, and local variables are created inside the code.
26 }
27 \note{
28 Due to the flexibility of creating and getting variables in R, these
29 functions are not guaranteed to find all possible variables in the code
30 (e.g., when the code is hidden behind \code{eval()}).
31 }
32 \examples{
33 x = 2
34 xfun::find_globals("y = x + 1")
35 xfun::find_globals("y = get('x') + 1") # x is not recognized
36 xfun::find_globals("y = zzz + 1") # zzz doesn't exist
37
38 xfun::find_locals("y = x + 1")
39 xfun::find_locals("assign('y', x + 1)") # it works
40 xfun::find_locals("assign('y', x + 1, new.env())") # still smart
41 xfun::find_locals("eval(parse(text = 'y = x + 1'))") # no way
42 }