0

I am using the rstudioapi package to run a command in the Rstudio terminal, like so:

myTerm <- rstudioapi::terminalCreate()
rstudioapi::terminalSend(myTerm, "echo Hello\n")

This gives me the following in the Rstudio terminal:

user@devicenum:~$ echo Hello
Hello

I would like to have the output available as an object in my Rstudio environment. Is there a simple way to do this? Thank you!

Edit: I can apparently do this by using | clip.exe:

myTerm <- rstudioapi::terminalCreate()
rstudioapi::terminalSend(myTerm, "echo Hello |clip.exe\n")
x <- readClipboard()
x

I can also use |& tee to save output and stderr to a file.

It's slower than system() or system2() but at least it works.

Hlachma1
  • 25
  • 3
  • 1
    Do you absolutely need it be to a rstudio terminal? Or would using `system` be an acceptable alternative? – Dason Oct 27 '21 at 20:36
  • Also I don't use the Rstudio terminal... well ever but as far as I can tell the rstudioapi function terminalSend doesn't actually even execute the text that is sent so it's not entirely clear to me what you would want to be stored here. Providing more details about what you're actually trying to do and hoping to get as output would be appreciated. – Dason Oct 27 '21 at 20:44
  • The R script creates a dataset and writes it to an output file. I am trying to run a program present on my Ubuntu partition on this file. I've tried `system` and `system2` but they don't seem to be working. Ideally I'd have C:\Users\User> Ubuntu, then user@devicenum:~$ cd scriptfolder, then user@devicenum:~$ ./script -parameters -etc, and I'd get the terminal output as an object in Rstudio. – Hlachma1 Oct 27 '21 at 21:41
  • I guess my recommendation would be to elaborate on what you mean when you say that system doesn't work? How exactly doesn't it work? What makes you think rstudioapi::terminalSend will be better? In short we can't help if you don't tell us what's wrong. Debugging the initial issue probably would be best. – Dason Oct 27 '21 at 22:30
  • I have a script on my Ubuntu partition. The input to this script is a .txt created by my R studio script, and the output is a line in the terminal. Using system2 to cd into the directory results in a "not found" error. – Hlachma1 Oct 28 '21 at 19:56
  • Cool. Let's debug that instead of trying to do this tangential thing. I would highly suggest creating a new question where you show your code (or a minimal reproducible example) and the error you're getting with system/system2. It's better to try to fix the initial problem than try to fix a problem that came up from a "fix" to the original problem. Google the XY problem. – Dason Oct 28 '21 at 21:43

0 Answers0