如何消除 R 函数中参数周围的引号?
以下是有效的 R 函数的前几行:
teetor <- function(x,y) {
require("quantmod")
require("tseries")
alpha <- getSymbols(x, auto.assign=FALSE)
bravo <- getSymbols(y, auto.assign=FALSE)
t <- as.data.frame(merge(alpha, bravo))
# ... <boring unit root econometric code>
}
当我传递两个股票代码作为函数参数时,我需要将它们用引号引起来:
teetor("GLD", "GDX")
我希望能够简单地键入:
teetor(GLD, GDX)
Here are the first few lines of an R function that works:
teetor <- function(x,y) {
require("quantmod")
require("tseries")
alpha <- getSymbols(x, auto.assign=FALSE)
bravo <- getSymbols(y, auto.assign=FALSE)
t <- as.data.frame(merge(alpha, bravo))
# ... <boring unit root econometric code>
}
When I pass two stock symbols as function parameters, I need to enclose them with quotes:
teetor("GLD", "GDX")
I want to be able to simply type:
teetor(GLD, GDX)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。为了节省几次击键而牺牲清晰、简单的代码是一个坏主意。您创建的函数只能交互使用,不能从其他函数调用。
Don't. It's a bad idea to sacrifice clear, simple code just to save a couple of keystrokes. You have created a function that can only be use interactively, not called from another function.
有几种方法可以做到这一点,但通常我不建议这样做。
通常,调用不带引号的内容意味着该对象本身位于搜索路径中。在不分配它的情况下执行此操作的一种方法是使用
with()
函数。您可以通过 deparse(substitute(...)) 获取某个东西的名称,而无需它实际存在:
因此原则上您可以使用 deparse(substitute(...) 获取名称) 如上例所示,在您的
teetor
函数中,而不是传入名称。There are several ways of doing this, but generally I wouldn't advise it.
Typically calling something without quotes like that means that the object itself is in the search path. One way to do this without assigning it is to use the
with()
function.You can get the name of something without having it actually exist by
deparse(substitute(...))
:So in principle you can get the names using
deparse(substitute(...))
as in the above example in yourteetor
function instead of passing in the names.好吧,我想一个解决方案是:
再想一想,没关系。
Well, I suppose one solution is:
On second thought, never mind.