R Examples for Beginners – R User Input

(R Example for Citizen Data Scientist & Business Analyst)

 

readinteger <- function()
{ 
  n <- readline(prompt="Enter an integer: ")
  return(as.integer(n))
}

print(readinteger())
Enter an integer: 88
[1] 88

The readline function

readline() lets the user enter a one-line string at the terminal.
The prompt argument is printed in front of the user input. It usually ends on “: “.

The as.integer function

as.integer makes an integer out of the string.

Preventing failure if no number is entered

Right now if the user doesn’t enter an integer, as.integer will return NA (Not Available). We can avoid this by using is.na to check the user input and asking again if the value is NA:

readinteger <- function()
{ 
  n <- readline(prompt="Enter an integer: ")
  n <- as.integer(n)
  if (is.na(n)){
    n <- readinteger()
  }
  return(n)
}

print(readinteger())
Enter an integer: 
Enter an integer: boo
Enter an integer: 44
[1] 44
Warning message:
In readinteger() : NAs introduced by coercion

However, a warning message is still shown. This is how to avoid it:

readinteger <- function()
{ 
  n <- readline(prompt="Enter an integer: ")
  if(!grepl("^[0-9]+$",n))
  {
    return(readinteger())
  }
  
  return(as.integer(n))
}

print(readinteger())
Enter an integer: 
Enter an integer: 31r132weq
Enter an integer: effasdf
Enter an integer: 222
[1] 222 

grepl returns TRUE if the regular expression “^[0-9]+$” is matched. (The expression checks for a string that consists of nothing but one or more digits.)

! negates the result and the if branch is executed if the user-entered string isn’t an integer.

 

Python tutorials for Business Analyst – Python Input, Output and Import

 

R Examples for Beginners – R User Input

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!