9

In python, one can get the path to the desktop on a Windows computer using:

os.sep.join((os.path.expanduser("~"), "Desktop"))

Is there something equivalent in R?

John Waller
  • 2,257
  • 4
  • 21
  • 26

3 Answers3

12

The ~ expands to your documents home, rather than your user profile where the desktop is normally located. I recommend you use Sys.getenv to find your user profile:

file.path(Sys.getenv("USERPROFILE"),"Desktop")
James
  • 65,548
  • 14
  • 155
  • 193
11

Something like (like mentioned in the comment) :

file.path(path.expand('~'),'Desktop')
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • It seems that Windows translates "~" to "/Users//Documents" and NOT "/Users/". – steveb Jan 13 '17 at 23:54
  • this worked for me: `file.path(dirname(path.expand('~')),'Desktop')`. James answer only works if your User folder is at the standard location – shosaco Nov 14 '20 at 11:47
2

Can't add a comment to James' answer, so here's another Answer.

On a terminal server network:

file.path(Sys.getenv("USERPROFILE"),"Desktop") gives me the wrong result

file.path(Sys.getenv("HOMESHARE"),"Desktop") gives me the right result

On my local machine it's the other way round. I haven't yet found a solution which works in both environments.

EDITED TO ADD

OK, this is a bit of a kludge based on the link I gave in my comment. A Windows expert could probably do it better, but I think this works in both environments and should be robust against changes in Desktop path.

Create a VBscript file to return the Desktop path: getDesk.vbs

dim WSHShell, desktop, pathstring, objFSO
set objFSO=CreateObject("Scripting.FileSystemObject")
Set WSHshell = CreateObject("WScript.Shell")
desktop = WSHShell.SpecialFolders("Desktop")
pathstring = objFSO.GetAbsolutePathName(desktop)
WScript.Echo pathstring

Now in R you can execute the VBscript to return the Desktop path

system("cscript //nologo getDesk.vbs", intern=TRUE)
user20637
  • 664
  • 3
  • 11
  • From this thread http://stackoverflow.com/questions/2000638/whats-the-environment-variable-for-the-path-to-the-desktop?rq=1 it looks as if any approach based simply on environment variables may not be very portable. – user20637 Sep 19 '14 at 09:44