Back to Blog
Download emacs for mac tutorial5/18/2023 ![]() Outline mode for R (add-hook 'ess-mode-hook '( lambda () (outline-minor-mode) ( setq outline-regexp "^#.*-") ( defun outline-level () ( cond (looking-at "^#.*-") 1) ( t 1000) ) ( defun send-section-to-R () (interactive ()) ( let ((beg)) ( if (outline-on-heading-p) (beginning-of-line) (outline-previous-visible-heading 1)) ( setq beg (point)) (set-mark (point)) (outline-next-visible-heading 1) (previous-line 1) (end-of-line 1) (ess-eval-region-or-function-or-paragraph-and-step) ) ) (local-set-key (kbd "C-c h") 'outline-hide-body) (local-set-key (kbd "C-c s") 'outline-show-all) (local-set-key (kbd "C-c ") 'outline-hide-entry) (local-set-key (kbd "C-c ") 'outline-show-entry) (local-set-key (kbd "C-c ") 'outline-previous-heading) (local-set-key (kbd "C-c ") 'outline-next-heading) (local-set-key (kbd "C-c t") 'send-section-to-R) ) ) To add these functions to emacs, just add the following to your init.el file, and enjoy using emacs as your main R editor. Finally, I created key bindings similar to those from RStudio, so that C-c t evaluates a region. For this, I wrote the function (based on the link above) called ‘send-section-to-R’. The next thing was to select the region and send it to R for evaluation. This is easy to achieve in emacs with the outline-regexp variable. ![]() Basically, I wanted to be able to create regions using the #region name - structure. After looking everywhere, I found a solution based on the emacs outline mode here. When I started writing R code in emacs, I started to miss the region evaluation from Rstudio. C-c C-c ess-eval-region-or-function-or-paragraph-and-step.Although I tend to only use C- to evaluate line by line (in emacs with UI). See below the most common tasks you would use in emacs. ![]() Also, the experience is better if you memorize a couple of key bindings. Depending on the size of your emacs window, the new R buffer would be created on the right or bottom side. Once the setup is ready, you can move on to enjoy using emacs to write R code. ( require 'company) ( setq tab-always-indent 'complete) ( setq company-idle-delay 0.1) (global-company-mode) (ess-toggle-underscore nil) (with-eval-after-load 'ess ( setq ess-use-company t)) Running R in emacs: Because sometimes I use the terminal and sometimes the GUI, I created an alias in my profile file ( ~/.bashrc) to use both. Here, I will describe the basics and then move on to how to use ESS for R.įirst, there are two ways to use emacs: from the terminal or from the GUI. The absolute beginner’s guide to emacs is a good one, the guided tour of emacs is also to understand the basics. There are several tutorials online to learn how to use Emacs. For windows, you can download and install emacs from its main website. In MacOS, emacs comes pre-installed, but if you want a more recent version, you can install it with homebrew, with brew cask install emacs. For instance, in fedora, you can install it with dnf install emacs. For linux users, emacs should be in the main repositories. You can install emacs in different ways, depending on your operative system. GNU Emacs is a very old editor and it’s mostly designed to be used with the keyboard. Emacs basicsĮmacs is a free and open source text editor that is fully customizable. Because I want to use emacs as much as I can for coding, I decided to switch from RStudio to ESS in emacs. Rstudio is a great editor, but I prefer to write in emacs because I usually use the terminal and command line in my daily tasks. ![]() ![]() Nonetheless, until recently, I had been writing most of my R scripts in RStudio. The fact that I am very comfortable with emacs’ keybindings, has made me more efficient in emacs than in any other editor. I use it to write code mostly in C++, perl and python. I have been using emacs for more than five years, mainly for coding. ![]()
0 Comments
Read More
Leave a Reply. |