我正在写一个从求职网站上提取信息(个人资料)的抓取项目。我希望用户发送的配置文件的数量和配置文件的位置从控制台&这2个数值在我的程序开始。我已经正确地完成了抓取部分,我将运行整个程序.I只是想知道我如何接受输入,同时让R等待一段时间?
我已经有了大部分的堆栈溢出解决方案,比如使用readLines或扫描。
library("dplyr")#pipe operator
library("purrr")#compact
library("rvest")#static webscraping
library("RSelenium")#dynamic webscraping
library("stringr")#str_trim
library("zeallot")#multiple assignment of variables %<-%
##val must contain number of profiles & position of profiles send through ##console , how do i make R wait so that user can send input.
val = 0
FUN3 <- function(n=2) {
val <<- readline(prompt ="YESS")
}
FUN3()
#global default settings
##every data frame you create after executing that line will not auto-convert to factors unless explicitly told to do so
options(stringsAsFactors = FALSE)
##Setting the default encoding of string(character) to UTF-8 for non-ASCII characters
options(encoding="utf-8")
#variables defined global for storing values
vector <- c("name","ind","post","locate","currently_With","role_Hiring")
#assign empty list to each variable
c(name,ind,position,locate,currently_With,role_Hiring) %<-%
lapply(vector,function(x) assign(x,list()))
#start selenium server
rD <- rsDriver(port = 4547L,browser="firefox")
remDr <- rD[["client"]]
发布于 2019-07-30 18:15:32
正如您正确地提到的,readline()
是一种很好的方法。这比你尝试过的方法更简单:只需添加
val = readline("Input number of profiles and position: ")
这将以字符的形式返回值。如果需要两个数值,请使用
val = as.numeric(strsplit(readline("Input number of profiles and position: ")," ")[[1]])
因此,用户可以输入由空格分隔的两个值。
如果您想让R
在您输入值后等待,请使用Sys.sleep(5)
(这将使R
等待5秒)
编辑
将其包装在函数周围将使代码停止,直到用户输入val
的值。
library("dplyr")#pipe operator
library("purrr")#compact
library("rvest")#static webscraping
library("RSelenium")#dynamic webscraping
library("stringr")#str_trim
library("zeallot")#multiple assignment of variables %<-%
function1 = function(){
val = as.numeric(strsplit(readline("Input number of profiles and position: ")," ")[[1]])
#global default settings
##every data frame you create after executing that line will not auto-convert to factors unless explicitly told to do so
options(stringsAsFactors = FALSE)
##Setting the default encoding of string(character) to UTF-8 for non-ASCII characters
options(encoding="utf-8")
#variables defined global for storing values
vector <- c("name","ind","post","locate","currently_With","role_Hiring")
#assign empty list to each variable
c(name,ind,position,locate,currently_With,role_Hiring) %<-%
lapply(vector,function(x) assign(x,list()))
#start selenium server
rD <- rsDriver(port = 4547L,browser="firefox")
remDr <- rD[["client"]]
}
然后让用户调用function1()
https://stackoverflow.com/questions/57268824
复制相似问题