首页 > 解决方案 > 是否可以在 R 中输入?

问题描述

在学习了几个月的 C++ 之后,对编程和学习 R 有了新的认识。是否可以在 C++ 中获得像 cin 这样的用户输入。我希望我的程序从用户那里得到一个二进制响应,并根据它的真假来增加或减少。

#Declaring the function
vector.maker.num1 <-function(len){
  #first condition results in the original function
  #used missing keyword to no get error message
  if(missing(len)){
    print(sample(1:20, 5))
  }
  #second condition results in printing a string warning of the length
  #being too small. Follewed by a random sample of any number below 5
  else if(len < 5){
    print("Warning Number is too small")
    sample(1:20, len)
  } 
  #same logic as previous condition except this one is for any number above
  #10 and it prints number being too large. Will still print the statement if it
  #is true but will not sample anything above 20.
  else if(len > 10){
    print("Number is too large")
    sample(1:20, len)
  }
#last condition of the function, executed if no other conditions are applicable
# only true if it is in between 5-10.
else{
    print("Number is just right")
    sample(1:20, len)
  }
}
vector.maker.num1(4)

标签: r

解决方案


是的,有:使用功能readline

例如

name <- readline("What is your name? ")

推荐阅读