[英]Allow user to input multiple values to be treated in while statement with R
Thanks to a solution found here , users can input a ticker/symbol which is then checked for availability both locally and on the Inte.net.多亏了这里找到的解决方案,用户可以输入代码/符号,然后在本地和 Inte.net 上检查其可用性。 If the former is yes and/or the latter is no, then ask again for user input, else download from the Inte.net:
如果前者是和/或后者不是,则再次询问用户输入,否则从 Inte.net 下载:
# load packages
library(svDialogs)
library(quantmod)
# start a loop; we'll find out if we need to exit based on user feedback
while (TRUE) {
# get a potential ticker value from the user
ticker <- toupper(dlgInput("Please enter a ticker that has not yet been loaded:")$res)
# if this value already exists in global environment, immediately go back to
# the beginning of the loop
if (exists(ticker)) next
# check Yahoo to see if the ticker is valid and if so get the results
yahooSymbol <- getSymbols.yahoo(ticker, env = globalenv())
# if yahoo returned a response, exit the loop
if (!identical(yahooSymbol, character(0))) break
}
But, when the user inputs more than one ticker/symbol, the code fails.但是,当用户输入多个代码/符号时,代码会失败。 In spite of this, when the code is run outside a
while
statement, it is able to handle multiple tickers/symbols (part of the solution found here ):尽管如此,当代码在
while
语句之外运行时,它能够处理多个代码/符号(此处找到的部分解决方案):
# load packages
library(svDialogs)
library(quantmod)
library(stringr)
# get tickers from user
ticker <- toupper(dlgInput("Please enter one or more tickers separated by comma.")$res)
# split tickers
tickers <- unlist(strsplit(gsub(" ", "", ticker, fixed = TRUE), ","))
# download from Yahoo!
yahooSymbol <- getSymbols.yahoo(tickers, env = globalenv())
# close all open Internet connections (as a precaution)
closeAllConnections()
So I thought that if code above works, why not in a while
statement like:所以我认为如果上面的代码有效,为什么不在
while
语句中,例如:
# load packages
library(svDialogs)
library(quantmod)
library(stringr)
while (TRUE) {
# get one or many tickers
ticker <- toupper(str_trim(dlgInput("Enter one or more new or not loaded tickers separated with comma.")$res))
# split tickers
tickers <- unlist(strsplit(gsub(" ", "", ticker, fixed = TRUE), ","))
# check locally if already loaded
if (exists(tickers)) next
# download from Yahoo!
yahooSymbol <- getSymbols.yahoo(tickers, env = globalenv())
# if yahoo returned a response, exit the loop
if (!identical(yahooSymbol, character(0))) break
}
Needless to say/write that I miserably failed with an Error in exists(tickers): first argument has length > 1
.不用说/写,我悲惨地失败了,并
Error in exists(tickers): first argument has length > 1
。 However, when I quote out if (exists(tickers)) next
, small victory, the yahooSymbol <- getSymbols.yahoo(tickers, env = globalenv())
does nevertheless download the symbols from Yahoo!然而,当我引用
if (exists(tickers)) next
时,小胜利, yahooSymbol <- getSymbols.yahoo(tickers, env = globalenv())
仍然从 Yahoo! 下载符号。
My question:我的问题:
Systems used:使用的系统:
The problem comes from the fact that exists
only tests for the existence of a single object.问题来自这样一个事实,即
exists
仅测试单个 object 的存在。
Try for example尝试例如
exists(c("a", "b"))
and you will get the same error that crashes your code.你会得到同样的错误,使你的代码崩溃。
To solve your problem, try要解决您的问题,请尝试
if (all(sapply(tickers, exists))) next
sapply
will allow you to "apply" the function exists
to all the elements in tickers
, sapply
将允许您将 function exists
“应用”到tickers
中的所有元素,all
will tell if the resulting vector given by the sapply
is composed only of TRUE
s. all
将判断sapply
给出的结果向量是否仅由TRUE
组成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.