2

Is there something in R to open a text file? In Python, one could open a text file using notepad:

import subprocess as sp
programName = "notepad.exe"
fileName = "file.txt"
sp.Popen([programName, fileName])

Other methods are described in the post as well. Is there something similar in R?

Community
  • 1
  • 1
sedeh
  • 7,083
  • 6
  • 48
  • 65
  • 3
    You can use the equivalent code in R via `system` but neither solution is good: most systems won’t have “notepad.exe” installed (because most R systems aren’t Windows), and at any rate you should probably open the default editor, not an arbitrary one (however, I don’t know to achieve this portably in R, hence this is a comment, not an answer). – Konrad Rudolph Jun 26 '15 at 17:00

1 Answers1

3

Keeping in mind portability issues, i.e. this is specific to Windows, you can use shell.exec, which will open the file in whatever program is associated with that type of file by default - e.g.

## full path
shell.exec(paste0(c(getwd(), "/tmpfile.txt"),collapse = ""))
## relative to current working directory
shell.exec("tmpfile.txt")

both open tmpfile.txt in notepad on my machine.

nrussell
  • 18,382
  • 4
  • 47
  • 60
  • 1
    I could then combine this with `if(Sys.info()["sysname"] == "Windows") shell.exec("tmpfile.txt")`. Sweet! – sedeh Jun 26 '15 at 17:31