0

I have the results file saved in .txt format. I wish to open that text file in Word document directly from the R console. If I copy paste the following command (winword "C:\Users\results.txt") in windows command prompt I was able to open the text file in the word document. However, I want to open this directly from the R console.

x = paste0('"','C:\\Users\\results.txt', '"')

system(x)

shell.exec(x)

shell(x)

Open the text using Word directly from R console.

  • What have you tried? Have you tried running that command with `system`? – divibisan Mar 29 '19 at 16:17
  • I tried shell.exec(winword "C:\Users\results.txt") . And I created a character x (address of the .txt file) and used the command shell.exec(x) – Sesha Meka Mar 29 '19 at 16:23
  • Maybe `x <- paste('winword', '"C:/Users/results.txt"')`. – Rui Barradas Mar 29 '19 at 16:34
  • 1
    You open a txt document in Word or any other editor. You don't open a text file "in Word document". I suspect the actual question is how to control Word from R? You'd have to use COM for this. `paste0` is irrelevant. The only reason pasting the file's path, or `shell.exec` works is because in **your** computer, the application associated with the `txt` extension is Word. On my machine the same code would start Sublime Text. – Panagiotis Kanavos Mar 29 '19 at 16:35
  • @Rui Barradas. I get the following error: Error in shell.exec(x) : 'winword "C:/Users/results.txt"' not found – Sesha Meka Mar 29 '19 at 16:53
  • Try without the double quotes around the filename. – Rui Barradas Mar 29 '19 at 16:57
  • @divibisanI tried running with system but it didn't work. – Sesha Meka Mar 29 '19 at 16:58
  • @Rui Barradas I tried without the double quotes but it isn't working. – Sesha Meka Mar 29 '19 at 17:05

1 Answers1

0

You can read text in the R console (similar to a document) with the readtext package

library(readtext)

my.text = readtext("file_path/text_file.txt")
my.text$text
NM_
  • 1,887
  • 3
  • 12
  • 27
  • The asker doesn't want to read text, they want to open a file in Microsoft Word from inside R (for some reason) – divibisan Mar 29 '19 at 16:54
  • Thank you for the clarification. I misunderstood. – NM_ Mar 29 '19 at 17:10
  • Actually, I wanted to save the output directly in word document. I wasn't able to do that. So I first saved the output from console in .txt file using the sink command and then from the command prompt i used the command winword "C:\users\results.txt" to open in word editor. – Sesha Meka Mar 29 '19 at 17:20