如何在使用变量时避免显式名称

时间:2022-07-05 14:56:58

I have the following code in R:

R中有如下代码:

library(quantmod)

mySymbol = "^STOXX50E"
getSymbols(mySymbol, from="2004-01-01", to=Sys.Date())

chartSeries(Cl(STOXX50E))

which simply download the time series for the inder Eurostoxx and then plots the closing price. It works as expected. Anyway, I was wondering how I can avoid to write explicitely "STOXX50E" everytime I want to reference to this variable. For example, I would like to be able to reference the variable that contains the data with a generic name like "INDEX" so that I don't need to change all the calls when I want to launch the code with another inder.

它只需下载inder Eurostoxx的时间序列,然后绘制收盘价图。它能够正常工作。无论如何,我想知道如何避免在每次我想要引用这个变量时,写一个明确的“STOXX50E”。例如,我希望能够引用包含数据的变量,该变量具有“INDEX”这样的通用名称,以便在启动代码时不需要更改所有调用。

For example, if i want to download and plot the closing price for the S&P500 I have to do:

例如,如果我想下载并绘制标准普尔500指数的收盘价,我必须这样做:

library(quantmod)

mySymbol = "^GSPC"
getSymbols(mySymbol, from="2004-01-01", to=Sys.Date())

chartSeries(Cl(GSPC))

so I have to change the variable name not only on the second line but also on the last. I would rather something more generic like:

所以我不仅要在第二行,还要在最后一行改变变量名。我宁愿要一些更一般的东西,比如:

library(quantmod)

mySymbol = "^GSPC"
getSymbols(mySymbol, from="2004-01-01", to=Sys.Date())

chartSeries(Cl(mySymbol))

So that once I have set the name for mySymbol I don't have to change all the rest of the code. But this doesn't work. How can I accomplish this?

一旦我为mySymbol设置了名称,我就不需要修改所有代码了。但这是行不通的。我怎样才能做到这一点呢?

2 个解决方案

#1


1  

You can do it this way:

你可以这样做:

library(quantmod)

mySymbol = "^STOXX50E"
getSymbols(mySymbol, from="2004-01-01", to=Sys.Date())

chartSeries(Cl(get(substring(mySymbol,2,nchar(mysymbol)))))

If you want to change the title of the plot do:

如果你想改变标题的情节做:

chartSeries(Cl(get(substring(mySymbol,2,nchar(mysymbol)))), name=mySymbol)

Essentially when you use getSymbols a variable named STOXX50E is stored on your global environment which contains the data. Using get you can access a variable name by providing a string i.e. "^STOXX50E". I then use substring to avoid the first character of the variable mySymbol which is ^.

本质上,当您使用getSymbols时,一个名为STOXX50E的变量会存储在包含数据的全局环境中。使用get,你可以通过提供一个字符串来访问变量名。“^ STOXX50E”。然后使用字符串来避免变量的第一个字符mySymbol ^。

And it works. You essentially change mySymbol and the code runs without having to alter anything else!

和它的工作原理。本质上,您更改了mySymbol,代码运行时无需更改任何其他内容!

如何在使用变量时避免显式名称

EDIT:

编辑:

This is probably a better way in terms that the code is more readable and you avoid the annoying ^ in the title:

而言,这可能是一个更好的方法的代码可读性更强,并且你在标题:避免恼人的^

library(quantmod)

mySymbol = "STOXX50E"
getSymbols(paste('^',mySymbol,sep=''), from="2004-01-01", to=Sys.Date())

chartSeries(Cl(get(mySymbol)),name=mySymbol)

如何在使用变量时避免显式名称

#2


2  

An alternative to the currently accepted solution is to use auto.assign=FALSE in your call to getSymbols.

另一种可选的解决方案是使用auto。在调用getSymbols时,assign=FALSE。

library(quantmod)
mySymbol <- "^STOXX50E"
x <- getSymbols(mySymbol, from="2004-01-01", to=Sys.Date(), auto.assign=FALSE)
chartSeries(Cl(x), name=mySymbol)
# If you want to remove the "^" from the name:
chartSeries(Cl(x), name=sub("^","",mySymbol,fixed=TRUE))

I prefer this solution because I find the coder clearer and easier to understand.

我更喜欢这个解决方案,因为我发现编码器更清晰更容易理解。

#1


1  

You can do it this way:

你可以这样做:

library(quantmod)

mySymbol = "^STOXX50E"
getSymbols(mySymbol, from="2004-01-01", to=Sys.Date())

chartSeries(Cl(get(substring(mySymbol,2,nchar(mysymbol)))))

If you want to change the title of the plot do:

如果你想改变标题的情节做:

chartSeries(Cl(get(substring(mySymbol,2,nchar(mysymbol)))), name=mySymbol)

Essentially when you use getSymbols a variable named STOXX50E is stored on your global environment which contains the data. Using get you can access a variable name by providing a string i.e. "^STOXX50E". I then use substring to avoid the first character of the variable mySymbol which is ^.

本质上,当您使用getSymbols时,一个名为STOXX50E的变量会存储在包含数据的全局环境中。使用get,你可以通过提供一个字符串来访问变量名。“^ STOXX50E”。然后使用字符串来避免变量的第一个字符mySymbol ^。

And it works. You essentially change mySymbol and the code runs without having to alter anything else!

和它的工作原理。本质上,您更改了mySymbol,代码运行时无需更改任何其他内容!

如何在使用变量时避免显式名称

EDIT:

编辑:

This is probably a better way in terms that the code is more readable and you avoid the annoying ^ in the title:

而言,这可能是一个更好的方法的代码可读性更强,并且你在标题:避免恼人的^

library(quantmod)

mySymbol = "STOXX50E"
getSymbols(paste('^',mySymbol,sep=''), from="2004-01-01", to=Sys.Date())

chartSeries(Cl(get(mySymbol)),name=mySymbol)

如何在使用变量时避免显式名称

#2


2  

An alternative to the currently accepted solution is to use auto.assign=FALSE in your call to getSymbols.

另一种可选的解决方案是使用auto。在调用getSymbols时,assign=FALSE。

library(quantmod)
mySymbol <- "^STOXX50E"
x <- getSymbols(mySymbol, from="2004-01-01", to=Sys.Date(), auto.assign=FALSE)
chartSeries(Cl(x), name=mySymbol)
# If you want to remove the "^" from the name:
chartSeries(Cl(x), name=sub("^","",mySymbol,fixed=TRUE))

I prefer this solution because I find the coder clearer and easier to understand.

我更喜欢这个解决方案,因为我发现编码器更清晰更容易理解。