Skip to content

Commit

Permalink
[instructions][xs]: R usage for getting metadata and data - refs #115 (
Browse files Browse the repository at this point in the history
…#121)

* [instructions][xs]: R usage for getting metadata and data - refs #115

* [fixed][xs]: updated path for datapackage.json -refs #115
  • Loading branch information
Mikanebu committed Oct 25, 2017
1 parent 3a53fec commit 4323fad
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions views/_instructions.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
{% macro r(dataset) -%}
<p>In order to use Data Package in R follow instructions below:</p>
<div class="highlight">
<pre>
<span></span>install.packages<span class="p">(</span><span class="s">"devtools"</span><span class="p">)</span>
<span class="kn">library</span><span class="p">(</span>devtools<span class="p">)</span>
install_github<span class="p">(</span><span class="s">"hadley/readr"</span><span class="p">)</span>
install_github<span class="p">(</span><span class="s">"ropenscilabs/jsonvalidate"</span><span class="p">)</span>
install_github<span class="p">(</span><span class="s">"ropenscilabs/datapkg"</span><span class="p">)</span>
<pre class="hljs"><code>install.packages(<span class="hljs-string">"rjson"</span>)
<span class="hljs-keyword">library</span>(<span class="hljs-string">"rjson"</span>)

<span class="c1">#Load client</span>
<span class="kn">library</span><span class="p">(</span>datapkg<span class="p">)</span>
json_file &lt;- <span class="hljs-string">'{{dataset.path}}/datapackage.json'</span>
json_data &lt;- fromJSON(paste(readLines(json_file), collapse=<span class="hljs-string">""</span>))

<span class="c1">#Get Data Package</span>
datapackage <span class="o">&lt;-</span> datapkg_read<span class="p">(</span><span class="s">"{{dataset.path}}"</span><span class="p">)</span>
<span class="hljs-comment"># see metadata</span>
print(json_data, row.names = <span class="hljs-literal">FALSE</span>)

<span class="c1">#Package info</span>
<span class="kp">print</span><span class="p">(</span>datapackage<span class="p">)</span>

<span class="c1">#Open actual data in RStudio Viewer</span>
{% for resource in dataset.resources %}View<span class="p">(</span>datapackage<span class="o">$</span>data<span class="o">$</span><span class="s">"{{resource.name}}"</span><span class="p">)</span>
{% endfor %}
<span class="hljs-comment"># access csv file by the index starting 1</span>
csv_url = json_data[[<span class="hljs-string">"resources"</span>]][[<span class="hljs-number">1</span>]][[<span class="hljs-string">"path"</span>]]
data &lt;- read.csv(url(csv_url))
print(data)
</code>
</pre>
</div>
{%- endmacro %}
Expand Down Expand Up @@ -72,7 +68,7 @@

<pre class="hljs"><code><span class="hljs-keyword">from</span> datapackage <span class="hljs-keyword">import</span> Package, Resource

package = Package(<span class="hljs-string">'https://pkgstore.datahub.io/core/finance-vix/latest/datapackage.json'</span>)
package = Package(<span class="hljs-string">'{{dataset.path}}/datapackage.json'</span>)

<span class="hljs-comment"># see metadata</span>
print(package.descriptor)
Expand Down

0 comments on commit 4323fad

Please sign in to comment.