rstudio r语言

Taking a user input is very simple in R using readline() function. In this tutorial, we will see how we can prompt the user in R.

在R中,使用readline()函数获取用户输入非常简单。 在本教程中,我们将看到如何在R中提示用户。

Interacting with the users for the input is going to be the best part whenever you code. Prompting the user for the input and reacting to that input is way exciting than anything. In this tutorial, we are going to take any user input’s and let’s do some meaningful stuff with it.

每当您编码时,与用户进行交互将是最好的部分。 提示用户输入内容并对该输入做出React比什么都令人兴奋。 在本教程中,我们将接受任何用户输入,并使用它进行一些有意义的工作。

Let’s roll!!!

来吧!!!

让我们从语法开始 (Let’s start with the syntax)

readline(): The readline() function will read the lines which is given in the terminal. The syntax is given as,

readline(): readline()函数将读取终端中给定的行。 语法为


readline(prompt=" ")

Where: “prompt = ” is a string that is requested by the user as input and it should be enclosed by double-quotes.

其中:“ 提示符=”是用户请求作为输入的字符串 ,并且应使用双引号将其引起来。



让我们从用户那里简单提示 (Let’s take a simple prompt from the user)

Now, let’s take simple user input from the user, and then we are going to print the result as well.

现在,让我们从用户那里获取简单的用户输入,然后我们还将打印结果。


#prompts the user
x<-readline(prompt = "Enter the organization name: ")

Output = Enter the organization name: Journaldev

输出= 输入组织名称: Journaldev

Let’s see another sample of prompting the age of the user.

让我们看看另一个提示用户年龄的示例。


#prompts the user age
x<-readline(prompt = "Enter your age: ")

Output = Enter your age: 22

输出 = 输入您的年龄: 22



R中的多个用户提示 (Multiple user-prompts in R)

In R you can take multiple users prompts using readline() function. The feature of multiple user inputs is highly useful as a complete sequence of the questionnaire can be created using this.

在R中,您可以使用readline()函数获取多个用户提示。 多个用户输入的功能非常有用,因为可以使用此功能创建问卷的完整序列。

After taking the user inputs, we are going to concatenate them in a meaningful way. This will be illustrated below.

接受用户输入后,我们将以有意义的方式将它们连接起来。 这将在下面说明。

Let’s see how this works.

让我们看看它是如何工作的。


a<-readline(prompt = "Enter your first name: ")

Enter your first name: Prajwal

输入您的名字: Prajwal

b<-readline(prompt = "Enter your last name: ")

Enter your last name: C N

输入您的姓氏: CN


paste("Hello,",a,b,'Welcome to JD')

“Hello, Prajwal C N Welcome to JD”

“您好,Prajwal CN欢迎来到JD”

As shown in the above examples, you can create user prompts and print them in a meaningful way.

如以上示例所示,您可以创建用户提示并以有意义的方式打印它们。



一行中有多个提示 (Multiple prompts in a single line )

In R we have discussed the multiple prompts in the above section. But do you know that you can have multiple prompts for users with a single line of code? Sounds interesting right? Let’s see how it works.

在R中,我们已经在上一节中讨论了多个提示。 但是您知道吗,只需一行代码就可以为用户提供多个提示? 听起来很有趣吧? 让我们看看它是如何工作的。


{name <- readline(prompt="Enter your name: ");
+ age <- readline(prompt="Enter your age: ");paste("Howdy,",name,age)}

This prompt will ask the user about their name, age, and then concatenates them with the welcome message as shown in the below output.

该提示将询问用户其姓名,年龄,然后将其与欢迎消息连接起来,如下面的输出所示。


Enter your name:   Prajwal
Enter your age:   CN
"Howdy,  Prajwal CN"


通过用户输入计算每日工资 (Calculating daily wages with user input )

This section will be interesting as we are going to prompt the user for the input data and based on that data we are going to calculate the daily wages of the labor.

