PythonCook R程序员

用Sublime Text 3运行R与Python脚本

2017-10-21  本文已影响327人  王诗翔

Sublime Text是广为流行的轻量级编辑器。当我们想要聚焦于编程而非调试时,使用轻量级编辑器无疑是一种享受。关于Sublime Text3特性的介绍,请参考其官方(http://www.sublimetext.com/)。

本文简单介绍Sublime Text3一些插件的安装和Python,R快捷键的定制。

安装Sublime Text3直接上前面的官网地址下载相应的版本进行安装,然后百度注册码输入激活即可。

插件安装请参考实用的sublime插件集合 – sublime推荐必备插件为 Sublime Text 3 设置 Python 的全栈开发环境两篇博文。因为这些插件并非全都需要,请按照自己的需要进行安装,这里我推荐一些必要的插件:

Emmet

Anaconda

ColorSublime (这里选择自己喜欢的主题安装)

SideBarEnhancements

Djaneiro

SublimeLinter

SublimeREPL (这个必须安装,不然就不能设置运行脚本的快捷键了)

在设置Python与R的脚本运行快捷键之前需要安装好它们。你可以自己去相应的网址安装下载,我是用的Anaconda,然后在里面安装Rstudio,这样R和Python都有了。Sublime Text会自动寻找程序的路径,所以两种方式都应该没问题。(第一种没验证)

参考Sublime Text3配置SublimeREPL快捷键的方法(Python)设置快捷键:

点击菜单栏Preferences-->Key Bindings User,在里面输入


[
        {
            "keys": ["f5"],//这是自己设运行python的快捷键
            "command": "run_existing_window_command", 
            "args":
            {
                "id": "repl_python_run",
                "file": "config/Python/Main.sublime-menu"
            }
    },

        {
            "keys": ["ctrl+alt+r"],//这是自己设的运行R脚本快捷键
            "command": "run_existing_window_command", 
            "args":
            {
                "id": "repl_r_run",
                "file": "config/R/Main.sublime-menu"
            }
    }
]

这里需要注意,我写这篇文章时(2017-10-21),R的设置文件中还没有叫“repl_r_run”的“id”。所以我参考python的设置进行理解和自定义。

按照以下操作打开R设置文件:Preferences-->Browse Packages-->SublimeREPL文件夹-->config文件夹-->R文件夹-->Main.sublime-menu(以文本格式打开)

将里面的内容改成

[
     {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "R",
            "id": "SublimeREPL",
            "children":
            [
                {"command": "repl_open",
                 "caption": "R",
                 "id": "repl_r",
                 "mnemonic": "R",
                 "args": {
                    "type": "subprocess",
                    "external_id": "r",
                    "additional_scopes": ["tex.latex.knitr"],
                    "encoding": {
                        "windows": "$win_cmd_encoding",
                        "linux": "utf8",
                        "osx": "utf8"
                        },
                    "soft_quit": "\nquit(save=\"no\")\n",
                    "cmd": {"linux": ["R", "--interactive", "--no-readline"],
                            "osx": ["R", "--interactive", "--no-readline"],
                            "windows": ["Rterm.exe", "--ess", "--encoding=$win_cmd_encoding"]},
                    "cwd": "$file_path",
                    "extend_env": {"osx": {"PATH": "{PATH}:/usr/local/bin"},
                                   "linux": {"PATH": "{PATH}:/usr/local/bin"},
                                   "windows": {}},
                    "cmd_postfix": "\n",
                    "suppress_echo": {"osx": true,
                                      "linux": true,
                                      "windows": false},
                    "syntax": "Packages/R/R Console.tmLanguage"
                    }
                },

               {"command": "repl_open",
                   "caption": "R - RUN current file",
                     "id": "repl_r_run",
                     "mnemonic": "R",
                     "args": {
                        "type": "subprocess",
                        "encoding": {
                        "windows": "$win_cmd_encoding",
                        "linux": "utf8",
                        "osx": "utf8"
                        },
                        "cmd": ["Rscript","--vanilla",  "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/R/R Console.tmLanguage",
                        "external_id": "R",
                        "extend_env": {"osx": {"PATH": "{PATH}:/usr/local/bin"},
                                   "linux": {"PATH": "{PATH}:/usr/local/bin"},
                                   "windows": {}}
                        }
                    }
            ]
        }]
    }
]

这里主要对内容插入了一个子项:

 {"command": "repl_open",
  "caption": "R - RUN current file",
  "id": "repl_r_run",
  "mnemonic": "R",
  "args": {
                        "type": "subprocess",
                        "encoding": {
                        "windows": "$win_cmd_encoding",
                        "linux": "utf8",
                        "osx": "utf8"
                        },
                        "cmd": ["Rscript","--vanilla",  "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/R/R Console.tmLanguage",
                        "external_id": "R",
                        "extend_env": {"osx": {"PATH": "{PATH}:/usr/local/bin"},
                                   "linux": {"PATH": "{PATH}:/usr/local/bin"},
                                   "windows": {}}
            }
    }

如果你想让文件设置更规范,可以修改该文档同一目录下文件Default.sublime-commands,把它的内容改为:

[
    {
        "caption": "SublimeREPL: R",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_r",
            "file": "config/R/Main.sublime-menu"
        }
    },

        {
        "caption": "SublimeREPL: R - RUN current file",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_r_run",
            "file": "config/R/Main.sublime-menu"
        }
    }
]

最后验证一下是否成功,我随便写了一个简单的用R和python都能执行的脚本,内容为

a = 3

b = 4
a - b


print(a)

先按f5测试Python

测试python

ctrl+alt+r测试R

测试R
上一篇 下一篇

猜你喜欢

热点阅读