Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
Example code that turns the human readable markdown (.md file) into a machine suitable csv file.
  • Loading branch information
thoughtfulbloke authored Oct 18, 2018
1 parent 4aaa0a9 commit 46ec99a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions example_csv_maker.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# by David Hood @thoughtfulnz on Twitter

library(dplyr)
# dplyr is a R helper library for structuring the processing of data
library(tidyr)
# tidyr is a R helper library for data restructuring

# Broad plan:
# read in the lines as 1 line per observation in a vairable
# ID entries by the ``` between them
# split the observations by the colon
Expand All @@ -12,12 +15,22 @@ library(tidyr)
# use the column of headings as common headings and the details underneath the headings
# save as csv

# Specific steps
# read in the markdown file as the first column of a data frame (tabular data block)
data.frame(rawlines = readLines("dataexamples.md"), stringsAsFactors = FALSE) %>%
# cumulatively count the entries based on the lines containing ```
mutate(entry= cumsum(as.numeric(trimws(rawlines) == "```"))) %>%
# split the text on the basis of the first column
separate(rawlines, into=c("first", "second"), sep=":", extra="merge", fill = "right") %>%
# remove the entries which don't have two parts (so had no colon)
filter(!is.na(second)) %>%
# remove excess whitespace
mutate(heading = trimws(first), details = trimws(second)) %>%
# keep the entry number and the trimmed data
select(entry, heading, details) %>%
# make the entries in the column of headings actual column headings
spread(heading, details) %>%
# don't need the entry numbers anymore
select(-entry) %>%
# save as cav file
write.csv(file = "dataexamples_as_csv.csv", row.names = FALSE)

0 comments on commit 46ec99a

Please sign in to comment.