1

I have lots of sales areas. Each one is a data.frame and I can create xts objects for each. This requires a line of code for each. I'd rather use a function to create on demand. I've tried using keyboard entry to create a character string, then strip the quotes and store the input as symbol, to be used in a function. This doesn't work!

## Function to specify unit
userinput <- scan("", what="")
userinput <- as.name(userinput[1])

## Function to create xts object
createXts=function(x) {
  xts(x[1], order.by=x$StartTime, dateFormat="POSIXct")
}
## Call function
createXts(userinput)
##Result:
>> Error in x[1] : object of type 'symbol' is not subsettable

How can I use user input to specify a data.frame to be used in a function? Note: The createXts function does work if I replace x with a real unit's name.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
felixmc
  • 516
  • 1
  • 4
  • 19

1 Answers1

1

How about:

CreateXts <- function () {
  xx <- scan("", what="")
  xx <- get(xx[[1]])
  xts(xx, order.by=xx$StartTime, dateFormat="POSIXct")
}
asb
  • 4,392
  • 1
  • 20
  • 30