knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE,
  eval = FALSE
)

DataverkR

DataverkR is a R wrapper for the python library Dataverk. It has support for creating and publishing datapackages, with Plotly figures and R dataframes.

Importing libraries

To create and publish a datapackage you need to import DataverkR. Also if you want to add Plotly figures you need to import Plotly.

Python

You must have Python installed on your machine in order to run this package. The first time you use any python call in R, you will get a warning:

No non-system installation of Python could be found. Would you like to download and install Miniconda? Miniconda is an open source environment management system for Python. See miniconda for more details.

The warning will only be given one time on each computer and only if Python is not actually installed.

You can install a Python environment in several ways, none of them very good on Windows. The simplest from a desktop installation of R is to download and install it from Python and then follow this excellent guide at RStudio support.

In order to run Python in R, you need the reticulate package.

install.packages("reticulate")

Creating datapackages

library(reticulate)
library(dataverkr)
library(plotly)

Metadata

To create a datapackage you need to supply some metadata in a named list. To see which field are needed see the datapackages documentation.

metadata <- list(title = 'DataverkR example',
                 temporal = list(from = '2020-09-16',
                                 to='2021-11-05'))

Creating the datapackage

To create the datapackage, simply run:

dp <- create_dp(metadata)

If the metadata is incomplete, an error will occur.

Adding Plotly Figures and Dataframes

Dataframes

To add a dataframe:

# Creating dataframe
country <- c("Norge","Sverige", "Danmark")
numberOf <- c(200, 300, 500)
df <- data.frame(country, numberOf)

add_resource(dp = dp,
             dataframe = df,
             resource_name = 'some_dataframe',
             resource_description = 'This is a resource example')

This dataframe will be available for download as .csv file.

Plotly figures

To add a Plotly figure:

# Creating plotly figure
fig <-  plot_ly(df, x = ~country, y = ~numberOf, type = 'bar')

add_fig(dp = dp,
        fig = fig,
        title = 'Some plotly figure', 
        description = 'This is a plotly figure example')

To create a Plotly figure in R, see Plotly.

Publishing

Finally to publish the datapackage:

publish(dp)

For NAV employees

Contact us in #data-catalog-intern



navikt/dataverkr documentation built on Dec. 22, 2021, 12:03 a.m.