6

In R I would like to store a console command to a variable. I have already tried with the solutions proposed in the following link but without luck: In R, is it possible to redirect console output to a variable? Here you are the command I'm using:

test <- capture.output(system("pa11y scuolafalconeborsellino.it; 
        perl -e \"print unpack('c', pack('C', $?)), \\$/\""), file = NULL)

The output visiblein the console is:

[4m[36m Welcome to Pa11y[39m[24m [90mWe'll sniff your page for you now. [39m [36m > [39mLoading page... [36m > [39mRunning HTML CodeSniffer... [36m > [39m[31mError: HTML CodeSniffer error[39m

-1

but the variable test is empty.

Thank you!

Community
  • 1
  • 1
Gianluca78
  • 794
  • 1
  • 9
  • 24

1 Answers1

7

system has a parameter intern which can be used to save the output to a character vector:

test <- system("pa11y scuolafalconeborsellino.it; perl -e \"print unpack('c', pack('C', $?)), \\$/\"", 
               intern = TRUE)

Note that system2 is now prefered and system should be avoided in new code.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Could you provide a link regarding your last sentence? I am curious. –  May 11 '15 at 09:22
  • 1
    @Pascal See `help("system")`. – Roland May 11 '15 at 09:35
  • Doesn't seem to work with `unix2dos` on Linux. `system("unix2dos ...", intern = TRUE)` returns `character(0)` and the output is still printed to the console. – fdetsch Sep 30 '15 at 07:10