在R中抑制system()输出

4
我想将invisible()函数的行为应用于我在脚本中得到的输出,该脚本如下:system("cmd.exe", input = command)。然而,即使使用了invisible,输出仍然显示在控制台上。有没有办法隐藏它?
编辑:我在命令中运行curl来下载网页,输出是预期的curl输出。
编辑2:可重复的示例。
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))

控制台输出:

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

输出显示什么?它是信息、警告、错误还是输出本身? - Melissa Key
@MelissaKey 这是使用curl命令下载页面时的输出结果。 - GonzaloXavier
1
这个描述太过模糊。“一个网页”?哪个网页?什么代码?“期望”的输出是什么? - IRTFM
添加了另一个带有示例的编辑。 - GonzaloXavier
@Dason实习生完成了它!谢谢!如果您将您的评论作为答案,我可以选择它作为已解决。 - GonzaloXavier
3个回答

5

在系统调用中使用 intern=TRUE。从 system 帮助页面中获取:

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

完成此操作后,您可以选择使用“invisible”选项或仅存储结果以防止打印。


3

将"ignore.stdout"或"ignore.stderr"属性(或两者都)设置为TRUE可能有效(可能只适用于Unix操作系统)。"ignore.stdout"抑制写入"stdout"的消息,而"ignore.stderr"抑制写入"stderr"的消息。

因此:

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

2
你可以使用 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

如果您将file设置为其他内容,它将输出到该文件而不是控制台。

这就是我想要避免的,因为我确实希望代码的某些部分输出到控制台。只是不包括那个部分。如果没有其他可能的解决方案,我将沉没并取消沉没代码的那一部分。 - GonzaloXavier
抑制R的输出真的这么困难吗? - masher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接