项目作者: randy3k

项目描述 :
A 21 century R console
高级语言: Python
项目地址: git://github.com/randy3k/radian.git
创建时间: 2017-03-31T23:13:43Z
项目社区:https://github.com/randy3k/radian

开源协议:MIT License

下载


radian: A 21 century R console

Main
codecov

Conda version

radian is an alternative console for the R program with multiline editing and rich syntax highlight.
One would consider radian as a ipython clone for R, though its design is more aligned to julia.

Features

  • cross platform, runs on Windows, macOS and Linux
  • shell mode: hit ; to enter and <backspace> to leave
  • reticulate python repl mode: hit ~ to enter
  • improved R prompt and reticulate python prompt
    • multiline editing
    • syntax highlight
    • auto completion (reticulate autocompletion depends on jedi)
  • unicode support
  • latex completion
  • auto matching parens/quotes.
  • bracketed paste mode
  • emacs/vi editing mode
  • automatically adjust to terminal width
  • read more than 4096 bytes per line

Installation

Requirements:

  1. # install released version
  2. pip3 install -U radian
  3. # to run radian
  4. radian
  1. # or the development version
  2. pip3 install -U git+https://github.com/randy3k/radian

Alternatively, if you use conda or miniconda,

  1. conda install -c conda-forge radian

In this case, it is recommended to install R via conda to ensure consistency in C runtime libraries.

Alias on unix system

You could alias r to radian by putting

  1. alias r="radian"

in ~/.bash_profile such that r would open radian and R would still open the traditional R console.
(R is still useful, e.g, running R CMD BUILD.)

Settings

radian can be customized by specifying the below options in various locations

  • $XDG_CONFIG_HOME/radian/profile or $HOME/.config/radian/profile (Unix)
  • %USERPROFILE%/radian/profile (Windows)
  • $HOME/.radian_profile (Unix)
  • %USERPROFILE%/.radian_profile (Windows)
  • .radian_profile in the working directory

The options could be also specified in the .Rprofile files, however,
it is not recommended because

  1. the settings are not persistent when vanilla mode is used;
  2. it doesn’t work well with packrat or renv.
  1. # Do not copy the whole configuration, just specify what you need!
  2. # see https://pygments.org/styles
  3. # for a list of supported color schemes, default scheme is "native"
  4. options(radian.color_scheme = "native")
  5. # either `"emacs"` (default) or `"vi"`.
  6. options(radian.editing_mode = "emacs")
  7. # enable various emacs bindings in vi insert mode
  8. options(radian.emacs_bindings_in_vi_insert_mode = FALSE)
  9. # show vi mode state when radian.editing_mode is `vi`
  10. options(radian.show_vi_mode_prompt = TRUE)
  11. options(radian.vi_mode_prompt = "\033[0;34m[{}]\033[0m ")
  12. # indent continuation lines
  13. # turn this off if you want to copy code without the extra indentation;
  14. # but it leads to less elegent layout
  15. options(radian.indent_lines = TRUE)
  16. # auto match brackets and quotes
  17. options(radian.auto_match = TRUE)
  18. # enable the [prompt_toolkit](https://python-prompt-toolkit.readthedocs.io/en/master/index.html) [`auto_suggest` feature](https://python-prompt-toolkit.readthedocs.io/en/master/pages/asking_for_input.html#auto-suggestion)
  19. # this option is experimental and is known to break python prompt, use it with caution
  20. options(radian.auto_suggest = FALSE)
  21. # highlight matching bracket
  22. options(radian.highlight_matching_bracket = FALSE)
  23. # auto indentation for new line and curly braces
  24. options(radian.auto_indentation = TRUE)
  25. options(radian.tab_size = 4)
  26. # pop up completion while typing
  27. options(radian.complete_while_typing = TRUE)
  28. # the minimum length of prefix to trigger auto completions
  29. options(radian.completion_prefix_length = 2)
  30. # timeout in seconds to cancel completion if it takes too long
  31. # set it to 0 to disable it
  32. options(radian.completion_timeout = 0.05)
  33. # add spaces around equals in function argument completion
  34. options(radian.completion_adding_spaces_around_equals = TRUE)
  35. # automatically adjust R buffer size based on terminal width
  36. options(radian.auto_width = TRUE)
  37. # insert new line between prompts
  38. options(radian.insert_new_line = TRUE)
  39. # max number of history records
  40. options(radian.history_size = 20000)
  41. # where the global history is stored, environmental variables will be expanded
  42. # note that "~" is expanded to %USERPROFILE% or %HOME% in Windows
  43. options(radian.global_history_file = "~/.radian_history")
  44. # the filename that local history is stored, this file would be used instead of
  45. # `radian.global_history_file` if it exists in the current working directory
  46. options(radian.local_history_file = ".radian_history")
  47. # when using history search (ctrl-r/ctrl-s in emacs mode), do not show duplicate results
  48. options(radian.history_search_no_duplicates = FALSE)
  49. # ignore case in history search
  50. options(radian.history_search_ignore_case = FALSE)
  51. # do not save debug browser commands such as `Q` in history
  52. options(radian.history_ignore_browser_commands = TRUE)
  53. # custom prompt for different modes
  54. options(radian.prompt = "\033[0;34mr$>\033[0m ")
  55. options(radian.shell_prompt = "\033[0;31m#!>\033[0m ")
  56. options(radian.browse_prompt = "\033[0;33mBrowse[{}]>\033[0m ")
  57. # stderr color format
  58. options(radian.stderr_format = "\033[0;31m{}\033[0m")
  59. # enable reticulate prompt and trigger `~`
  60. options(radian.enable_reticulate_prompt = TRUE)

