7

When I open R from the terminal, I can use the Tab key to autocomplete functions and objects. By pressing the Tab key after a function and (, the function arguments get listed. However, it looks like that in Ubuntu those arguments are ordered according to how they are declared; in macos, they are ordered alphabetically.

Under MacOs:

> findInterval(
all.inside=        left.open=         rightmost.closed=  vec=               x= 

Under Ubuntu:

> findInterval(
x=                 vec=               rightmost.closed=  all.inside=        left.open=

I'd like to have the Ubuntu behaviour on my Mac. I gave a look at the readline options (not sure if relevant) through man readline from the terminal, but wasn't able to find anything useful.

nicola
  • 24,005
  • 3
  • 35
  • 56
  • Did you try in terminal before launching R : `compgen -o nosort`? – Waldi Mar 25 '21 at 23:17
  • It says `-bash: compgen: nosort: invalid option name`. I get `compgen: usage: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [word]` if I make `compgen -h` and no `man` looks to be available. – nicola Mar 26 '21 at 07:52
  • surprising, but I tried on Debian, not MacOS, see [programmable completion builtins](https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html) – Waldi Mar 26 '21 at 08:10
  • I tried also on Ubuntu with the same output (however, I want the change on Mac of course). – nicola Mar 26 '21 at 08:11
  • I checked that link after your comment and I saw the option listed. However, can't make it work on both Ubuntu and MacOs. – nicola Mar 26 '21 at 08:12
  • what does `bash --version` say? – Waldi Mar 26 '21 at 08:17
  • `GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19) Copyright (C) 2007 Free Software Foundation, Inc.` – nicola Mar 26 '21 at 08:18
  • It's 4.3.48 on Ubuntu. – nicola Mar 26 '21 at 08:19
  • [This](https://itnext.io/programmable-completion-for-bash-on-macos-f81a0103080b) might be a part of the problem – Waldi Mar 26 '21 at 08:22
  • I upgraded to version 5.1, the nosort option now doesn't return an error, however in R the behaviour is the same. – nicola Mar 26 '21 at 08:28
  • I have no other ideas : perhaps others can help :-) – Waldi Mar 26 '21 at 08:33
  • One question : did you set the new bash as default as described in the link above? Perhaps R is still using the old version in terminal mode? – Waldi Mar 26 '21 at 09:08
  • Yes, I did. Nothing changed unfortunately. – nicola Mar 26 '21 at 10:02
  • perhaps check from R terminal which bash version it sees :`system('bash --version')` – Waldi Mar 26 '21 at 12:52
  • @Waldi It's the new 5.1.4 version. Interesting is if I do `compgen -o nosort` from the shell, I don't get any error; If I do from R `system("compgen -o nosort")` I get: `sh: line 0: compgen: nosort: invalid option name`, since it invokes `sh` instead of `bash`. Any idea? – nicola Mar 26 '21 at 13:03
  • how old is your Mac? From catalina on , MacOS seems to use `zsh` using this [completion configuration](http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System-Configuration). Expand is in this case always sorted unless sort option is set to false. – Waldi Mar 26 '21 at 19:23
  • Perhaps try something like `zstyle ':completion:*' sort 'false'` – Waldi Mar 26 '21 at 19:37
  • 1
    I suspect that shell autocompletion behavior is not inherited by the R console, even if launched from a Terminal shell window. I wonder if you should be looking at the `?rcompgen` page in the utils package? (We clearly do not have the same setup, since I get no autocompletion in a Terminal launched session. I only get variations on autocompletion in Rstudio and auto-hinting in R.app). – IRTFM Mar 27 '21 at 01:36

1 Answers1

5

Requirements

  • Open R via Terminal under macOS
  • Autocompletion orders function arguments according to the order of their declaration

As you observed, R arranges the arguments alphabetically when R-4.0.4.pkg is installed. However, when R is installed with Homebrew, they are displayed in the expected order.

So one possible solution could be to uninstall R and install it via Homebrew.

Deinstall R

rm /usr/local/bin/{R,RScript}
sudo rm -r /Applications/R.app
sudo rm -r /Library/Frameworks/R.framework
sudo pkgutil --regexp --forget 'org\.R-project*' 

Install R via Homebrew

brew install R

For me it shows the error that some directories like /usr/local/share/info are not writable for my user. copy/paste what brew recommends, for me it is for example:

sudo chown -R $(whoami) /usr/local/lib/pkgconfig /usr/local/share/info /usr/local/share/man/man3 /usr/local/share/man/man5

Then repeat the command:

brew install R

This time it works, it also installs all dependencies like openblas, readline, openssl@1.1 and so on.

Now when you start R, it shows a series of warnings like Setting LC_COLLATE failed. Assuming you are using zsh, create or edit ~/.zshrc and add the line

export LC_ALL=en_US.UTF-8

Test

Now open a new terminal window and type R. If you use autocompletion with findInterval( you will see the expected behavior, see screenshot:

macOS Test

Why do the two installations differ?

You can take a look at the configuration of the brew vs. the R-4.0.4.pkg variant. For brew you can find it in /usr/local/Cellar/r/4.0.4_2/lib/R/etc/Makeconf and it looks like this:

configure  '--prefix=/usr/local/Cellar/r/4.0.4_2' '--enable-memory-profiling' '--without-cairo' '--without-x' '--with-tcl-config=/usr/local/opt/tcl-tk/lib/tclConfig.sh' '--with-tk-config=/usr/local/opt/tcl-tk/lib/tkConfig.sh' '--with-aqua' '--with-blas=-L/usr/local/opt/openblas/lib -lopenblas' '--enable-R-shlib' '--disable-java' 'PKG_CONFIG_PATH=/usr/local/opt/gmp/lib/pkgconfig:/usr/local/opt/isl/lib/pkgconfig:/usr/local/opt/mpfr/lib/pkgconfig:/usr/local/opt/jpeg/lib/pkgconfig:/usr/local/opt/libpng/lib/pkgconfig:/usr/local/opt/openblas/lib/pkgconfig:/usr/local/opt/pcre2/lib/pkgconfig:/usr/local/opt/readline/lib/pkgconfig:/usr/local/opt/openssl@1.1/lib/pkgconfig:/usr/local/opt/tcl-tk/lib/pkgconfig:/usr/local/opt/xz/lib/pkgconfig' 'PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig:/usr/local/Homebrew/Library/Homebrew/os/mac/pkgconfig/10.15' 'CC=clang' 'CFLAGS=-Wno-implicit-function-declaration' 'LDFLAGS=-L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/xz/lib' 'CPPFLAGS=-I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/opt/xz/include' 'CXX=clang++' 'OBJC=clang'

For the Framework R-4.0.4.pkg variant you can find the information in the file /Library/Frameworks/R.framework/Resources/etc/Makeconf:

configure  'CC=clang -mmacosx-version-min=10.13' 'CXX=clang++ -mmacosx-version-min=10.13' 'OBJC=clang -mmacosx-version-min=10.13' 'FC=gfortran -mmacosx-version-min=10.13' 'F77=gfortran -mmacosx-version-min=10.13' 'CFLAGS=-Wall -g -O2' 'CXXFLAGS=-Wall -g -O2' 'OBJCFLAGS=-Wall -g -O2' 'FCFLAGS=-Wall -g -O2' 'F77FLAGS=-Wall -g -O2' '--enable-memory-profiling' '--x-libraries=/opt/X11/lib' '--x-includes=/opt/X11/include' '--enable-R-framework' '--build=x86_64-apple-darwin17.0' 'build_alias=x86_64-apple-darwin17.0' 'PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/opt/X11/lib/pkgconfig'

Note the CPPFLAGS and LDFLAGS for readline and gettext in the brew variant.

Test: Add CPPFLAGS/LDFLAGS to R-4.0.4.pkg Variant

Just to substantiate the theory that these compile/link flags are needed, R is built from source code here.

First, some dependencies are needed if they are not already present on the system.

Xcode
XQuartz-2.8.0.dmg from https://www.xquartz.org

brew install gfortran
brew install xz
brew install cairo
brew install pcre2
brew install libtiff libjpeg

Download the source code R-4.0.4.tar.gz from one of the mirrors.

The adapted configure command could now look like this:

./configure  'CC=clang -mmacosx-version-min=10.13' 'CXX=clang++ -mmacosx-version-min=10.13' 'OBJC=clang -mmacosx-version-min=10.13' 'FC=gfortran -mmacosx-version-min=10.13' 'F77=gfortran -mmacosx-version-min=10.13' 'CFLAGS=-Wall -g -O2' 'CXXFLAGS=-Wall -g -O2' 'OBJCFLAGS=-Wall -g -O2' 'FCFLAGS=-Wall -g -O2' 'F77FLAGS=-Wall -g -O2' '--enable-memory-profiling' '--x-libraries=/opt/X11/lib' '--x-includes=/opt/X11/include' '--enable-R-framework' '--build=x86_64-apple-darwin17.0' 'build_alias=x86_64-apple-darwin17.0' 'PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/opt/X11/lib/pkgconfig' 'CPPFLAGS=-I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/opt/xz/include' 'LDFLAGS=-L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/xz/lib' 'CFLAGS=-Wno-implicit-function-declaration'

Afterwards one can call

make

Make sure that you have created a backup before performing the last step. Finally with this command

sudo make install

one would create or overwrite /Library/Frameworks/R.framework. Calling /Library/Frameworks/R.framework/Resources/bin/R now shows the expected behavior regarding autocompletion of function parameters and also provides the expected capabilities.

Stephan Schlecht
  • 26,556
  • 1
  • 33
  • 47
  • 1
    Thank you very much for this answer. Didn't test yet, since it's a quite impactful solution, given also that I have hundreds of packages to reinstall. I will give it a try, nonetheless. – nicola Mar 28 '21 at 13:12
  • Just before I try this procedure, is it possible to make a local installation through `brew`? As far as I understand, I would just like to not make the link in `/usr/local/bin` so I can stay with the current installation, test if this works and incrementally install on the `brew` R all the packages. – nicola Mar 30 '21 at 12:31
  • 1
    The first thing I would recommend is to make a backup. You could install R with brew, it will detect that R and Rscript exists in /usr/local/bin and not overwrite it. You can then call /usr/local/Cellar/r/4.0.4_2/bin/R to run the version that brew installed. Alternatively, if you just want to try it out, you can use a virtualization solution (for example, Virtualbox, VMware Fusion, or Parallels). – Stephan Schlecht Mar 30 '21 at 14:06
  • I tried to install R from brew and it actually works for the completion. However, unfortunately, there are several downsides. 1) New packages are installed from source and a lot of problems may arise. 2) Packages that rely on external libraries are more difficult to install, because paths are not found. 3) Many `capabilities()` are not enabled (`jpeg`, `png`, `cairo` amongst others). Bottom line: if you have a fresh Mac, you can maybe install R from brew and try to tweak everything; but if you already installed the macos package, it's hard to go back. – nicola Mar 30 '21 at 15:36
  • Having said that, what you have wrote answers to my question and so, unless another less "invasive" solution pops up, I will grant you the bounty. – nicola Mar 30 '21 at 15:37
  • It would also be great to understand why the two installations differ in this aspect. – nicola Mar 30 '21 at 15:38
  • This is an interesting question. The assumption is that the brew variant is configured/compiled differently than the R-4.0.4.pkg variant. I have added details on this in the answer. I then built R from source with customized R-4.0.4.pkg variant settings. This is still an invasive solution and may not apply to your use case, but serves at least to support the assumption. – Stephan Schlecht Mar 31 '21 at 21:37
  • 1
    That's a great addition in your answer. Actually, if you don't perform `sudo make install` installing from source is not invasive (that's the way I used on Linux) and you can manually create symbolic links to the different R versions/installations. Still, it persists, I guess, the dependency problem: some of the packages need external libraries (like `rgdal` for instance) that might not be "seen" from the source installation. I will also give a try with this path. Thank you – nicola Apr 01 '21 at 06:27