How to switch to ESS mode manually? - r

I sometimes use emacs with the "It's all text" firefox extension to write in textareas, and
sometimes I need to insert R code into the file, which is usually named something like
"stackoverflow.com.231x234.txt", which is of course triggers the text mode. How can I switch
to ESS mode manually in this case? I know I can do "M-x python-mode" for python,
but I don't what the equivalent is for ESS.

M-x R-mode should do the trick.

Related

Print with syntax color in R-Studio

In R I always like to print out the script since it gives a good overview and one can adjust eventual errors. I like the syntax highlighting in R-Studio because it facilitates reading and fast comprehension of code.
Is there a way to print out the text with the highlighting I see in the editor?
Its not an R-Studio solution, but notepad++ will print R source with syntax highlighting.
RStudio will not print in colour, but it's easy to save the code as a PDF; in this case the syntax format is preserved. My favourite package is knitr.
library(knitr)
stitch("file_name.R")
The default output is PDF/Markup in .tex. If you prefer not to typeset, running the below will export as .html
stitch(script="file_name.R", system.file("misc", "knitr-template.Rhtml", package="knitr"))
Brief explanation
The reason this is an answer to this question in because of the last line of the question:
Is there a way to print out the text with the highlighting I see in
the editor?
so we are not limited to only and only using Rstudio software here.
After exploring the awesome answer by #rrg and realizing that it runs the code line by line, I wrote a comment below his answer and continued googling. My problem is that the code I wrote is so large and so time consuming to run that running it for the sake of having a syntax highlighted version is not feasible.
Most of the solution out there online involves having notepad++ which is a Windows application and I'm a dedicated Linux user, so I searched for a way I can do this in Linux (and possibly Mac)
The way I solved it:
Inspired by a blog post, I used the famous and beloved Vim to convert R to syntax highlighted HTML and then because you can open HTML in your browser, you can what ever you want with it (print, screenshot, etc.)
Activate synax highlighting in Vim:
open terminal
then open the vim config file by typing vim ~/.vimrc
press i from keyboard to go to "insert mode"
go to the end of the file using arrow keys on your keyboard
type syntax on at the end of the file
now you need to save and exit. For this you need to press Esc button from keyboard to come out of "insert mode" and then type :x and press Enter to save and close the file.
if you want to change the color scheme of the syntax highlighting, visit the bottom part of this website
From terminal open your file with Vim:
vim YOUR_FILE_PATH
Having you R code open in vim, you can turn on the line numbers if you like by pressing Esc and then write :set number and press Enter.
For converting R to HTML, press Esc to make sure you are not in "insert mode" and then type :TOhtml and press Enter. This will result is having a split window in terminal, half is your R code and the other half id your new HTML code.
For saving the files, type :x along with Enter button from keyboard twice to save both files (your R file will be unchanged if you have not typed anything extra in it and your HTML file will be created with the same name near your R code)
Now open it with your favorite browser (in my case Vivaldi) and do what ever you want (in my case converting the whole HTML into PNG)
Best way:
download https://github.com/jaredpetersen/codeprinter and paste in the r code. then choose syntax highlighting Xcode
For those using a Mac (and thus without access to Notepad++) cutting and pasting into Xcode and printing from there will also work.
As with Ron Jensen's earlier comment, this isn't an R Studio solution, but in the interests of "just getting it to work", I hope this helps someone.

Changing Cntrl + R shortcut for Running scripts in R Windows GUI

Is is possible to change the "Control + R" shortcut for sending scripts from the R text editor in the Windows GUI to the R console? I'd like to change it to "Control + Enter" to be more like the shortcut on my Mac. I do all my normal work on a Mac but have to use R on a PC to interface with some PC-only computational software.
Additional tidbits:
I'd rather not run an IDE on the PC if I don't have to, though perhaps this is the solution.
I use Rstudio on my Mac, but Rstudio does not get along with the PC software I'm running
The short answer is:
"No, there are no [built-in] ways to alter the menu shortcuts in the R Console"
I'm however gathering here -community wiki style- some of suggestions posted as remarks to this questions.
One approach may be to download the R source, hack it (see circa line 625 of src/gnuwin32/editor.c: ), and build the R binary anew (see the R for Windows FAQ for the tools you need to build from source). This seems to be a rather radical approach for the mere convenience of using an alternate keystroke sequence...
A similar approach may be to create an automatic patcher program which would patch the R executable, by locating the byte patterns surrounding the compiled logic of editor.c mentioned above and replacing it with a byte sequence for the desired keystroke. This solution may be sensitive to changes in the binaries, but also avoids the build process altogether...
An easier way to achieve this is probably by using an external text editor. Most modern editors have macros or configs that can be used, for example, to execute a source command in R for the selected text.
Customizing keyboard shortcuts is made available in Rstudio 0.99.644.
See https://support.rstudio.com/hc/en-us/articles/206382178-Customizing-Keyboard-Shortcuts for more information.

