knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

nottshcOpenData

This package demonstrates some of the ways we use R.

Installation

You can install the developmental version of nottshcOpenData using:

# install.packages("devtools")
devtools::install_github("CDU-data-science-team/nottshcOpenData")

You may also have to set up MySQL on your computer if it's not already installed, see https://db.rstudio.com/best-practices/drivers/ for more information.

Example

This example demonstrates how the nottshcOpenData package can be used to

1. connect to the server using connect_mysql() 1. query data from the database within R Studio using get_px_exp() 1. tidy the data on the database using tidy_px_exp(), and 1. load the data into the global environment using dplyr::collect()

# Load public package
library(nottshcOpenData)

1. Connect to database

# Create MySQL connection
# Note, the driver name, here "MySQL ODBC 8.0 ANSI Driver", may differ on your computer
conn_mysql_suce <- connect_mysql(database = "SUCE",
                                 driver = "MySQL ODBC 8.0 ANSI Driver")

2. Query data

# Get database (db_) connection for specified date range 
db_px_data <- get_px_exp(from = "2020-01-01",
                         to = "2020-12-31")

# Look at messy data
db_px_data

3. Tidy data

# Tidy the data (on the database)
# Next select some variables for this example
db_px_data_tidy <- db_px_data %>% 
  tidy_px_exp() %>% 
  dplyr::select(key, comment_key, date, team_c, team_n, directorate, division,
                category, subcategory, comment_type, comment_txt, crit)

# Look at tidy data
db_px_data_tidy

4. Collect data

In case the data needs to be loaded from the MySQL database into the global environment you can use the collect() function from the dplyr package.

# Collect tidy data
df_px_data_tidy <- db_px_data_tidy %>% 
  dplyr::collect()


CDU-data-science-team/nottshcOpenData documentation built on Dec. 17, 2021, 12:53 p.m.