# Getting Started with R on Nextjournal

Run Code, Create Graphs, Import and Save Data

Micah Dombrowski

![R_logo.svg][nextjournal#file#10b98f23-aa9a-4b9f-9c1e-d7a95f029a5a] 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: ```r id=e316649e-9cdc-4021-adf0-b5e6d0453030 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](https://nextjournal.com/nextjournal/what-are-environments)**, 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: ```r id=c721261e-4cf7-44ac-98c2-97916fb07411 plist <- installed.packages() t(plist[order(plist[,1]),3]) ``` Additional packages that are required for an article should be [installed ](https://nextjournal.com/nextjournal/install-r-packages)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](https://nextjournal.com/nextjournal/transclusions)* its environment into your article, or [remix it](https://nextjournal.com/nextjournal/remix). Below, we demonstrate one method that you can use to [create a plot](https://nextjournal.com/nextjournal/plot-in-r) from the 'iris' dataset using [Plotly](https://plot.ly/r/). ```r id=939b5699-8ece-4a5f-ac99-72e1862d8acb 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. ```r id=a67f0d84-826d-47ba-b60f-2016754ab724 iris ``` There are [multiple ways](https://nextjournal.com/nextjournal/data-in-r) 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][nextjournal#file#87cf6987-c365-4528-be66-d723bd814540] 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. ```r id=740514f0-8dd8-426a-8c5d-0c90e1bd103c data <- read.csv([reference][nextjournal#reference#1542e448-62f8-4556-b07f-66bbf6cfde44], header=FALSE) ``` You can export results by writing or copying files to the `/results` directory. Executing the following code makes data available for download. ```r id=b137dec7-6288-4429-99c3-381f38a78de3 write.csv(data, "/results/cubic.csv") ``` [cubic.csv][nextjournal#output#b137dec7-6288-4429-99c3-381f38a78de3#cubic.csv] Feel free to remix this article, or explore our collections to see [more examples](https://nextjournal.com/collection/highlights) and [learn ](https://nextjournal.com/collection/learn)more about the platform. We're excited to see what you [create ](https://nextjournal.com/new)with Nextjournal! [nextjournal#file#10b98f23-aa9a-4b9f-9c1e-d7a95f029a5a]: [nextjournal#file#87cf6987-c365-4528-be66-d723bd814540]: [nextjournal#reference#1542e448-62f8-4556-b07f-66bbf6cfde44]: <#nextjournal#reference#1542e448-62f8-4556-b07f-66bbf6cfde44> [nextjournal#output#b137dec7-6288-4429-99c3-381f38a78de3#cubic.csv]:
This notebook was exported from https://nextjournal.com/a/C6K42MXGLmFkWvgDqNpsJJ?change-id=CHMNA1ejaoTKfXY9zBE4sf ```edn nextjournal-metadata {:article {:settings {:numbered? true, :sidebar? true, :image "nextjournal/ubuntu:17.04-658650854", :subtitle? true}, :nodes {"10b98f23-aa9a-4b9f-9c1e-d7a95f029a5a" {:id "10b98f23-aa9a-4b9f-9c1e-d7a95f029a5a", :kind "file"}, "1542e448-62f8-4556-b07f-66bbf6cfde44" {:id "1542e448-62f8-4556-b07f-66bbf6cfde44", :kind "reference"}, "740514f0-8dd8-426a-8c5d-0c90e1bd103c" {:compute-ref #uuid "a033cb70-7b71-11e8-a8de-70e746be6acb", :exec-duration 451, :id "740514f0-8dd8-426a-8c5d-0c90e1bd103c", :kind "code", :name "read uploaded file", :output-log-lines {}, :runtime [:runtime "f599449f-484e-4d27-8976-e7a8823601c5"]}, "87cf6987-c365-4528-be66-d723bd814540" {:id "87cf6987-c365-4528-be66-d723bd814540", :kind "file"}, "939b5699-8ece-4a5f-ac99-72e1862d8acb" {:compute-ref #uuid "9f7e1000-7b71-11e8-a8de-70e746be6acb", :exec-duration 852, :id "939b5699-8ece-4a5f-ac99-72e1862d8acb", :kind "code", :output-log-lines {}, :runtime [:runtime "f599449f-484e-4d27-8976-e7a8823601c5"]}, "a67f0d84-826d-47ba-b60f-2016754ab724" {:compute-ref #uuid "a0031e80-7b71-11e8-a8de-70e746be6acb", :exec-duration 300, :id "a67f0d84-826d-47ba-b60f-2016754ab724", :kind "code", :output-log-lines {}, :runtime [:runtime "f599449f-484e-4d27-8976-e7a8823601c5"]}, "b137dec7-6288-4429-99c3-381f38a78de3" {:compute-ref #uuid "d9a88120-7b71-11e8-a8de-70e746be6acb", :exec-duration 148, :id "b137dec7-6288-4429-99c3-381f38a78de3", :kind "code", :name "read results file", :output-log-lines {}, :runtime [:runtime "f599449f-484e-4d27-8976-e7a8823601c5"]}, "c721261e-4cf7-44ac-98c2-97916fb07411" {:compute-ref #uuid "9f423f80-7b71-11e8-a8de-70e746be6acb", :exec-duration 364, :id "c721261e-4cf7-44ac-98c2-97916fb07411", :kind "code", :output-log-lines {}, :runtime [:runtime "f599449f-484e-4d27-8976-e7a8823601c5"]}, "e316649e-9cdc-4021-adf0-b5e6d0453030" {:compute-ref #uuid "c3e3ec70-85d6-11e8-b16c-f5d3632918eb", :exec-duration 533, :id "e316649e-9cdc-4021-adf0-b5e6d0453030", :kind "code", :output-log-lines {}, :runtime [:runtime "f599449f-484e-4d27-8976-e7a8823601c5"]}, "f599449f-484e-4d27-8976-e7a8823601c5" {:environment [:environment {:node/id "e8cb826f-2c7d-4cdd-b45b-0ad4282c5394", :article/nextjournal.id #uuid "5b45e6f7-fe51-488a-b89b-1c8f74dfb387", :change/nextjournal.id #uuid "5b460b54-56eb-4bc7-ba8d-eb84ab5ef807"}], :id "f599449f-484e-4d27-8976-e7a8823601c5", :kind "runtime", :language "r", :name "Main", :type :nextjournal}}, :nextjournal/id #uuid "59d25e7f-2efa-4b5d-8ab8-8f8a9558ca1b", :article/change {:nextjournal/id #uuid "5b5cf9ce-2182-4e51-accb-1fa7b0ca83ae"}}} ```