本节将很有趣,因为我们将提示用户输入数据,并根据这些数据来计算劳动的日工资。

Let’s see how it works.

让我们看看它是如何工作的。


#here we are dealing with US dollars
x<-as.numeric(readline(prompt = "Enter number of hours: "))
y<-as.numeric(readline(prompt = "Enter per hour rate: "))
total_wages_per_week<-x*y
total_wages_per_week
paste("Total wages per day is:$",x*y)

Output =

输出=


Enter number of hours:   8
Enter per hour rate:   12.50
100
Output = "Total wages per day is:    $ 100"


In this modern era, people want faster actions. We want everything to be done in a quick fashion. So I will try to save some time for you by illustrating this example in a single line of code.

在这个现代时代,人们想要更快的行动。 我们希望一切都能快速完成。 因此,我将通过在一行代码中说明此示例来尝试为您节省一些时间。

Let’s see how it works.

让我们看看它是如何工作的。


{
x<-as.numeric(readline(prompt = "Enter the number of hours per day: "));
y=as.numeric(readline(prompt = "Enter per hour rate: "));
total_wages_per_week<-x*y;total_wages_per_week;
paste("Total wages per day is:$",x*y)
}

Output =

输出=


Enter the number of hours per day: 8
Enter per hour rate: 12.50
"Total wages per day is:  $ 100"

As shown above, you can prompt the user for input data and you can compute some meaningful insights such as finding simple interest, calculating daily wages, and many more in merely a single line of code.

如上所示,您可以提示用户输入数据,并且可以仅通过一行代码就可以计算出一些有意义的见解,例如发现简单的兴趣,计算日工资等。



使用readline()进行简单的登录设置 (Simple login setup using readline())

In this section, we are going to create a simple login set up to prompt the user to give the input or enter the details to log in. After that, we are displaying a welcome message to the user.

在本节中,我们将创建一个简单的登录设置,以提示用户输入或输入详细信息以进行登录。之后,我们将向用户显示欢迎消息。

Let’s see how it works.

让我们看看它是如何工作的。


x<-readline(prompt = "Enter your username: ")
y<-readline(prompt = "Enter your password: ")
paste("Welcome to Journaldev, Mr.",x)

Output =

输出=


Enter your username:   Prajwal
Enter your password:   Prajwal@jd
"Welcome to Journaldev,   Mr. Prajwal"


结语 (Wrapping up)

As we have discussed in the above sections, take user input or prompting a user for the input is pretty much easy in R. The readline() function helps to take the user input in R as shown above.

正如我们在以上各节中讨论的那样,在R中接受用户输入或提示用户输入是非常容易的。如上所示,readline()函数有助于在R中接受用户输入。

We, humans, love the interaction. We love talking to each other. So for all of us, R has made these things simple with functions like readline(). Thanks to R, its always going to be fun when you interact with the user through your code.

我们人类喜欢互动。 我们喜欢互相交谈。 因此,对于我们所有人来说,R使用readline()之类的函数使这些事情变得简单。 多亏了R,当您通过代码与用户互动时,它总是很有趣。

That’s all about taking user input in R. Happy learning!!!

这就是在R中获取用户输入的全部内容。 祝您学习愉快!!!

More study: R documentation

更多研究: R文档

翻译自: https://www.journaldev.com/40628/user-input-in-r

rstudio r语言

