Getting Started with R on Nextjournal

Run Code, Create Graphs, Import and Save Data

This article covers some basic functions within R, showing you how to get started working with it on the Nextjournal platform. Links are provided to more in-depth documents covering specific topics and the platform itself.

Add to an article using the Insert Menu, which appears when you click the 'Add new content' button, the +-line that appears when hovering the cursor between article content, or when you hit the Return key twice at the end of a text paragraph. Code cells are language-specific—we'll add an R cell here and check the version:

toString(R.Version()["version.string"])

The basic functionality of R on Nextjournal is similar to R in other contexts. Almost 100 extra packages are installed in the default environment, including the tidyverse, the devtools, feather, and plotly.

This code displays a sorted list of all packages that are installed in the 'R Default' environment:

plist <- installed.packages()
t(plist[order(plist[,1]),3])

Additional packages that are required for an article should be installed in the standard way using the install.packages() command, and saved to a new environment. If an existing article has the setup you need, you can transclude its environment into your article, or remix it.

Below, we demonstrate one method that you can use to create a plot from the 'iris' dataset using Plotly.

plot_ly(data=iris, x=~Petal.Length, y=~Petal.Width,
        type="scatter", mode="markers") %>%
  layout(title="Iris Data",
         xaxis=list(title="Iris petal length"),
         yaxis=list(title="Iris petal width"))

It is also possible to display many forms of data in the results area below a cell. Strings and simple variables will be printed, while more complicated structures (arrays, dictionaries) can appear as an expandable tree or a table.

iris

There are multiple ways to get data in and out of Nextjournal. The most direct way to load our own data is to upload a file via the Insert Menu.

cubic.csv

Files uploaded in this way are stored in a persistent, versioned database. We can access a file in a language cell by inserting a reference to it from the @-menu, which appears automatically when you type the '@' symbol.

data <- read.csv(cubic.csv, header=FALSE)

You can export results by writing or copying files to the /results directory. Executing the following code makes data available for download.

write.csv(data, "/results/cubic.csv")
cubic.csv

Feel free to remix this article, or explore our collections to see more examples and learn more about the platform.

We're excited to see what you create with Nextjournal!