Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-如何使用system()或system2()执行PowerShell cmds_R_Powershell_Cmd_Operating System - Fatal编程技术网

R-如何使用system()或system2()执行PowerShell cmds

R-如何使用system()或system2()执行PowerShell cmds,r,powershell,cmd,operating-system,R,Powershell,Cmd,Operating System,我在R中工作(在Windows操作系统上),试图计算文本文件中的字数,而不将文件加载到内存中。我们的想法是获取一些关于文件大小、行数、字数等的统计信息。调用R的system()函数使用find进行行数计算并不难: 文件count_words.txt的字数约为数百万字。我还在一个.txt文件中测试了它,该文件的字数要少得多 "There are seven words in this file." 但计数再次返回为127 print(system2("Measure-Object", args

我在R中工作(在Windows操作系统上),试图计算文本文件中的字数,而不将文件加载到内存中。我们的想法是获取一些关于文件大小、行数、字数等的统计信息。调用R的system()函数使用
find
进行行数计算并不难:

文件
count_words.txt
的字数约为数百万字。我还在一个.txt文件中测试了它,该文件的字数要少得多

"There are seven words in this file."
但计数再次返回为127

print(system2("Measure-Object", args = c('seven_words.txt', '-Word')))
[1] 127
system2()
是否识别PowerShell命令?使用
Measure Object
时调用函数的正确语法是什么?为什么不管实际字数多少,它都返回相同的值?

问题——概述 这里有两个问题:

  • 您没有告诉
    system2()
    使用powershell
  • 您没有使用正确的powershell语法
  • 解决方案 更多解释 从
    帮助(“系统2”)

    system2调用由命令指定的OS命令

    一个主要问题是
    Measure Object
    不是一个系统命令——它是一个PowerShell命令。PowerShell的系统命令是
    PowerShell
    ,这是您需要调用的命令

    此外,您还没有掌握正确的PowerShell语法。如果查看一下,您将看到真正需要的PowerShell命令是

    Get-Content C:/Users/User/Documents/count_words.txt | Measure-Object -Word
    
    (查看链接文档中的示例三)

    print(system2("Measure-Object", args = c('seven_words.txt', '-Word')))
    [1] 127
    
    command <- "Get-Content C:/Users/User/Documents/test1.txt | Measure-Object -Word"
    system2("powershell", args = command)
    
    command <- "Get-Content C:/Users/User/Documents/test1.txt | Measure-Object -Word"
    system2("powershell", args = command)
    
    Lines                             Words Characters          Property           
    -----                             ----- ----------          --------           
                                          7                                        
    
    
    command <- "Get-Content C:/Users/User/Documents/test2.txt | Measure-Object -Word"
    system2("powershell", args = command)
    
    Lines                             Words Characters          Property           
    -----                             ----- ----------          --------           
                                          8                                        
    
    Get-Content C:/Users/User/Documents/count_words.txt | Measure-Object -Word