Basic Positron Use

Overview

In this unit, we’ll cover some of the basics of using Positron.

Goals

  • Be familiar with basic ways to use Positron.

Reading

Introduction

At the heart of every IDE, such as Positron, is the editor. Positron allows you to edit files efficiently, and if it is code, you can also run it. We’ll explore some basic examples in this short unit.

Editing a text file

This is the most boring use of the editor. Just create a new file, or open an existing text file and edit it. A text file doesn’t necessarily need to be plain text. You can for instance open and edit a CSV file as well. We already tried that earlier when we discussed extensions. Feel free to revisit this by re-opening the prior CSV file and changing some of its contents. Then save it under a new name.

Depending on the type of file you are editing, Positron will try to provide some features that make it easier to work with it. For instance, if you open an R script, it will highlight the syntax and provide some code completion features. Syntax highlighting exists for many languages, not just R. As you work with the editor and learn more features, you will continue to find features that make your workflow easier and more efficient.

Editing and running code

The following only works if you already have R installed. If you don’t have it on your computer, you should still read this section, you just can’t run it for now.

Positron is meant for working with code. It is especially designed to work well with R and Python, but other languages are also supported, many through extensions. Let’s explore a small example with R. Go to “File” -> “New File” and pick “R File”. An empty file will open in the editor. Write a few lines of R code, for instance the following

x = seq(1,100)
y = x^2
plot(x,y)

Next, save the file somewhere. If Positron was ‘just an editor’, we would now have to switch over to R and run the code there. But with an IDE, we can do it all in one place. You should see a small triangle symbol in the top right corner of the editor. Press it to run the code. This will open the R console in the bottom window, execute the ‘source’ command to run the code, and show the resulting plot in the bottom right window. In the top right window, you can also see the variables that were generated.

Let’s say you want to have a line instead of circles for the plot but don’t remember the command. In the R console, you can type ?plot to call the help file for the plot function, which will show in a separate pane.

Once you found what you are looking for, change your code so it says plot(x,y,type='l') and run again. This time, instead of pressing the ‘run’ button, try to execute the code one line at a time. You can do that by starting in the first line and pressing Ctrl/Cmd + Enter. The first line will be executed, and you can then continue to the next line by pressing Ctrl/Cmd + Enter again. You will see the new plot show up. You should also see that the old plot is still there, you can go back to it with the arrows above the plot. The menu above the plot also allows you to save any of them, and do other things.

It is generally not advised to run code manually/interactively, or save plots through the GUI. You should instead have fully automated code do all of it to ensure reproducibility. But for exploring and testing, these iterative and manual steps are often needed and having all these features in one location makes some of these coding, modeling and data science tasks much more efficient.

Working with Quarto documents

The following only works if you already have both R and Quarto installed. If you don’t have both on your computer yet, you should still read this section, you just can’t run it for now.

Let’s try a brief Quarto document. Quarto itself is discussed in our Brief Introduction to Quarto course, so we won’t talk about it much here, just showcase how to work with it in Positron.

Start another new file, and this time from the menu, pick Quarto Document towards the bottom. Another editor window should open with an almost empty area. There will be a few lines at the top indicating a Quarto document.

Give it a title, write a heading with # Heading, then add some text. Next take the code from the previous example and insert it into the Quarto file as code chunk, like this

```{r}
#| echo: true
#| eval: true
x = seq(1,100)
y = x^2
plot(x,y)
```

Then add a bit more text after this code chunk. Finally, save the file as .qmd file.

Then press the ‘preview’ button above the editor window. Quarto should run and compile/render the document. If things work right, you should see a preview of your Quarto document show up in the viewer pane or open in an external browser, depending on your settings for the Quarto extension.

Summary

You saw two examples illustrating the basics of working with Positron. There is much more to discover. We’ll cover some of it in other courses and there’s much more that you might want to learn on your own. Fortunately, Positron is rather user-friendly, and since it is based on VS Code, which has been around for a long time and is very popular, there is a lot of documentation on it.

Further Resources

  • Positron Keyboard shortcuts - if you are one of those persons who prefers to use keyboard shortcuts instead of the mouse, check out this handy reference. Note that these are just a few basic ones, almost everything can be turned into a keyboard shortcut.

Test yourself

What happens when you click the Run (triangle) button on an R script in Positron?

Pressing Run sources the script in Positron, showing console output and plots in the integrated panes.

  • True
  • False
  • False
  • False

How can you run code line by line from the editor in Positron?

Use Ctrl/Cmd + Enter to execute the current line (and step through subsequent lines) directly from the editor.

  • False
  • False
  • True
  • False

After editing a Quarto .qmd file in Positron, how do you view the rendered document?

Use the Preview button to render; the output appears in the integrated viewer or an external browser based on the Quarto extension setting.

  • False
  • False
  • False
  • True

Practice

  • Start a new text file. Use Markdown Syntax to write a bit of text. You should see Positron do syntax highlighting for Markdown. Save the file as myfirstfile.md. For now, you can’t preview it unless you have Quarto (or some other extension) installed. You can try that later once you have Quarto up and running.
  • If you have R installed: Revisit the R script you created earlier. Extend it such that the code saves the figure to a file. Run the modified code. If you are new to R, look online to find out how to do this. Then also inspect the variables you created.
  • If you have R and Quarto installed: Re-open your Quarto document. Replace the code chunk with code that uses the ggplot2 package to create the same plot. If you are completely new to R and don’t know what packages are in R yet, you can skip this - or just look online or ask your AI and implement it, even if you don’t fully understand yet what’s going on 😁.