You are not logged in.

#1 2016-01-08 12:36:16

woodape
Member
Registered: 2015-03-25
Posts: 159

How do Nemo actions handle current working directories?

Hi everyone, I'm having difficulty with a script / Nemo action combination I'm trying to use to load an R dataframe into R Studio (rstudio-bin from the AUR). I need to make a file called ".Rprofile" in the working directory of the dataframe to  get around the fact the r-studio-bin doesn't allow command line arguments. R Studio always sources the ".Rprofile" from the current working directory if it exists, so the script is as simple as making the file with the load command and then launching rstudio-bin.

The issue is that when using the Nemo action, despite the claims made by the output of my script of being in the correct working directory, rstudio-bin launches with the working directory set as my home folder - thus not sourcing my ".Rprofile". If I launch the script from the command line with the path to the dataframe as the argument, everything works as expected.

The strange part is if I open a terminal, issue "killall nemo && nemo", and then try the command again it'll work. This issue only occurs when Nemo is launched from my Cinnamon menu or using the launcher with Alt-F2.

Any ideas as to why this would happen or where I should look for more information?

I'm using
kernel 4.3.3-2
bash shell
Cinnamon 2.8.6
Nemo 2.8.6
R 3.2.3
R Studio 0.99.491
python 3.5.1

Here's the script, called "pyr" which resides in the Nemo actions folder:

#!/usr/bin/env python

import os
import sys
from subprocess import call

data = sys.argv[1]

path,name = os.path.split(data)

os.chdir(path)

wd = os.getcwd()

log=open("logfile","w+t")
log.write(wd+"\n")
print(wd)

rprof = open(".Rprofile","w+t")
rprof.write('\nload("'+data+'")\n')
rprof.close()

call(["rstudio-bin"])

os.remove(".Rprofile")

wd = os.getcwd()
log.write(wd+"\n")
print(wd)
log.close()

exit(0)

And here is the Nemo action:

[Nemo Action]
Active=true
Name=Use in R Studio
Comment=Open an R session with the selected object loaded

Exec=<pyr %F>

Icon-Name=rstudio
Selection=notnone
Extensions=Rda;

Quote=double

Offline

#2 2016-01-08 12:55:18

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: How do Nemo actions handle current working directories?

Apparently rstudio does not read .Rprofile from the current working directory, only the "project directory" then the user's home directory:
https://support.rstudio.com/hc/en-us/co … ot-loading

If you don't use your ~/.Rprofile for anything else, you could just use that file instead, and most of your python code would remain the same.

If you do use ~/.Rprofile for other things, you'd have to do a little more to add/remove and/or replace the data importing command, but it'd still be pretty straitforward.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2016-01-08 14:37:15

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: How do Nemo actions handle current working directories?

Thanks for the reply Trilby and the link. I've thought about changing the code to backup the home ".Rprofile", append the load command, run rstudio-bin, then restore the backup afterwards, and I may end up going that route if I can't get this resolved, but I'd rather not if I can help it.

Looking at the link you sent, I think I read it a different way. It looks like the OP was asking why a ".Rprofile" was not loaded from a directory she changed in to. Keven I think is using project directory to mean the directory from which R was started. The official R documentation for "Startup" says [bold is mine]:

...
Then, unless --no-init-file was given, R searches for a user profile, a file of R code. The path of this file can be specified by the R_PROFILE_USER environment variable (and tilde expansion will be performed). If this is unset, a file called ‘.Rprofile’ is searched for in the current directory or in the user's home directory (in that order). The user profile file is sourced into the workspace.

Also, on their support site they discuss the behavior of opening R Studio from the terminal and it is just as any normal program should execute:
https://support.rstudio.com/hc/en-us/ar … Workspaces

It seems like if I have a ".Rprofile" file in the directory from which R was launched it should be sourced, which is what my script sets up. My pyr script works like I expect it to when I run it from the command line, which makes me think it has something to do with Nemo, not R.

Offline

#4 2016-01-08 15:13:40

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: How do Nemo actions handle current working directories?

Sorry, I know R quite well, but never use Rstudio.

But I still read it the same way.  The link you provided further specifies that you can provide a project directory as a command line parameter to rstudio.  Have you tried this?  Pass the directory of your new .Rprofile to rstudio as the first argument.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2016-01-08 15:55:00

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: How do Nemo actions handle current working directories?

