简体   繁体   English

通过键盘输入创建xts对象的函数

[英]Function to create xts object from keyboard input

I have lots of sales areas. 我有很多销售区域。 Each one is a data.frame and I can create xts objects for each. 每个对象都是一个data.frame,我可以为每个对象创建xts对象。 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? 如何使用用户输入指定要在函数中使用的data.frame? Note: The createXts function does work if I replace x with a real unit's name. 注意:如果我将x替换为真实单位的名称, createXts函数将起作用。

How about: 怎么样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM