Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing function arguments in order in an R shell

Tags:

macos

r

readline

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.

like image 732
nicola Avatar asked Mar 23 '21 15:03

nicola


People also ask

How to use arguments in R?

To start working with R arguments you must have a clear understanding of functions in R We can pass an argument to a function when we call that function. We just need to give the value of the argument inside the parenthesis after the function’s name. The following is the example of a function with a single argument.

What are the arguments of a function called?

This function takes two arguments, called first and second. Inside the body of the function, you can refer to the arguments by these names. When you call a function in R, you can specify the arguments in three different ways (in order of priority):

What is the Order of arguments in a generic function?

Argument order. The arguments will be assigned to names in the order in which they were given: When you are using generic functions, you cannot specify the argument name of the object on which the generic function is being called. You can still specify names for other arguments.

What are optional arguments in C++?

Arguments are optional, you only need to define them if the function requires any. A function can have multiple arguments. The names of the arguments are optional in a function call. When calling a function, you only need to give their values in the order they are in, in the function’s definition.


1 Answers

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, [email protected] 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/[email protected]/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.

like image 190
Stephan Schlecht Avatar answered Sep 29 '22 09:09

Stephan Schlecht