0
votes

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()
}
1
Type only choice at a prompt and then type choice() to see the difference. Your while(choice != 3) executes the former. You must also revise the loop logic, because when the if is reached, choice is executed once again. - Rui Barradas
Thank you so much @RuiBarradas. I revised the code and updated the post! Unfortunately, I got a new error :( - Hang Nguyen

1 Answers

0
votes

The following function returns the chosen data set, dataset. If the choice is Boston, it does not attach it, just returns it.

#install.packages('httr')
#install.packages('RCurl')
#install.packages('readxl')

library(httr)
library(RCurl)
library(readxl)
library(MASS)    # base package

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)
  }
  
  ch <- choice()  
  while (ch != 3) 
  {
    if (ch == 1) # Boston dataset
    {
      # Import the Boston dataset
      dataset <- Boston
    } else if (ch == 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.")
    }
    ch <- choice()
  }
  dataset
}

if(!interactive()) {
  main()
}