Class <- calls in R

This may be a simple question. But why are these two classes different?

class(call("assign", "x", 2))
[1] "call"

class(call("<-", "x", 2))
[1] "<-"

Why is there a class <-for calls?

+4
source share
1 answer

I am sure that this behavior is a deduction from S and is not related to R. You are executing a relatively undefined part of the R language: what is an object class that really uses S3 or S4?

Let's look at the source code of the answers:

class
#> function (x)  .Primitive("class")
pryr::show_c_source(.Primitive("class"))
#> class is implemented by R_do_data_class with op = 0

This leads us to R_do_data_class, and then R_data_class. The object is not S4 and does not have a class attribute, so it does not return to some default values. In the case of LANGSXPs, it calls lang2str:

/* character elements corresponding to the syntactic types in the
   grammar */
static SEXP lang2str(SEXP obj, SEXPTYPE t)
{
  SEXP symb = CAR(obj);
  static SEXP if_sym = 0, while_sym, for_sym, eq_sym, gets_sym,
    lpar_sym, lbrace_sym, call_sym;
  if(!if_sym) {
    /* initialize:  another place for a hash table */
    if_sym = install("if");
    while_sym = install("while");
    for_sym = install("for");
    eq_sym = install("=");
    gets_sym = install("<-");
    lpar_sym = install("(");
    lbrace_sym = install("{");
    call_sym = install("call");
  }
  if(isSymbol(symb)) {
    if(symb == if_sym || symb == for_sym || symb == while_sym ||
       symb == lpar_sym || symb == lbrace_sym ||
       symb == eq_sym || symb == gets_sym)
      return PRINTNAME(symb);
  }
  return PRINTNAME(call_sym);
}

, .

, R, :

f <- function(x) UseMethod("f")
f.if <- function(x) "If statement"
f.while <- function(x) "While loop"

x <- quote(if (a) TRUE)
f(x)
#> "If statement"

y <- quote(while(TRUE){})
f(y)
#> "While loop"

(, , , , )

+2

All Articles