27  Script editor

Secrétaire personnel(le)

Those reading this handbook will almost certainly use R. This involves writing, copying, editing, and deleting line after line of code. For such an intensive task, making the experience as pleasant, productive, and efficient as possible is essential.

The core purpose of a script editor is to assist the user in every possible way. The most basic script editor available is a plain text editor (e.g., Notepad). It functions as a script editor because it allows text to be written, edited, and saved as code. In the early days of R, codes were written in Notepad or Notepad++ and then copied into the console (Chapter 26) for execution.

Today, many script editors exist, but most users rely on the editor included with an IDE (Chapter 28). Below are several key features that improve code writing:

Different script editors suit different needs (a personal assistant is choosen on specific skills needed to handle particular tasks). A programmer using multiple languages—such as Python, Java, and R—might prefer a multi-language editor. For the typical R user, the script editor built into RStudio is the most popular choice.

Formal

A script editor is a specialized text interface used by programmers to write, edit, and save code files. Unlike an interactive console (Chapter 26) that runs code immediately line-by-line, a script editor allows you to write out a full sequence of instructions, review them, and save them as a permanent file before running them.

Examples

  • Code as it appears in the simplest text editor without syntax highlighting and indentation.

    # Add one plus one
    1 + 1
    
    # Import your data into R
    data <- read.csv('my_data.csv')
    
    # A ggplot code
    ggplot(data, aes(x = age, y = height)) +
    geom_point()+
    theme_bw()
    
  • The same code with syntax highlighting for different object types: comments in gray, numbers (Chapter 30) in red, functions (Chapter 40) in blue, and characters (Chapter 32) and variables (Chapter 29) in black.

    # Add one plus one
    1 + 1
    
    # Import your data into R
    data <- read.csv(“my_data.csv”)
    
    
    # A ggplot code
    ggplot(data, aes(x = age, y = height)) +
        geom_point()+
        theme_bw()
  • A script editor includes various features to facilitate coding and improve productivity. This figure (Figure 27.1) is an example the script editor in RStudio displaying syntax highlighting on lines 1,2 3, and syntax suggestion and auto-completion for log as well as the help file (yellow rectangle).

    Figure 27.1: Features of script editor

Tips and warnings

  • By definition, a script is meant for writing code. To evaluate code in the console, the brute-force approach is to copy the chunks run. However, code can be sent directly to the console while remaining in the script editor with an IDE (Chapter 28).

  • Documenting code: A good habit that is easy to ignore is adding human-readable text. While code is for the machine, other people and the original author can benefit from verbal descriptions. These are called comments (see the code preceded by # above). They are ignored during evaluation by the computer.

  • An underused feature of script editors is keyboard shortcuts. Exploring available shortcuts is a good habit. For example, using a shortcut for variable assignment <- is very useful when certain symbols are not easily accessible on the keyboard. Custom shortcuts can even be created.

  • Embracing the features: Most script editors are more powerful than expected. Exploring and learning features make coding more productive.

  • Code is written in plain text format. It can be opened with any text editor including Microsoft Word. However, features like syntax highlighting belong to the script editor, not the file itself (unlike a Word document with embedded bold formatting, etc.).

  • Because a code is stored in plain text files, it can easily be tracked using a version control program.

  • Themes allow personalization. Themes can be light or dark, with bright or muted colors. This can be customized in RStudio under Tools > Global Options > Appearance.

Notation/Semantic/Terminology

  • Command, code, and script are often used interchangeably to refer to the text typed for the program to evaluate.1

  1. Curious readers may look up the precise definitions and distinct meanings of these terms.↩︎