DC

R Studio basics: 0826

RStudio Basics: Panes, Scripts, and Core Workflows

  • RStudio layout

    • Three main panes: Console, Environment, and Plots.
    • Console: where computations are executed and results appear.
    • Environment tab: lists all objects saved in the current session (e.g., imported datasets, fitted models).
    • Plots tab: displays graphical outputs produced during the session.
    • The video focuses mainly on the Console and leaves deeper emphasis on the Environment and Plots windows for later.
    • Zooming: you can enlarge the panes via View > Zoom In (and Zoom Out as needed).
  • Using the Console vs. using scripts

    • The Console allows direct entry of commands and basic calculations (acts as a simple calculator).
    • Example: entering 3+6 in the console yields the result 9.
    • Note on console output: the brackets you may see in the prompt/output are just formatting; you can ignore that first bracket in practice.
    • For sophisticated computations, you should not rely solely on the console; write commands in an R script for reproducibility.
    • Why use a script: console history is not automatically saved for long-term use; scripts preserve your commands for later review and re-run.
    • How to create an R script: File > New File > R Script, or View > Panes > Show All Panes. When multiple scripts open, you can close extras (e.g., close the duplicate untitled script).
    • Saving work: File > Save saves the R script file (not the contents of the console).
  • Running commands from an R script

    • Write your code in the script, then run it to send commands to the console.
    • Example in script: 7 * 9 + 2
    • To run:
    • Place the cursor on the line and press Run (or use the Run button).
    • You can run a single line or highlight multiple lines and run them sequentially.
    • To run multiple lines at once, highlight them (or use Run after selecting) so that all are moved to the console.
    • Output behavior: when commands are sent to the console, their results appear there (the console becomes the execution and display surface).
    • Demonstration: in the script, typing 7*9+2 and pressing Run moves the computation to the console and shows 65.
  • Handling incomplete commands and debugging in the console

    • If a command is incomplete (e.g., missing a closing parenthesis):
    • The console shows a plus sign at the bottom, indicating it awaits more input.
    • You can complete the command by adding the missing character(s) and pressing Enter.
    • Alternative to continue later: press Escape to cancel the current command, return to the ready state, fix in the script, and Run again.
    • This approach helps when building longer expressions or multi-line code blocks.
  • Types of commands with non-console outputs

    • Not every command returns a result in the console; some commands produce plots or open data viewers.
    • You will learn about loading and viewing data in upcoming sections.
  • Viewing data and help in R

    • Viewing a pre-programmed dataset in R:
    • Use the view command with a capital V: View(dataset_name)
    • Example: View(Iris) to preview a dataset about flowers.
    • The view command provides a data preview: listing variables and their values.
    • You can return to the R script by clicking the script’s title/tab.
    • Help system for datasets:
    • Use help(dataset_name) or help("iris").
    • The output appears in the Help tab and includes: dataset name, description, source, references, and example commands.
    • Important limitation: help files exist only for data that is pre-programmed into R.
    • If you upload your own data (e.g., from Excel), there will be no pre-made help documentation for that data.
    • The view command works for both pre-programmed data and user-uploaded data (view simply previews the data without relying on pre-loaded help files).
  • Example walkthroughs mentioned in the transcript

    • Simple arithmetic in the console: 3+6 → 9; 8 imes (3+2) → 40; and other basic operations like 7 imes 9 + 2 → 65.
    • Running code from a script demonstrates the flow: write, run, and observe the result in the console.
    • When data is needed for analysis, use View to preview, and use help to read documentation for pre-programmed datasets (Iris as a canonical example).
  • Practical tips for reproducible workflow

    • Keep your commands in an R Script rather than typing everything in the console.
    • Regularly save your R Script to preserve your workflow.
    • Use Run to execute code selectively (line-by-line or in blocks) or to execute the entire selection.
    • Be mindful of incomplete commands; fix them promptly or cancel and correct in the script.
    • Leverage View for quick data previews and Help for built-in datasets to understand structure and usage.
  • Quick reference equations (LaTeX)

    • Simple addition: 3+6 = 9
    • Mixed operation: 7 \times 9 + 2 = 65
    • Nested operations: 8 \times (3+2) = 40
    • These illustrate how to present mathematical expressions in notes using LaTeX formatting.