Using TeXmacs as an interface for R (part 1)

3 Dec

A nice, but not very well known, interface to R is
TeXmacs. (I have to say that I am not totally objective, since I wrote the interface between R and TeXmacs…)

Here’s a sample window:

Screen Shot 2012-12-04 at 12.05.43 PM

In the following few posts I’d like to explain how to use this interface.


First, install TeXmacs. Best is to install the latest and greatest version of the svn, because that will include the latest changes of the interface with R, and in particular syntax highlighting. If you don’t install the latest version, you’ll probably only miss syntax highlighting. In that case it would probably be good to still install the latest version of the TeXmacs library for R.

Now that TeXmacs is installed, we can start it.
From the command line, just enter:


A nice window will open:
TeXmacs main window

I will not go into how to work with TeXmacs very much, just the R interface.

Still, a few hints might help.

  1. You can chose your favorite key bindings in the menu Edit→Preferences→Look and Feel. I like emacs bindings.
  2. TeXmacs initially communicates with the user via the status line (bottom edge of window). If you prefer popups, select this from the menu Edit→Preferences→Interactive questions→In popup window.
  3. Usually TeXmacs files are stored with the extension “.tm”
  4. TeXmacs has a somewhat awkward interaction with the clipboard. Cut and paste within TeXmacs works fine. But, if you want to cut of paste to a different program, the text has to be converted from the special TeXmacs representation to text. To do that do:
    Edit→Copy to→Verbatim and Edit→Paste from→Verbatim. If you copy/paste a lot from other programs, you can chose:
    Tools→Selections→Import→Verbatim and Tools→Selections→Export→Verbatim. Then the default behaviour will be import and export in verbatim.
  5. TeXmacs has a lot of documentation. Explore the help menu and use its search functions.

Using TeXmacs and R

Now let’s work with R. Select the little monitor icon, and chose “R”.

Dropdown menu for choosing session type
After starting an R session, you get the usual R prompt ‘>’.


> 1:100

TeXmacs responds with the output from R:

 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100

What is nice in using TeXmacs as an interface to R is that you can now move up to the previous line, edit it, and press enter again. The output will be replaced by The new output.

If you would like to enter multiple lines of text at once, press shift-enter, instead of enter. The input field will expend, and nothing will be sent to the R process. Once you are done, press enter. Here is an example:

Screen Shot 2012-12-03 at 10.28.24 PM

You also see two additional features: syntax highlighting, and special background colors for input and output text. To turn on this coloring scheme, select the aptly named menu item Documentadd package→Programvarsession.

BTW: If you prefer to invert the behaviour of enter and shift-enter, select the option multiline input as shown below:

Screen Shot 2012-12-03 at 10.34.37 PM


TeXmacs+R can also handle graphics.

Enter the following expression:

> plot(1:10);v()

Notice that after the first plot() command, usually a new window pops up, and you have to select the TeXmacs window again.
The result will look something like the following:
Screen Shot 2012-12-03 at 10.43.41 PM
As you see, we needed two commands: the usual plot call, and v(). v() inserts the current plot as is into TeXmacs. So you can now do the following:

> title("Our first sample plot");v()

Which gives:
Screen Shot 2012-12-04 at 11.42.04 AM
We now have a second copy of our graph, with a title. You could, of course, have done two plot commands and one v().

> plot(1:10);title("Our first sample plot");v()

Let us try a more complicated example:
Here is a nice plot taken from the R Graph Gallery.

mu1mu2s11s12s22rhox1<-seq(-10,10,length=41) # generating the vector series x1
	term1 	term2 	term3 	term4 	term5 	term1*exp(term2*(term3+term4-term5))
} # setting up the function of the multivariate normal density
z<-outer(x1,x2,f) # calculating the density values
persp(x1, x2, z,
      main="Two dimensional Normal Distribution",
	             bgroup("[", frac((x[1]~-~mu[1])^2, sigma[11])~-~2~rho~frac(x[1]~-~mu[1],
	             sqrt(sigma[11]))~ frac(x[2]~-~mu[2],sqrt(sigma[22])) ~+~
	             frac((x[2]~-~mu[2])^2, sigma[22]),"]")),"}")),
      theta=30, phi=20,
      ltheta=90, lphi=180,
      nticks=5) # produces the 3-D plot