Custom key bindings

  1. # allows user defined shortcuts, these keys should be escaped when send through the terminal.
  2. # In the following example, `esc` + `-` sends `<-` and `ctrl` + `right` sends `%>%`.
  3. # Note that in some terminals, you could mark `alt` as `escape` so you could use `alt` + `-` instead.
  4. # Also, note that some ctrl mappings are reserved. You cannot remap m, i, h, d, or c
  5. options(
  6. radian.escape_key_map = list(
  7. list(key = "-", value = " <- "),
  8. ),
  9. radian.ctrl_key_map = list(
  10. list(key = "right", value = " %>% ")
  11. )
  12. )

FAQ

Unicode doesn’t work in Windows and R 4.2+.

The latest version of R supports Unicode codepage directly. However, radian relies on Python and Python doesn’t support Unicode in the way that R supports it. A workaround could be found here: https://github.com/randy3k/radian/issues/269#issuecomment-1169663251. Though it may break the ploting device (running plot() will kill radian).

I can’t specify python runtime in reticulate.

It is expected. radian runs on python and the python runtime used by radian is forced in
reticulate. reticulate::py_config() gives the note:

  1. NOTE: Python version was forced by the current process

In order to use radian with another python runtime, you will need to install radian on
that python environment.

How to switch to a different R or specify the version of R.

There are serveral options.

  • The easiest option is to pass the path to the R binary with --r-binary, i.e., radian --r-binary=/path/to/R
  • Also, one could expose the path to the R binary in the PATH variable
  • The environment variable R_BINARY could also be used to specify the path to R.
  • The environment variable R_HOME could also be used to specify R home directory. Note that it is should be set as the result of R.home(), not the directory where R is located. For example, in Unix
    1. $ env R_HOME=/usr/local/lib/R radian

Cannot find shared library

Please also make sure that R was installed with the R shared library libR.so or libR.dylib or libR.dll. On Linux, the configure flag ./configure --enable-R-shlib may be needed to install R from the source. Do not forget to make clean to force the recompilation of the files with the correct compiler options.

Outdated setuptools

If you encounter

The package setup script has attempted to modify files on your system that are not within the EasyInstall build area.

Please update your setuptools by

  1. pip install -U setuptools

How to use local history file

radian maintains its own history file .radian_history and doesn’t use the .Rhistory file. A local .radian_history is used if it is found in the launch directory. Otherwise, the global history file ~/.radian_history would be used. To override the default behavior, you could launch radian with the options: radian --local-history, radian --global-history or radian --no-history.

Does it slow down my R program?

radian only provides a frontend to the R program, the actual running eventloop is the same as that of the traditional R console. There is no performance sacrifice (or gain) while using this modern command line interface.

However, it was reported that radian may be slower when using parallel computation, specifially when using forking. User should take extra care in those cases.

Nvim-R support

Put

  1. let R_app = "radian"
  2. let R_cmd = "R"
  3. let R_hl_term = 0
  4. let R_args = [] " if you had set any
  5. let R_bracketed_paste = 1

in your vim config.

reticulate Auto Completions

To enable reticulate prompt completions, make sure that jedi is installed.

  1. pip install jedi

Alternatively, if you use conda,

  1. conda install -c conda-forge jedi

Prompt not shown inside a docker container

It maybe caused by the invalid terminal size, try running stty size in your terminal
to see if it returns a correct size. You could change the values of it from the environmental variables
$COLUMNS and $LINES when you log-in the docker container.

  1. docker exec -it <container> bash -c "stty cols $COLUMNS rows $LINES && bash"

Why called radian?

radian is powered by (π)thon.

Credits

radian wouldn’t be possible without the creative work prompt_toolkit by Jonathan Slenders.