3

For a Ruby project I need an OpenCPU server to process some statistics by R packages. OpenCPU has an JSON REST API, so with Ruby I can easily hook into that to communicate with R. So far, so good.

One of those packages has a strange (at least to me) syntax.

add <- function(.argument1, .argument2) {
  return(.argument1 + .argument2)
}

So this is just an example, not the actual function. The part I found weird are those points in front of parameters. When I suggested to remove those points, the developer of the package said that it was meant to hide the variables from the overview of variables in IDE (R-Studio). It sounds to me like he wanted to make the parameters, what we call it, private, or at least to scope it somehow. I searched the internets to explain this feature of R, but found nothing.

The actual problem is: we use Ruby, Ruby translates data into JSON and sends it to OpenCPU. OpenCPU looks up for the right package/function, passes arguments and executes it. Then it returns to OpenCPU which makes a JSON response. Ruby's >1.9 Hash syntax that we prefer to work with looks like:

{ argument1: 4, argument2: 3 }.to_json
# => { "argument1": 4, "argument2": 3 }

instead of older forced by the package arguments:

{ ".argument1" => 4, ".argument2" => 3 }.to_json
# => { ".argument1": 4, ".argument2": 3 }

So in desperate search for an argument to convince the R-developer not to use the dots, my question was still unanswered: does this point in front of the parameters have a real functionality in R or is it just a hack to exclude the variables from the IDE?

Ivan
  • 874
  • 10
  • 32
  • 1
    Such object names are valid R syntax and used by several packages (possibly even by some base R functions?). Thus, you don't have a case and should handle this outside of R. – Roland Apr 25 '14 at 10:46
  • Dots are valid in R variable names. That is, the dot is part of the argument name and is not an operator that acts on the argument (as would be the case in SQL, say). In your `add` example, the first argument is `.argument1`, not `argument1`. – G. Grothendieck Apr 25 '14 at 11:40
  • Thanks for your comment @Roland. In most programming languages there are many ways to do one thing, but most of the languages have also a style guide that advises to choose one way or another. From your answer I assume there is no such thing in R as style guide? – Ivan Apr 25 '14 at 12:17
  • @Ivan There is no official style guide. Some organisations, notably [Google](https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml), have developed there own style guides, but there are several "schools" (specifically regarding under_score/camelCase/dotted.identifiers). – Roland Apr 25 '14 at 17:11

1 Answers1

2

I think you will find that the reason comes from the default behaviour of the ls function in R which does not return objects beginning with a point. This can be modified by the all.names parameter:

all.names a logical value. If TRUE, all object names are returned. If FALSE, names which begin with a . are omitted.

It is quite common for more technical functions or variables to be "hidden" in this way, e.g. .Machine, .Options, .Fortran, .dynLibs

James
  • 65,548
  • 14
  • 155
  • 193