# adding a text line to the graph
mtext(expression(list(mu[1]==0,mu[2]==0,sigma[11]==10,sigma[22]==10,sigma[12]==15,rho==0.5)), side=3)

In order to paste this text properly into the R session, you will have to chose Edit→paste from→Verbatim. This is because TeXmacs normally treats the clipboard as representing a special TeXmacs format, not plain text.

One you press enter, you’ll see something like this:

Screen Shot 2012-12-03 at 11.03.43 PM

At the Bottom you see a strange combination of > and +. These represent the prompts given by R as the lines are sent to the session. The current interface between TeXmacs and R can not tell when the R process finished evaluating its input. Not only that, but it might be that we’re not even finished with sending/receiving all the input lines. Thesefore, hit enter a couple more times, followed by v():
Screen Shot 2012-12-03 at 11.15.02 PM
As you can see, the size of the graph doesn’t fit very well. We can control this by specifying the size of the image that should actually be produced by R v(width=8,height=8) (remember you can go up and edit the previous expression):
Screen Shot 2012-12-03 at 11.18.11 PM
Much better!


There are at least two ways of calling help in R inside TeXmacs. The first, is the usual help.start(), and using a web browser. The second, which is what I usually use, is to just ask for help in the usual way:

> ?plot

The help page is inserted formated into the current Buffer:
Screen Shot 2012-12-03 at 11.24.50 PM

Working on a remote server

One of the best features of the TeXmacs+R interface is the ability to work on remote systems. To do that it is best to first set up a possibility for password-less ssh sessions (which means you need to ssh-keygen on the source system, and add the public key in the file ~/.ssh/authorized_hosts.)

You should also install the TeXmacs package on the remote system.

One that is set up, enter the following command in an R session:

> system("ssh -t REMOTE_HOST R")

You should then get the prompt of the remote system. (The argument ‘-t’ makes ssh open a ‘psydo-terminal’, which interacts better with TeXmacs) Now enter:

> library(TeXmacs)

And, if you’d like to use graphics,

> start.view()

This last command opens a postscript device, and sets it up so that the v() command will copy from it to TeXmacs.
Here is what things look like then:
Screen Shot 2012-12-04 at 12.26.40 AM
Everything should work as on the local machine. Thus, you can ask R for help, and it should be inserted nicely formated into the current buffer.


