【问题标题】:Merge 2 while statements in R在 R 中合并 2 while 语句
【发布时间】:2021-11-03 20:03:32
【问题描述】:

我有两个单独的 while 语句,但是相互依赖我需要合并它们。根据这个stackoverflow 的回答,这应该很简单。但是,我尝试的方法不起作用。

在分隔的while 语句下方(使用 cmets):

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 post。但是,无论符号(无论是否存在),代码似乎都没有“反应”(因为没有更好的术语):

# 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

【问题讨论】:

    标签: r while-loop


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢@ChristopherBelanger。你的代码就像一个魅力:-D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 2016-03-16
    • 1970-01-01
    • 2020-01-18
    • 2021-06-02
    • 1970-01-01
    相关资源
    最近更新 更多