r - 在 R 中合并 2 while 语句
问题描述
我有两个单独while
的语句,但是相互依赖我需要合并它们。根据this stackoverflow answer这应该很简单。但是,我尝试过的没有奏效。
在分隔的语句下方while
(带注释):
library(svDialogs)
library(quantmod)
# List of data.frame names
loadedDataframes <- names(which(unlist(eapply(.GlobalEnv,is.data.frame))))
# ask user to enter a ticker
ticker <- toupper(dlgInput(paste("Enter a ticker (these are already loaded:", toString(stripExtension), ")"), "e.g., AAPL")$res)
# check if ticker already loaded as ```data.frame```
existsTicker <- exists(ticker)
# while loop until a not loaded data.frame ticker is typed
while (existsTicker == TRUE){
# Ask ticker again
ticker <- toupper(dlgInput(paste("Please enter a ticker that has not yet been loaded:", toString(stripExtension), ")"), "e.g., AAPL")$res)
# Check if ticker exists and load value in existsTicker
existsTicker <- exists(ticker)
}
# get some stock prices from default service
yahooSymbol <- getSymbols.yahoo(ticker, env = globalenv())
# Close an open internet connection
# If getSymbols.yahoo has an unknown ticker, it leaves connection to Yahoo! open
closeAllConnections()
# Check if yahooSymbol returned a character(0) and if true
# ask for an existing ticker at Yahoo! Finance
while (identical(yahooSymbol, character(0)) == TRUE) {
# Ask for an existing ticker at Yahoo! Finance
ticker <- toupper(dlgInput(paste("Please enter an existing ticker at Yahoo! Finance, except: ", toString(loadedDataframes), ")"))$res)
# Check if ticker exists and load value in existsTicker
yahooSymbol <- getSymbols.yahoo(ticker, env = globalenv())
}
while
以下是根据提到的 stakoverflow 帖子的两个条件的初步合并。但是,无论符号(无论是否存在),代码似乎都没有“反应”(因为没有更好的术语):
# Merge two conditions into one while loop
while (existsTicker == TRUE && identical(yahooSymbol, character(0)) == TRUE) {
# Ask ticker again
ticker <- toupper(dlgInput(paste("Please enter a ticker that has not yet been loaded:", toString(stripExtension), ")"), "e.g., AAPL")$res)
# Check if ticker exists and load value in existsTicker
existsTicker <- exists(ticker)
# Check if ticker exists and load value in existsTicker
yahooSymbol <- getSymbols.yahoo(ticker, env = globalenv())
}
任何帮助表示赞赏。
使用的系统:
- R 版本:4.1.1 (2021-08-10)
- RStudio 版本:1.4.1717
- 操作系统:macOS Catalina 版本 10.15.7
解决方案
这是一个潜在的答案,它被简化为专注于核心逻辑。
我看到的问题是,通过while()
循环,您在获取重要用户输入之前和执行影响全局环境的命令之后检查条件。
取而代之的是,此代码在执行后评估每个部分:首先是用户输入以查看他们是否提供了已被读取的代码,然后是来自 Yahoo 的响应以查看它是否有效。
# 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
}
推荐阅读
- python - docker run 中的烧瓶主机地址
- javascript - 为什么更改 testMatch 正则表达式时 Jest 覆盖率报告会中断?
- python - 熊猫数据框按一列分组,同时将其他列相乘
- azure-active-directory - 身份验证示例机器人不允许我在 Microsoft Teams 中进行身份验证
- django - 初始本地 Django mysql 数据库
- java - 如何修复从内部类引用的局部变量必须是最终或有效的最终错误
- java - 将 XSSFWorkbook 转换为 SXSSFWorkbook APACHE POI 时出现问题
- javascript - btn-group-justify 新的引导程序等效项。如何?
- gephi - Gephi 中的边缘厚度
- javascript - 如何从 JavaScript 中获取复制的文本