I have two separate while
statements, however being interdependent I need to merge them. According to this stackoverflow answer this should be pretty straightforward. However, what I tried did not work.
Below the separated while
statements (with comments):
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())
}
Below is a tentative merger of the two while
conditions as per mentioned stakoverflow post. However, the code does not seem to "react" (for lack of a better term) whatever the symbol (whether existing or not):
# 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())
}
Any help appreciated.
System used:
- R version: 4.1.1 (2021-08-10)
- RStudio version: 1.4.1717
- OS: macOS Catalina version 10.15.7