ESS and RScript : Executing in style of `compile-dwim-run'

I just got ESS set up in EMACS (I'm a relative newbie in this area). I have figured out how to take an R script and fire up an interactive R shell and evaluate a whole buffer ("C-c C-b"). But I'd also like to have the ability to submit an R script via RScript in the way that you can with Perl or Python with `compile-dwim-run', which I have bound to "C-c r", and have the whole output returned to me in a separate buffer without keeping open an interactive R shell.
I can't seem to find a default way to do this, and I'd like to leverage whatever ESS has to work that (I assume there is) before I go off and attempt to roll my own.
Thank you,
Matt
C-c C-l is what you are looking for? Use C-c C-h to see all the keys that are bound on C-c map (an even better approach is to install helm-descrbind from emacs package manager - you will be pleasantly surprised :).
[edit:] Sorry, misread your post slightly. You want batch evaluation. That is not available for R. The reason, the analysis in R is usually a complex process which you don't want to execute again and again. So you keep your interactive session open and iteratively achieve what you want.
There have been talks inside ESS to add some batch functionality, but it seems like very few people really need that.

r-autoyas in Emacs

I am trying to get r-autoyas to work on Emacs 23.3.1
I have installed yasnippet and it works fine on its own. For eg: TAB after 'for' in c++ mode auto expands as it should. I then went on the get r-autoyas to work. I have followed the instructions given in the github repository but am unable to get the TAB to expand even inbuilt functions in an R buffer.
If I type, rnorm( and then press TAB, a minibuffer opens which shows me the various arguments to the functions. Is this the default behavior? Or should it fill in the input arguments as default and let me change them one by one?
I searched online to see if anyone else had come across this problem. In one forum, it was mentioned that it could be because of the auto-completion feature in Emacs.
I have the following lines in my init.el file which were given in the instructions:
(require 'r-autoyas)
(add-hook 'ess-mode-hook 'r-autoyas-ess-activate)
(add-hook 'ess-mode 'yas/minor-mode-on)
You need to add your own yasnippets for ESS/R for any yasnippet expansion to work. By default there are none.
The behavior you are seeing when you type rnorm(<TAB> has nothing to do with yasnippet, this is behavior that ESS provides to make your R-coding-life easier.
So -- you will have to create your own snippets for R. You need to do this under the text-mode/ess-mode directory wherever your yasnippets are located (you'll have to create the ess-mode directory).
Here are some of my R snippets. I thought I'd use them more, but I only really ever use the setGeneric and setMethod snippets ... and those aren't all that bullet proof, either.

does clisp (Ubuntu port) has indent and parenthese completion

clisp interpreter come from Ubuntu package is very good with readline, editing single line of code is easy. Though work it with Slime most time it make me think if it's possible to get auto-indent and parentheses flashing/completion in clisp interpreter itself.
EDIT:
(ED "FILE.NAME.LISP") can call system editor, and start editing, the results will not AUTO loaded into the REPL.
clisp does flash back to matching paren as long as you stay on a single line. This limitation stems from readline which provides the feature.
clisp does not auto-indent on console.
the editor does not auto-load the edited file because your edit might be unsuccessful; you should try to compile the file first to uncover the errors.
EDIT: (ED "FILE.NAME.LISP") can call system editor, and start editing,
the results will not AUTO loaded into the REPL.
You can easily write a function that will call up your editor then load the file when you're done with it.
Something like this in your .clisprc.lisp, for example:
(defun edit-load-file (filename)
(ed filename)
(load filename))

Resources