首页 > 解决方案 > 在 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())
  
}

任何帮助表示赞赏。


使用的系统:

标签: rwhile-loop

解决方案


这是一个潜在的答案,它被简化为专注于核心逻辑。

我看到的问题是,通过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
}

推荐阅读