博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rstudio r语言_如何在R中接受用户输入?
阅读量:2533 次
发布时间:2019-05-11

本文共 5873 字,大约阅读时间需要 19 分钟。

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 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 agex<-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:   PrajwalEnter 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 dollarsx<-as.numeric(readline(prompt = "Enter number of hours: "))y<-as.numeric(readline(prompt = "Enter per hour rate: "))total_wages_per_week<-x*ytotal_wages_per_weekpaste("Total wages per day is:$",x*y)

Output =

输出=

Enter number of hours:   8Enter per hour rate:   12.50100Output = "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: 8Enter 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:   PrajwalEnter 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:

更多研究:

翻译自:

rstudio r语言

转载地址:http://noqzd.baihongyu.com/

你可能感兴趣的文章
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>