user1471980
user1471980

Reputation: 10636

How do you pass unknown number of arguments to R function via opencpu?

I have this R function. The first argument is the json data object. I convert the json object to data frame then based on other arguments given to this function, I subset the data frame based on given arguments. Arguments could be all or subset of column names in the dataframe, so it will vary.

convert to custom json data

data<-function(x, ...) {
  x<-fromJSON(x)
  (x <- lapply(x, function(x) { as.data.frame(x) }))
  (x <- do.call(rbind, x))
  x<-unique(x)
  
  cols <- c(...)
  dd<-cbind(Date=x[,1],subset(x, select=cols))
  mm <- melt(dd)
  ss <- split(mm, mm$variable)
  
  poo <- unname(Map(function(n,x) 
    list(name=n, data=unname(lapply(split(x, 1:nrow(x)), function(x) {
      list(x$Date, x$value)
    }))), names(ss),ss))
  p<-toJSON(poo)
  return(p)
}

My question is this. Given a function that could take different number of arguments, how can I reference the arguments?

Fora example, I have this data drame:

structure(list(DateTime = structure(1:8, .Label = c("8/24/2014 15:20", 
"8/24/2014 15:55", "8/24/2014 16:04", "8/24/2014 16:18", "8/24/2014 16:27", 
"8/24/2014 16:42", "8/24/2014 16:56", "8/24/2014 17:10"), class = "factor"), 
    Server1 = c(6.09, 4.54, 5.03, 4.93, 6.27, 4.59, 5.91, 4.53
    ), Server2 = c(5.7, 4.38, 4.52, 4.61, 4.18, 4.61, 4.37, 4.3
    ), Server3 = c(5.21, 5.33, 4.92, 5.56, 5.62, 6.73, 4.76, 
    4.59)), .Names = c("DateTime", "Server1", "Server2", "Server3"
), row.names = c("DateTime.1", "DateTime.2", "DateTime.3", "DateTime.4", 
"DateTime.5", "DateTime.6", "DateTime.7", "DateTime.8"), class = "data.frame")

I need to call my data function,

data(x, "DateTime","Server1")

someother time maybe:

data(x, "Datetime", "Server1", "Server2")

Is there a way to pass a function argument list?

For example:

arg_list<-c("DateTime", "Server1", "Server3")

data(x, arg_list)

Upvotes: 0

Views: 1813

Answers (1)

John Paul
John Paul

Reputation: 12684

The simplest thing to do here is to pass your column names as a list. You can then write the function so that is will work no matter how many elements are in the list, and you can even test for the list being NA and make an appropriate response for that.

As for the question about passing arguments, arguments can be passed to a function in the form of a list. This works well with do.call For example:

 PlottList<-list(x=rnorm(10), y=rnorm(10), col="red",pch=16, xlab="X",ylab="Y")
 do.call(plot, PlotList)

If you want to have some named parameter passed and combine them with the list you can do:

  PlottList2<-list( y=rnorm(10), col="red",pch=16, xlab="X",ylab="Y")
   do.call(plot,c(list(x=rnorm(10)),PlotList2))

note that x was not in PlotList2. It represents a name argument that you combine in your do.call function. If you pass your arguments as a list and use do.call then you don't need to know all of the arguments ahead of time, assuming, of course, that thew will eventually be passed to an function that recognizes them. This can be helpful if you want one set of unknown arguments represented as ... to go to one function and another set to go to a second function,

Upvotes: 2

Related Questions