17 Responses to “Using TeXmacs as an interface for R (part 1)”

  1. Herimanitra January 16, 2013 at 5:09 pm #

    Hi Sir!
    I’m working on windows!
    I’ve just installed TexMacs but I can’t figure out why my Texmacs is indefinitely loading when I click on R inside the session button

    • mlachmann January 16, 2013 at 6:09 pm #

      I have never used TeXmacs on windows. For R to work, one needs pipes, and I’m not sure how they work in windows.
      I’ll try to test it.

    • mlachmann February 11, 2013 at 5:13 pm #

      I prepared a virtualbox with TeXmacs and R installed that you could use inside Windows. It is an ubuntu 12.04 virtual box, and has the latest TeXmacs+ syntax highlighting installed.
      Download it from:!dhlVhDjS!VcqY6klTTX9S9VJUyzNbTnwcuT99zYNCfXYFYbRTnGc
      It is 880MB, and unpacks to around 2.5GB user/password is mike/mike – change the password by opening a terminal and giving the command ‘passwd’
      You can add a shared folder in virtualbox, and after reboot it will show up under /media inside ubuntu.

      You can get VirtualBox from here:

      • Herimanitra February 11, 2013 at 5:45 pm #

        Thanks! I’ll try it! but it’s heavy way!

  2. pseud0scientist February 7, 2013 at 7:14 pm #

    I am encountering a problem on my linux system (ubuntu): When I load the R session, a little red arrow is pointing to the window in which the R console is loaded with read text reading “Dead”. No R commands work. Any suggestions?

    • mlachmann February 8, 2013 at 12:06 am #

      I’m just now trying to install ubuntu+R+texmacs in a virtual machine to see what’s up. If you give me a more precise description of
      what you have, I can try to fit it better. I’m trying ubuntu 12.04, with standard apt-get install r-base. How did you install texmacs?
      One thing you can check is the file /tmp/log.

    • mlachmann February 8, 2013 at 9:24 am #

      OK, try the following:
      Download texmacs.tar.bz2 from
      unpack it with
      tar jxf texmacs.tar.bz2
      Then go into the directory src and install:
      cd src
      sudo make install
      Then try it:
      If you have a very different version of ubuntu than 12.10, then you might need to recompile. First do
      sudo apt-get build-dep texmacs
      then, in the directory src do:
      make clean
      and then:
      sudo make install

      • mlachmann February 8, 2013 at 12:19 pm #

        Sorry, I’m having a hard time building a package instead of just providing a compiled directory. I’ll fight with it some more over the weekend.

      • pseud0scientist March 30, 2013 at 7:52 pm #

        This worked perfectly! Thanks so much and sorry for taking so long to respond. I am looking forward to using texmacs with R!

  3. Nicola Mingotti (@nico020978) January 27, 2015 at 10:29 pm #

    Hi, I’m trying R +TeXmacs, on OSX (10.9.5) , and Linux (debian stable). After a bit of tweaking i got it working, and it seems it works very well. There are only a couple small but annoying of issues (i’m using release TeXmacs 1.99.2 ):

    1) Every command I ginve on the prompt is echoed in output before the result.

    2) Every time I start the R (or also python) console in TeXmacs the CPU usage rise to 100%, this is annoing because after a couple of minutes the fan starts buzzing.

    Have you any idea how to fix this things?
    Thank you in advance!

    Nicola Mingotti

    • mlachmann January 28, 2015 at 12:18 am #


      1) I’m sorry. This is “by design”. Echo is on, because echo off causes problems with recognising end of prompt, I think. I haven’t worked on
      the plugin in a while, but I did try to fix this several times, and ended up going back to echo on. Sorry. Identifying end of output from R, when it is
      waiting for new input is a major hassle…

      2) That is annoying. I’m assuming this is on OSX, because I have the same problem there. The solution is to compile with the –enable-qtpipes
      flag. The TeXmacs ftp site has now a version that is compiled with that flag:
      (or check for a newer version)

  4. Kyle November 17, 2017 at 3:55 am #

    Hi, I am trying to use texmacs (1.99.5) + R on Manjaro Linux, but every time I try to evaluate an R command, Texmacs prints the word “Busy…”. and it just stays that way. python, Maxima, scheme, shell, etc. seem to be working correctly. What do you think might be broken?

    • mlachmann November 20, 2017 at 4:33 pm #

      I’ll try to install Manjaro Linux (on my mac….) just to make things smoother – you’re just using the regular version, right, 17.0.6? And R from the command line is working with no problem?

      • Kyle December 16, 2017 at 11:56 am #

        The answers to both questions are yes. I just see Busy… followed by a field with R] that can’t do anything.

    • mlachmann December 17, 2017 at 4:15 am #

      OK, I installed manjaro under parallels under OSX. Works! Pretty nice.
      I installed texmacs and R. I checked that both work.
      Indeed R doesn’t work under TeXmacs. In my installation, this is because for some reason the interface between R and TeXmacs, called tm_r, isn’t installed. It should be under /usr/lib/TeXmacs/bin
      I’m not sure why. I was trying to install TeXmacs from the source, but I was having problems (with guile). Do you know how to recompile the TeXmacs package in manjaro?

      OK, actually I don’t need to compile all of TeXmacs, just tm_r.
      It seems there’s a problem with the compilation, which can be solved by adding a line
      #define HAVE_PTY_H
      to the source code, or compile with -D HAVE_PTY_H
      Maybe I should tell the package maintainer.

      So, you can compile that file in the TeXmacs source and put it in said directory (in the source directory go to plugins/r and type make).

      Or you can just download my compiled version and put it there (/usr/lib/TeXmacs/bin) (Oh, yeah, I forgot, and chmod a+x it..)

      • Kyle December 17, 2017 at 6:00 pm #

        Thanks! Your tm_r executable worked like a charm once I dropped it in there and ran `chmod +x` on it. The R startup message is really verbose (more than usually, since in addition to the usual R startup, it also shows some code from the R TeXmacs package I think). But, hey, it works afterwards :).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: