r - 是否可以在 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)
解决方案
是的,有:使用功能readline
例如
name <- readline("What is your name? ")
推荐阅读
- javascript - JavaScript:如何动态定义方法?
- r - 数据问题:addAwesomeMarkers 需要 lng、lat,但我的数据存储在向量中。解决问题?
- python - 如何在 python 的 sklearn 中使用交叉验证执行 SMOTE
- javascript - Vue Apollo - 无法使用变量“无法分配给对象的只读属性'名称'”进行查询
- c# - 文本框下划线
- android - Volley JSONObject 请求解析响应返回无值
- php - CodeIgniter 数据库错误,但只有一部分应用程序
- python - 如何按照我需要的方式重塑这个数组?
- django - 如何在表单中使用 django_select2
- amazon-web-services - 失败:WebSocket 打开握手超时(亚马逊 AWS)