简体   繁体   English

抑制 R 中的 system() 输出

[英]Suppress system() output in R

I wanna apply the invisible() function behavior to an output I got in my script that goes like this system("cmd.exe", input = command) .我想将invisible()函数行为应用于我在脚本中得到的输出,就像这个system("cmd.exe", input = command) However, even when using invisible, the output still shows in console.但是,即使使用 invisible,输出仍然显示在控制台中。 Is there any way to hide it?有什么办法可以隐藏吗?

EDIT: I'm running curl in the command to download a webpage and the output is the expected curl output.编辑:我在命令中运行curl来下载网页,输出是预期的 curl 输出。

EDIT2: Reproducible example EDIT2:可重现的例子

url <- "www.google.com"
command <- paste0('curl "', 
                  url,
                  '"',
                  ' -H "Pragma: no-cache" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: en-US,en;q=0.9" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" -H "Cache-Control: no-cache" -H "Cookie: uupid69991=eb4c8ec5-1f94-4cf5-957d-e477a778f79c; uupid99993=eb4c8ec5-1f94-4cf5-957d-e477a778f79c; uupid89991=1; uupid79991=Fee_Based_Role; locale=en; uupid99991=1903 x 1012; IVZSESSIONID=CEHrDvYqpDuQ-zwy8YZpWwm1RWZHY3DGq4V7elBfxcH87XFFo-J_^! -175310928; _ga=GA1.2.1974800928.1524362050; _gid=GA1.2.1544243785.1524362050" -H "Connection: keep-alive" --compressed',
                  ' > google.html')

invisible(system("cmd.exe", input = command))

Console Output:控制台输出:

Microsoft Windows [Version 10.0.16299.371]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\gonza\OneDrive\Documents\etfcmfa>curl "www.google.com" -H "Pragma: no-cache" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: en-US,en;q=0.9" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" -H "Cache-Control: no-cache" -H "Cookie: uupid69991=eb4c8ec5-1f94-4cf5-957d-e477a778f79c; uupid99993=eb4c8ec5-1f94-4cf5-957d-e477a778f79c; uupid89991=1; uupid79991=Fee_Based_Role; locale=en; uupid99991=1903 x 1012; IVZSESSIONID=CEHrDvYqpDuQ-zwy8YZpWwm1RWZHY3DGq4V7elBfxcH87XFFo-J_^! -175310928; _ga=GA1.2.1974800928.1524362050; _gid=GA1.2.1544243785.1524362050" -H "Connection: keep-alive" --compressed > google.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   231  100   231    0     0    982      0 --:--:-- --:--:-- --:--:--   982

Use intern=TRUE in your system call.在系统调用中使用intern=TRUE From the system help page:system帮助页面:

intern  
a logical (not NA) which indicates whether to capture the output of the command as an R character vector.

Once you do that you can either use invisible or just store the results to keep them from being printed.完成此操作后,您可以使用 invisible 或仅存储结果以防止它们被打印。

You can use the sink function to divert output in R.您可以使用sink函数来转移 R 中的输出。

> curl::curl('https://stackoverflow.com/')
A connection with                                        
description "https://stackoverflow.com/"
class       "curl"                      
mode        "r"                         
text        "text"                      
opened      "closed"                    
can read    "yes"                       
can write   "no"                        
> sink(file="nul") # set file = '/dev/null' if using Unix-based OS
> curl::curl('https://stackoverflow.com/') # no output

If you set file to something else, it will print output to that file instead of the console.如果您将file设置为其他内容,它会将输出打印到该文件而不是控制台。

Setting the "ignore.stdout" or "ignore.stderr" attributes (or both) to TRUE might work (may be specific to Unix operating systems).将“ignore.stdout”或“ignore.stderr”属性(或两者)设置为 TRUE 可能会起作用(可能特定于 Unix 操作系统)。 "ignore.stdout" suppresses messages written to "stdout" and "ignore.stderr" suppresses messages written to "stderr". “ignore.stdout”禁止写入“stdout”的消息,“ignore.stderr”禁止写入“stderr”的消息。

So:所以:

system(some_command, ignore.stdout = TRUE, ignore.stderr = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM