Updated: Revised code but got 2nd error as "{ : missing value where TRUE/FALSE needed"
Revised code according to the recommendation is as follows:
# Load in the keras package
#install.packages(keras)
library(keras)
# Install TensorFlow
#install.packages(tensorflow)
library(tensorflow)
# Install xlsx package to read xls/xlsx file
#install.packages(httr)
library(httr)
#install.packages(RCurl)
library(RCurl)
#install.packages(readxl)
library(readxl)
main <- function()
{
choice <- function(){
choice <- readline("Choose option 1 for Boston dataset, option 2 for Default dataset (1/2): ")
choice <- as.integer(choice)
return(choice)
}
choice()
if (choice() == 1) {
install.packages(MASS) # Import the MASS library
library(MASS)
dataset <- attach(Boston) # Import the Boston dataset
}
else if (choice() == 2) {
url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/00350/default%20of%20credit%20card%20clients.xls"
GET(url, write_disk("default.xls", overwrite=TRUE))
dataset <- read_xls('default.xls', sheet = 1, skip = 1)
}
else {
break
}
}
if(!interactive()) {
main()
}
Could anyone kindly explain for me? Thank you so much!
1st Error: I'm trying to run a while loop and a nested if/else if/else loop in the main function but I repeatedly got the error as comparison (2) is possible only for atomic and list types
My code is as follows:
# Load in the keras package
#install.packages(keras)
library(keras)
# Install TensorFlow
#install.packages(tensorflow)
library(tensorflow)
# Install xlsx package to read xls/xlsx file
#install.packages(httr)
library(httr)
#install.packages(RCurl)
library(RCurl)
#install.packages(readxl)
library(readxl)
main <- function()
{
choice <- function()
{
# ask user for their choice
choice <- readline(prompt="Choose option 1 for Boston dataset, option 2 for Default dataset, option 3 to quit (1/2/3): ")
# convert character into integer
choice <- as.integer(choice)
return(choice)
}
while (choice != 3)
{
if (choice == 1) # Boston dataset
{
# Import the MASS library
install.packages(MASS)
library(MASS)
# Import the Boston dataset
dataset <- attach(Boston)
}
else if (choice == 2) # Default dataset
{
url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/00350/default%20of%20credit%20card%20clients.xls"
GET(url, write_disk("default.xls", overwrite=TRUE))
dataset <- read_xls('default.xls', sheet = 1, skip = 1)
}
else # Wrong choice
{
print("Wrong choice. Please try again.")
}
}
break
}
if(!interactive()) {
main()
}
choiceat a prompt and then typechoice()to see the difference. Yourwhile(choice != 3)executes the former. You must also revise the loop logic, because when theifis reached,choiceis executed once again. - Rui Barradas