aleksandr barakin Asked:2020-05-10 21:15:31 +0000 UTC2020-05-10 21:15:31 +0000 UTC 2020-05-10 21:15:31 +0000 UTC 从标准输入读取的内容在标准输出中重复 772 从标准输入流 ( stdin )读取数据时,它们会复制到标准输出流 ( stdout )。例子: $ echo "10" | r -e 'x<-readLines()' 10 $ echo "10" | r -e 'x<-scan(quiet=T)' 1: 10 2: 如何避免这种重复? r 1 个回答 Voted Best Answer aleksandr barakin 2020-05-10T21:15:31Z2020-05-10T21:15:31Z 虽然这两个函数的描述都说默认读取来自stdin,但您应该使用该函数显式打开stdinfile(),并将由它创建的对象传递给提到的读取函数(参数conforreadLines()和filefor scan())。 $ echo "10" | r -e 'x<-readLines(con=file("stdin"))' $ echo "10" | r -e 'x<-scan(file=file("stdin"),quiet=T)' $ 示例,辅以输入数据的实际处理和结果的输出: $ echo "10" | r -e 'x<-readLines(con=file("stdin")); cat(as.numeric(x[1])+1)' 11 $ echo "10" | r -e 'x<-scan(file=file("stdin"),quiet=T); cat(x[1]+1)' 11 ps 在函数的情况下scan(),您甚至可以file()通过立即将file值传递给参数来避免调用该函数stdin: $ echo "10" | r -e 'x<-scan(file="stdin",quiet=T); cat(x[1]+1)' 11
虽然这两个函数的描述都说默认读取来自stdin,但您应该使用该函数显式打开stdin
file(),并将由它创建的对象传递给提到的读取函数(参数conforreadLines()和fileforscan())。示例,辅以输入数据的实际处理和结果的输出:
ps 在函数的情况下
scan(),您甚至可以file()通过立即将file值传递给参数来避免调用该函数stdin: