在 R 中合并 2 个 while 语句

Merge 2 while statements in R

我有两个单独的 while 语句,但是由于相互依赖,我需要将它们合并。根据这个 Whosebug 答案,这应该非常简单。但是,我试过没有用。

在分隔的 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())
  
}

下面是根据提到的 stakoverflow post 两个 while 条件的初步合并。但是,无论符号(无论是否存在),代码似乎都没有“反应”(因为缺少更好的术语):

# 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())
  
}

感谢任何帮助。


使用的系统:

这是一个可能的答案,它稍微简化以专注于核心逻辑。

我看到的问题是,在 while() 循环中,您正在检查条件 before 获取重要用户输入和 after 执行影响全局环境的命令。

相反,此代码会在执行后评估每个部分:首先是用户输入以查看他们是否提供了已被读取的代码,其次是 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
}