Interactive R for Beginners: Your First Interactive Script

Get Started with R Interactive Coding Using Quarto Live

This tutorial guides you through setting up and running your first interactive R script using Quarto Live. Learn the basics of interactive R coding, explore a step-by-step walkthrough, complete an exercise, and review troubleshooting tips.

Tools
Author
Affiliation
Published

March 8, 2025

Keywords

interactive R for beginners, R interactive coding, first interactive R script

Introduction

Interactive coding in R lets you experiment with code and see the results immediately—all within your web browser. With Quarto Live, you can write, edit, and run R code interactively using the webr engine. This tutorial will guide you step-by-step to create your very first interactive R script, explain each part of the code, provide an interactive exercise, and offer troubleshooting tips and additional resources.



Detailed Walkthrough

Understanding the Code

Let’s break down a simple interactive R script:

  1. Printing a Message:
    The message() function prints a message to the console.

  2. Performing a Calculation:
    We assign the result of a calculation to a variable. For instance, result <- 10 + 5 calculates the sum of 10 and 5.

  3. Displaying the Output:
    The print() function outputs the result to the console. We combine text and the variable using paste().

Below is the annotated code:

R Code Walkthrough

# Print a welcome message to the console.
message("Hello, Interactive R!")

# Calculate the sum of 10 and 5.
result <- 10 + 5

# Print the calculation result, combining text with the value.
print(paste("10 + 5 =", result))
Note

In this example, you’ll see the message, the calculation, and then the printed output.

Your First Interactive R Script

Try running the code block below. Then, modify it to experiment with different arithmetic operations or messages.

Source
```{webr}
# Your First Interactive R Script
message("Hello, Interactive R!")
result <- 10 + 5
print(paste("10 + 5 =", result))
```

Interactive Exercise

Now it’s your turn! Modify the code to perform a different calculation—change the numbers or operation. For instance, try multiplying two numbers instead of adding them.

Source
```{webr}
# Replace the blank with an arithmetic operation that multiplies 10 by 5.
message("Interactive R: Let's try multiplication!")
result <- 10 * 5  # <-- Change this line to test different operations
print(paste("10 * 5 =", result))
```
Note

Experiment with the code and observe how the output changes. If you encounter any errors, refer to the troubleshooting tips below.

Troubleshooting Tips

  • Common Issues:
    • Syntax Errors: Check for missing parentheses or quotation marks.
    • Variable Names: Ensure that variable names are spelled correctly and consistently.
    • Execution Problems: If the code doesn’t run, refresh the page and try again.
  • Helpful Advice:
    • Use the interactive editor’s immediate feedback to pinpoint issues.
    • Review the Quarto Live documentation if you encounter unexpected behavior.

Real-world Example

Let’s build a simple interactive analysis using the built-in mtcars dataset. In this example, we calculate and print the mean miles-per-gallon (mpg) from the dataset.

Source
```{webr}
# Load the dataset
data(mtcars)

# Calculate the mean mpg
mean_mpg <- mean(mtcars$mpg)

# Display the result
print(paste("The mean MPG for mtcars is", round(mean_mpg, 2)))
```
Note

This example demonstrates how interactive scripts can be used for data analysis in a practical scenario.

Further Reading

For further exploration and advanced topics in interactive R coding, consider these resources:

Conclusion

This expanded tutorial provides a comprehensive introduction to interactive R coding with Quarto Live. By following the detailed walkthrough, experimenting with interactive exercises, and reviewing real-world examples, you’re well on your way to mastering interactive R scripting. Continue exploring the further reading links to deepen your understanding and build more advanced interactive projects.

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2025,
  author = {Kassambara, Alboukadel},
  title = {Interactive {R} for {Beginners:} {Your} {First} {Interactive}
    {Script}},
  date = {2025-03-08},
  url = {https://www.datanovia.com/learn/interactive/r/basics.html},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2025. “Interactive R for Beginners: Your First Interactive Script.” March 8, 2025. https://www.datanovia.com/learn/interactive/r/basics.html.