<p>Saving your data in the cloud ensures that when you send your scripts to your colleagues, you don’t have to send them your data or any additional files with it. When it’s a URL link rather than “C://…” or “/home/…”, your script is always pointing to the same path/address. In this text, we’ll go through how we can use a cloud dataset in our R scripts. We’re going to be using a dataset containing the <a href="https://www.datazar.com/file/f7cb0a20c-2f1c-4ad5-9d05-900d7af97a9c">population of Earth from 5000BC until 2016</a>.</p><p>There are several ways and packages to access a url from R. In the Datazar SDK, we’ll be using the “httr” package. Let’s go ahead and grab the <a href="https://github.com/datazar/datazar-r">Datazar SDK for R</a>. I’ve also included the R code here so you can just copy and paste it to your script.</p><pre>datazar&lt;-function(username,token,objectType,objectId,option) {<br>    require(httr)<br>    require(jsonlite)<br>    url&lt;-paste(&quot;<a href="https://api.datazar.com/">https://api.datazar.com/</a>&quot;,objectType,&quot;/&quot;,objectId,&quot;/&quot;,option,&quot;&quot;,sep=&quot;&quot;)<br>    data&lt;-GET(url,authenticate(username,token,type=&quot;basic&quot;))<br>    return(fromJSON(content(data),flatten=TRUE))<br>}<br>datazarData&lt;-function(username,token,fileId) {<br>    return(datazar(username,token,&quot;files&quot;,fileId,&quot;data&quot;))<br>}</pre><p>We’ll be using the datazarData function.</p><p>Here are the parameters we need:</p><ul><li>username</li><li>token</li><li>fileId</li></ul><pre>myUsername&lt;-&quot;aman&quot;<br>myToken&lt;-&quot;mysupersecrettokenthaticantshow&quot;<br>fileId&lt;-&quot;f7cb0a20c-2f1c-4ad5-9d05-900d7af97a9c&quot;</pre><pre>data&lt;-datazarData(myUsername,myToken,fileId)</pre><p>That’s it! All done. There’s no need to parse the JSON since the datazarData function takes care of it. Let’s go ahead and plot it so we can see what it looks like.</p><pre>plot(data,&quot;Year&quot;,&quot;Population&quot;)</pre>R Plot of the streamed dataset.<h3>Conclusion</h3><p>We went over how to stream datasets directly from the cloud. This method uses HTTP “Basic Authentication” and secures your connection to the Datazar API while you’re streaming your datasets.</p><p>I have included both the R script in a <a href="https://www.datazar.com/project/p9c9e37a6-f1bf-4f57-ac3b-77eefcc31ade">project</a> to you can use that one if you want to.</p><p><a href="https://www.datazar.com/file/fc8748a68-c0ff-4bdc-a17d-2bdb3004f0d7">R Script link.</a></p><p>Just modify the parameters to your own Datazar username and token. Using this as best practice will ensure your data is always in one location and you or your colleagues will never have to change dataset location-pointers in your scripts.</p><p>Hope you enjoyed this! Feel free to ask questions if you’re stuck somewhere.</p><p><a href="https://blog.datazar.com/streaming-cloud-data-to-mathematica-cb8ef9ae06ce#.b7ghxexad">Note: there’s a related post on how to do the exact same thing with Mathematica.</a></p><hr><p><a href="https://blog.datazar.com/streaming-cloud-data-to-r-e06038243c10">Streaming Cloud Data to R</a> was originally published in <a href="https://blog.datazar.com/">Datazar</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>