开发者

How can I load my .RProfile using Textmate's R Bundle

开发者 https://www.devze.com 2022-12-20 09:31 出处:网络
This question is for those of you who happen to use R, on a Mac, in combination with Macromate\'s [Textmate](http://macromates.com/) text editor and the \"R\" Bundle. All of which are nifty, needless

This question is for those of you who happen to use R, on a Mac, in combination with Macromate's [Textmate](http://macromates.com/) text editor and the "R" Bundle. All of which are nifty, needless to say, but that's beside the point for now :-)

I've got a .RProfile file sitting in my default "~" startup directory, and it's got a number of useful functions in it I like to have access to when writing R scripts. But I also use Textmate for most of my writing, and the cmd-R functionality to to run my scripts within Textmate.

At the moment, I don't know how to tell Textmate where my .Rprofile is.

Is there a way--most likely through Textmate's Bundle settings--that I can point Textmate to my .RProfile so I don't have to write my functions into every script on a per-script basis?

OR

Is it actually better to include any custom functions in any script I write, so that anyone with a basic R setup 开发者_开发知识库can source and run my scripts?

I feel like I must be missing a dead-easy setting or config file here within either Textmate or the R environment it calls to run my scripts.

Thanks so much in advance!


The R Bundle Developer is apparently working on this (see this Post on the Mailing List) but it's not available at the moment.

In the meantime, you have a couple of choices.

First, you can create a new bundle (e..g, "briandk-R") then create a snippet w/in that bundle either with 'source($1)' or just hardcode the file you want to source instead of the placeholder (so, e.g., "source("~/some_file_to_source.R"). If you do the latter, then you can configure TM to source your file via a tab trigger (in the Bundle Editor, toggle over to 'settings' (upper left hand corner) and type "source.r, source.rd.console" in the 'Scope Selector' field then choose a few letters for your tab trigger (e.g., "src.")

If you don't want to do that, go to the 'Rdaemon' Directory (which is either in your home directory or in ~/Library/Application Support/Rdaemon). Look in this directory and you will see another directory called "daemon.' In there is a file called "start.r" which lists the files that are sourced upon starting R from the Rdaemon. You know what to do from there. (Note: This directory also contains a couple of other scripts which contain initial settings; you might wish to have a look at those as well)


The first part of Doug's response offers the simplest immediate solution... add

source('/Users/briandk/.Rprofile')

to the head of any .r files you want those functions in... with that one line of code, you get your utility functions. Of course, that only helps if you're running the whole TM file.

Ideally, the bundle will be updated... perhaps to support a shell variable via TM's preferences???

TM_RPROFILE 

which could be set to the path to your .Rprofile file.

I just hacked this into tmR.rb with just 2 lines of code. To implement this, go to ~/Library/Application Support/TextMate/Pristine Copy/Bundles/ and Show the Contents of R.tmbundle

In there, you'll find support/tmR.rb

in my version, near line 112, you should change

tmpDir = File.join(ENV['TMP'] || "/tmp", "TM_R")
recursive_delete(tmpDir) if File.exists?(tmpDir) # remove the temp dir if it's already there
Dir::mkdir(tmpDir)

# Mechanism for dynamic reading
# stdin, stdout, stderr = popen3("R", "--vanilla", "--no-readline", "--slave", "--encoding=UTF-8")
stdin, stdout, stderr, pid = my_popen3("R --vanilla --slave --encoding=UTF-8 2>&1")
# init the R slave
stdin.puts(%{options(device="pdf")})
stdin.puts(%{options(repos="#{cran}")})

to

tmpDir = File.join(ENV['TMP'] || "/tmp", "TM_R")
recursive_delete(tmpDir) if File.exists?(tmpDir) # remove the temp dir if it's already there
Dir::mkdir(tmpDir)

rprofile = (ENV['TM_RPROFILE'] == nil) ? "" : "source('" + ENV['TM_RPROFILE'] + "')"

# Mechanism for dynamic reading
# stdin, stdout, stderr = popen3("R", "--vanilla", "--no-readline", "--slave", "--encoding=UTF-8")
stdin, stdout, stderr, pid = my_popen3("R --vanilla --slave --encoding=UTF-8 2>&1")
# init the R slave
stdin.puts("#{rprofile}")
stdin.puts(%{options(device="pdf")})
stdin.puts(%{options(repos="#{cran}")})

Just added 2 lines there... the one that begins "rprofile =" and the one that includes "#{rprofile}"

-Wil

0

精彩评论

暂无评论...
验证码 换一张
取 消