Bug 17177 - Wishlist: as.formula(<character>) not to eval arbitrarily
Summary: Wishlist: as.formula(<character>) not to eval arbitrarily
Alias: None
Product: R
Classification: Unclassified
Component: Wishlist (show other bugs)
Version: R 3.3.0
Hardware: ix86 (32-bit) Windows 32-bit
: P5 enhancement
Assignee: R-core
URL: https://stat.ethz.ch/pipermail/r-deve...
Depends on:
Reported: 2016-11-04 17:06 UTC by Suharto Anggono
Modified: 2016-11-07 15:19 UTC (History)
1 user (show)

See Also:

Attempted change (1.95 KB, patch)
2016-11-04 17:10 UTC, Suharto Anggono
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Suharto Anggono 2016-11-04 17:06:07 UTC
Examples in https://stat.ethz.ch/pipermail/r-devel/2016-November/073338.html surprised me. They are reproduced below.

R> as.formula("x")
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

R> as.formula("env")
Error: object of type 'special' is not subsettable
R> as.formula("...")
Error in eval(expr, envir, enclos) : '...' used in an incorrect context
R> y <- "response ~ pred1 + pred2"
R> as.formula("y")
response ~ pred1 + pred2
R> f <- function(x) { y <- "foo ~ bar" ; as.formula(x) }
R> f("y")
response ~ pred1 + pred2
<environment: 0x03276548>
R> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows XP (build 2600) Service Pack 2

[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

The following statement from https://stat.ethz.ch/pipermail/r-devel/2016-November/073338.html summarizes quite well.
The character method for formula works well if its 'x' argument looks like
a formula, but it doesn't act consistently otherwise.

Questions in https://stat.ethz.ch/pipermail/r-devel/2016-November/073339.html:
Is it possible to add a note to the help page regarding the conclusion (or
even the edge cases)?
is it possible to add any validation for x in formula.character?

My own wish for as.formula(<character>) is not to do 'eval' arbitrarily or not to do 'eval' at all.
Comment 1 Suharto Anggono 2016-11-04 17:10:26 UTC
Created attachment 2179 [details]
Attempted change

Changed definition of the internal function 'notnull' in 'formula.default' is my attempt to get rid of "not subsettable" error.
Comment 2 Benjamin Tyner 2016-11-04 17:52:49 UTC
Another one:

> as.formula("text")
Error: object of type 'closure' is not subsettable
Comment 3 Benjamin Tyner 2016-11-07 15:19:55 UTC
Never mind; I'd forgotten about graphics::text

(In reply to Benjamin Tyner from comment #2)
> Another one:
> > as.formula("text")
> Error: object of type 'closure' is not subsettable