Home Download Buy Blog Forum Support

Help with sending code from ST2 to R

Help with sending code from ST2 to R

Postby maiasaura on Wed Feb 22, 2012 9:18 pm

I use Sublime Text primarily for R code. I found a version of send selection to R in the package forum and have been using it. It sort of works, but it handles single quotes and double quotes poorly and inconsistently. That is, it works some of the time but not all of the time. Since the R syntax requires use of quotes (I can't lose those entirely), I need to address this issue.

I am using this attached code (sits in a file called Send-Selection.py in my ~/Library/Application Support/Sublime Text 2/Packages/User folder. I run ST2 on OSX 10.7.3

Code: Select all
import sublime, sublime_plugin
import os, subprocess, string

class SendSelectionCommand(sublime_plugin.TextCommand):
   def cleanString(str):
      return str

   def run(self, edit):
      # get selection
      selection = ""
      for region in self.view.sel():
         selection+=self.view.substr(region) + "\n"
      selection = (selection[::-1].replace('\n'[::-1], '', 1))[::-1]

      # only proceed if selection is not empty
         extension = os.path.splitext(self.view.file_name())[1]

         # R file
            # define list of arguments
            args=['osascript','-e','tell app "R64" to activate']
            # split selection into lines
            # add code lines to list of arguments
            for part in selection:
               args.extend(['-e', 'tell app "R64" to cmd "' + part + '"\n'])
            # execute code and activate ST2
            p = subprocess.Popen(args)
            subprocess.Popen("""osascript -e 'tell app "Sublime Text 2" to activate' """, shell=True)

How it fails:

If I select this line
Code: Select all
from a file called test.r (and ST2 recognizes that it is R), and press ⌘ + Enter, it does send the code to R, but escapes the ' so I get this error in my R interactive console:

Code: Select all
Error in source(../initialize.r) : object '..' not found

How do I overcome this? Thanks for any advice and I am happy to clarify if anything I've said is unclear.
Posts: 15
Joined: Mon Dec 19, 2011 2:31 am

Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 38 guests