I saw that little bit on the page as well about the command line arguments, but it doesn't seem to work for the version out of the AUR. For instance, from the terminal:

echo "$PWD"
/home/woodape/
rstudio-bin /home/woodape/music

and then from in Rstudio:

> getwd()
[1] "/home/woodape"

When I replace the call in my pyr script wih

call(["rstudio-bin",path])

to pass the path of the data frame as the first argument, it still doesn't souce the ".Rprofile" from the "path" directory. If, however, I run rstudio-desktop from the terminal with a ".Rprofile" in the current working directory it is sourced as I expect it should be.

I'm confused by this distinction between project directory and working directory. It seems that if I don't load a project from within Rstudio they should be the same. I don't know why the default behavior of R would be overridden. I still am leaning towards this being an issue with Nemo and not rstudio though.

Offline

#6 2016-01-08 17:10:15

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: How do Nemo actions handle current working directories?

woodape wrote:

I still am leaning towards this being an issue with Nemo and not rstudio though.

There is an easy test to determine this for sure: don't run rstudio from the command line, run your rpy script from the command line.  Does that work?

EDIT: oops! Sorry you did clearly say you did this already.  My mind was partially elsewhere.  But you can be confident then that this is a Nemo issue, not just lean that way.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2016-01-08 18:13:33

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: How do Nemo actions handle current working directories?

Yes, as I said in my earlier posts, the original script runs fine from the command line everytime, just not from Nemo if i launch Nemo from my app menu or a launcher. If I run Nemo from the command line, everything runs as expected. Its this weird behavior that's a problem because I like using the menu to run Nemo

Offline

#8 2016-01-08 18:43:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: How do Nemo actions handle current working directories?

Sorry, I edited my above post, you do have all that info here.

Can you change the nemo action command from "pyr %F" to "bash -l -c pyr %F".  If that works then it means it's a matter of finding what environment variable or shell config option is required for it to work - or this might be a suitable work around in itself if it works.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2016-01-08 19:30:33

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: How do Nemo actions handle current working directories?

No prob, happens to the best of us smile

I substituted that command in, but quoting the "pyr %F" part to get it to do anything. Still no dice.

I can see the logic here though about passing the command through the shell. If I open Nemo in the terminal, the action works - even without the "bash -l -c" part in the "Exec=" line. But unless this is done it doesn't work.

Offline

#10 2016-01-09 08:10:51

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: How do Nemo actions handle current working directories?

Some more testing. If I change the "call" line in pyr to:

call(["terminator","-e","rstudio-bin"])

The Nemo action works correctly in all cases, though now I have a terminator window hanging around, which is not optimal. Its strange to me that this would work and

call(["bash","-c","-l","rstudio-bin"])

does not.

Offline

#11 2016-01-09 10:58:19

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: How do Nemo actions handle current working directories?

I'm a bit frustrated with this not working, and given that spawning terminals with the same code puts them in the right directory I'm now split between thinking its a Rstudio problem and a Nemo problem. Its certainly not an R problem as

call(["terminator","-e","R"])

works as expected.

For now I'm going with the workaround suggested by Trilby of just adding the "load" command to the ".Rprofile" in the home directory. Here's my updated pyr:

#!/usr/bin/env python
import os, sys, tempfile
from subprocess import call

data = sys.argv[1]
path,name = os.path.split(data)
os.chdir(path)

hrprof = os.path.join( os.path.expanduser("~") , ".Rprofile"  )

try:
    rprof = open(hrprof,"r")
    tmprp = tempfile.NamedTemporaryFile(mode='w+t',delete=False,suffix='.Rprofile') 
    for line in rprof:
        tmprp.write(line)
    tmprp.close()
    rprof.close()
except FileNotFoundError:
    rprof = open(hrprof,"w+t")
    rprof.write('\n')
    rprof.close()

rprof = open(hrprof,"a")
rprof.write('setwd("'+path+'")\n')
rprof.write('load("'+data+'")\n')
rprof.close()
call(["rstudio-bin"])
os.remove(hrprof)

try:
    oldrp = open(tmprp.name,"r")
    rprof = open(hrprof,"w+t")
    for line in oldrp:
        rprof.write(line)
    oldrp.close()
    rprof.close()
except NameError:
    pass

I would still like to have my original plan of using a local ".Rprofile" to load in the dataframe if anyone has any suggestions about how to resolve the issue.

Offline

Board footer

Powered by FluxBB