rstudio r语言_如何在R中接受用户输入?相关推荐

  1. python 运行r语言_如何在R中运行Python

    python 运行r语言 尽管我很喜欢R,但很显然Python还是一种很棒的语言-既适用于数据科学又适用于通用计算. R用户想要在Python中做一些事情可能有充分的理由. 也许这是一个很棒的库,还没 ...

  2. python输入复数_如何在Python中作为用户输入获取复数?

    我正在尝试构建一个执行复数基本运算的计算器.我正在使用在网上找到的计算器的代码,并且希望能够将用户输入作为复数.现在,代码使用int(input)来获取要求值的整数,但我希望输入形式为complex( ...

  3. linux中更改用户密码_如何在Linux中更改用户密码

    linux中更改用户密码 In this tutorial, we will focus on how you can change a user's password in Linux. We wi ...

  4. 计算密码子频率的代码R语言_科学网—R语言终止密码子统计 - 熊荣川的博文

    熊荣川 六盘水师范学院生物信息学实验室 #R语言终止密码子统计 setwd("**") infile = "protein1.fasta" outname = ...

  5. wordpress头像被墙_如何在WordPress中更改用户头像的形状

    wordpress头像被墙 Ever come across a site that has custom shapes for their user avatars or gravatars? Wa ...

  6. mysql中用户权限的授予_如何在MySQL中创建用户和授予权限

    如何创建MySQL用户并授予权限?为了实现良好的安全性,需要为每个应用程序创建单独的用户帐户,而不是root用户访问数据库.这将确保应用程序无法访问其他应用程序的数据库.因此需要mysql管理员(ro ...

  7. 如何在 Python 中验证用户输入

    要验证用户输入: 使用 while 循环进行迭代,直到提供的输入值有效. 检查输入值在每次迭代中是否有效. 如果该值有效,则跳出 while 循环. # ✅ 验证用户输入的是否是整数num = 0wh ...

  8. 1071svm函数 r语言_如何利用R语言中的rpart函数建立决策树模型

    决策树是根据若干输入变量的值构造出一个适合的模型,以此来预测输出变量的值,并用树形结构展示出来.决策树主要有两个类别:分类树和回归树.分类树主要针对离散的目标变量,回归树则针对连续的目标变量.R语言中 ...

  9. java获取语言_如何在java中获取语言环境对象?

    我在框架Spring和Liferay中使用Java. 使用liferay我知道如何获得一个语言环境(对象有一些信息:语言,国家......),但现在我在一个没有与liferay连接的java类,我不知 ...

最新文章

  1. http请求过程简要
  2. 【图文】云栖大会深圳峰会:阿里云ET医疗大脑与工业大脑,机器学习平台PAI2.0...
  3. 使用register_chrdev注册字符设备
  4. sr里简体中文的代码_常用的语言代码对照表
  5. HyperV2012的学习,从这里开始
  6. python画图显示中文乱码_解决Python pandas plot输出图形中显示中文乱码问题
  7. MyBatis框架学习笔记03:利用MyBatis实现关联查询
  8. 全国计算机一级考试试题大题,2016年全国计算机一级考试试题汇集
  9. QString、QByteArray 相互转换、和16进制与asc2转换
  10. 杭电多校第一场补题-1002 Balanced Sequence
  11. PowerDesigner生成sql建表语句
  12. shell 值判断,大于0,等于0,小于0,大于等于0,小于等于0,不等于0
  13. mac pycharm如何打开setting
  14. 只需一个损失函数,一个超参数即可压缩BERT,MSRA提模型压缩新方法
  15. HTML和CSS中的图像与背景图像
  16. Kubernetes 国外镜像的网络问题
  17. 计算机中怎么查找时间日志,电脑的系统日志怎么查看
  18. cin.tie与sync_with_stdio加速I/O
  19. 康耐视InSight相机实现ModBusTCP通讯详解
  20. python代做在哪找靠谱_现在知道莆田鞋在哪买靠谱普及_莆田鞋在哪买靠谱

热门文章

  1. 练习:----计算阶乘按钮
  2. 关于 xib 的使用
  3. 用Netty解析Redis网络协议
  4. Asp.Net细节性问题精萃
  5. [转载] C++ std::vector指定位置插入
  6. [转载] set集合python_python基础-set集合
  7. [转载] Python中pandas dataframe删除一行或一列:drop函数
  8. 面向对象及软件工程——团队作业3
  9. 记录一些关于操作数据库(本地和linux服务器)常用的命令
  10. bzoj2257瓶子与燃料——最大公约数