Skip to content

Commit

Permalink
Style code (GHA)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjentoft committed Nov 15, 2024
1 parent 57d88e3 commit c4d48da
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 83 deletions.
2 changes: 1 addition & 1 deletion R/Hent_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ get_variant_name <- function(variant) {
#' @keywords internal
#' @return text in json format
GetUrl2 <- function(url, check = TRUE) {
# Fetch contents from classfication
# Fetch contents from classfication
if (check) {
hent_klass <- check_connect(url)
} else {
Expand Down
57 changes: 31 additions & 26 deletions R/KLASS.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ MakeChar <- function(x) {
#'
#' @examples
#' data(klassdata)
#' kommune_names <- apply_klass(x = klassdata$kommune,
#' classification = 131,
#' language = "en",
#' format = FALSE
#' )
#' kommune_names <- apply_klass(
#' x = klassdata$kommune,
#' classification = 131,
#' language = "en",
#' format = FALSE
#' )
apply_klass <- function(x,
classification,
date = NULL,
Expand All @@ -45,22 +46,21 @@ apply_klass <- function(x,
output_level = NULL,
output = "name",
format = TRUE) {

# Check and standardise variables
classification <- MakeChar(classification)
if (is.null(x)) {
stop("The input vector is empty.")
}
x <- MakeChar(x)

if (is.null(date)) {
date <- Sys.Date()
}

type <- ifelse(is.null(correspond), "vanlig", "kor")
type <- ifelse(isTRUE(correspond), "change", type)
type <- ifelse(is.null(variant), type, "variant")

# Fetch classification table
klass_data <- get_klass(
classification,
Expand All @@ -70,7 +70,7 @@ apply_klass <- function(x,
language = language,
output_level = NULL
)

# Extract correspondence table
if (type == "kor") {
cor_table <- get_klass(
Expand All @@ -79,7 +79,7 @@ apply_klass <- function(x,
correspond = correspond,
language = language
) # , output_level = output_level)

new_table <- get_klass(
classification = correspond,
date = date,
Expand All @@ -96,23 +96,25 @@ apply_klass <- function(x,
output_level = NULL
)
}

# Formattering - only for nace and municipality
if (format == TRUE & classification %in% c("6", "131")) {
x_formatted <- formattering(x, classification = classification)
} else {
x_formatted <- x
}

# Check input data for level
input_level <- levelCheck(x = x_formatted, klass_data = klass_data) # implies all are same level!
if (is.null(output_level))
if (is.null(output_level)) {
output_level <- input_level

}

if (!all(input_level == output_level) &
type %in% c("kor", "change"))
type %in% c("kor", "change")) {
stop("Level changes and time changes/correspondence concurrently is not programmed.")

}

# Run level function
if (!all(input_level == output_level) & is.null(correspond)) {
x_level <- Levels(
Expand All @@ -121,39 +123,42 @@ apply_klass <- function(x,
klass_data = klass_data
)
}

if (all(input_level == output_level)) {
x_level <- klass_data[klass_data$level == input_level, ]
x_level[, paste("level", input_level, sep = "")] <- x_level$code
x_level[, paste("name", input_level, sep = "")] <- x_level$name
}


# run matching
levelcode <- paste("level", input_level, sep = "")
if (type %in% c("vanlig", "variant")) {
m <- match(x_formatted, x_level[, levelcode]) ### sjekk rekkefolge
}

if (type == "kor") {
m1 <- match(x_level[, levelcode], cor_table[, "sourceCode"])
m2 <- match(x_formatted, cor_table[, "sourceCode"])
m3 <- match(cor_table[, "targetCode"], new_table[, "code"]) ## ?
}

if (type == "change") {
m1 <- match(x_formatted, x_level[, levelcode])
m2 <- match(x_formatted, cor_table$sourceCode)
}

# Choose format output
if (type %in% c("vanlig", "variant")) {
if (output == "code")
if (output == "code") {
vars <- paste("level", output_level, sep = "")
if (output == "name")
}
if (output == "name") {
vars <- paste("name", output_level, sep = "")
if (output == "both")
}
if (output == "both") {
vars <- paste(c("level", "name"), output_level, sep = "")
}
out <- x_level[m, vars]
}
if (type == "kor") {
Expand Down
2 changes: 1 addition & 1 deletion R/UpdateKlass-graph-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ get_klass_changes <- function(classification) {
#' \dontrun{
#' klass_131_2020 <- klass_graph(131, "2020-01-01")
#' }
#'
#'
klass_graph <- function(classification, date = NULL) {
if (is.null(classification)) stop("Please provide a classification ID.")

Expand Down
3 changes: 1 addition & 2 deletions R/UpdateKlass-graph-navigate.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,9 @@ is_combined <- function(graph, node, compare_node = NULL) {
#' # Find the most recent node in the graph representing the code "0101" (Halden,
#' # valid to 2020.)
#' halden_node <- klass_node(klass_131, "0101")
#'
#'
#' # Find the most recent code corresponding to 0101 Halden
#' halden_node_updated <- update_klass_node(klass_131, halden_node)
#'
#'
update_klass_node <- function(graph, node) {
bfs_result <- igraph::bfs(
Expand Down
110 changes: 63 additions & 47 deletions tests/testthat/test_ApplyKlass.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
test_that("apply_klass returns correct names for numeric codes", {
data(klassdata)
kommune_names <- apply_klass(klassdata$kommune,
classification = 131,
date = "2015-01-01")
classification = 131,
date = "2015-01-01"
)
expect_equal(kommune_names[1], "Sandefjord")
})

Expand All @@ -11,17 +12,19 @@ test_that("apply_klass returns correct names for numeric codes", {
test_that("apply_klass returns correct names for numeric codes", {
data(klassdata)
kommune_names <- apply_klass(klassdata$kommune,
classification = 131,
date = "2015-01-01")
classification = 131,
date = "2015-01-01"
)
expect_equal(kommune_names[1], "Sandefjord")
})


test_that("apply_klass returns correct names for character codes", {
data(klassdata)
sektor_names <- apply_klass(c("INNL", "UTL", "B_FIN"),
classification = 39,
date = "2019-01-01")
classification = 39,
date = "2019-01-01"
)
expect_equal(sektor_names[2], "Utlandet")
})

Expand Down Expand Up @@ -74,13 +77,14 @@ test_that("Check levelCheck for character codes", {
test_that("apply_klass can return a variant classification", {
dat <- c("000", "101", "102", "103")
dat <- c("01.21", "01.46", "10.61")

dat_new <- klassR::apply_klass(dat,
classification = 6,
variant = 1616,
date = "2021-01-02")
classification = 6,
variant = 1616,
date = "2021-01-02"
)
expect_equal(dat_new[2], "Svinehold")

dat_new <- klassR::apply_klass(
dat,
classification = 6,
Expand Down Expand Up @@ -114,35 +118,44 @@ test_that("An error is correctly returned in the case of a null vector", {
})


test_that("apply_klass works for classifications with varying number of digits",
{
dat <- c("56", "580")
new <- apply_klass(dat, classification = 270, date = "2024-01-01")
expect_false(new[1] == new[2])
expect_false(all(is.na(new)))
})
test_that("apply_klass works for classifications with varying number of digits", {
dat <- c("56", "580")
new <- apply_klass(dat, classification = 270, date = "2024-01-01")
expect_false(new[1] == new[2])
expect_false(all(is.na(new)))
})


test_that("Nace classification with missing formatting", {
# simple example with all missing formatting
dat <- c("01460", "45112", "45111", "45310")
expect_warning(new <- apply_klass(dat, classification = 6, date = "2024-01-01"),
"Number missing .: 4")
expect_warning(
new <- apply_klass(dat, classification = 6, date = "2024-01-01"),
"Number missing .: 4"
)
expect_equal(new[1], "Svinehold")

# Check mixture of formatting
dat <- c("45112", "45.111")
expect_warning(new <- apply_klass(dat, classification = 6, date = "2024-01-01"),
"Number missing .: 1")
expect_equal(new[1],
"Detaljhandel med biler og lette motorvogner, unntatt motorsykler")

expect_warning(
new <- apply_klass(dat, classification = 6, date = "2024-01-01"),
"Number missing .: 1"
)
expect_equal(
new[1],
"Detaljhandel med biler og lette motorvogner, unntatt motorsykler"
)

# checking NAs
dat <- c("45.112", "45.111", NA)
expect_warning(new <- apply_klass(dat, classification = 6, date = "2024-01-01"),
"Number of NA: 1")
expect_equal(new[1],
"Detaljhandel med biler og lette motorvogner, unntatt motorsykler")
expect_warning(
new <- apply_klass(dat, classification = 6, date = "2024-01-01"),
"Number of NA: 1"
)
expect_equal(
new[1],
"Detaljhandel med biler og lette motorvogner, unntatt motorsykler"
)
expect_true(is.na(new[3]))
})

Expand All @@ -155,35 +168,38 @@ test_that("Municipality classification with missing formatting", {
"Number missing leading 0: 2"
)
expect_equal(new[1], "Oslo")

# simple example with all missing leading 0
dat <- c("301", "301")
expect_warning(
new <- apply_klass(dat, classification = 131, date = "2024-01-01"),
"Number missing leading 0: 2"
)
expect_equal(new[1], "Oslo")


# Check mixture of formatting
dat <- c("45112", "45.111")
expect_warning(new <- apply_klass(dat, classification = 6, date = "2024-01-01"),
"Number missing .: 1")
expect_equal(new[1],
"Detaljhandel med biler og lette motorvogner, unntatt motorsykler")
expect_warning(
new <- apply_klass(dat, classification = 6, date = "2024-01-01"),
"Number missing .: 1"
)
expect_equal(
new[1],
"Detaljhandel med biler og lette motorvogner, unntatt motorsykler"
)
})


test_that("apply_klass works for classifications with varying digits and letters",
{
dat <- c("56", "580", "KG1")
new <- apply_klass(dat, classification = 270, date = "2024-01-01")
expect_false(all(is.na(new)))

dat <- c("01", "03b")
new2 <- apply_klass(dat, classification = 207, date = "2024-01-01")
expect_false(all(is.na(new2)))
})
test_that("apply_klass works for classifications with varying digits and letters", {
dat <- c("56", "580", "KG1")
new <- apply_klass(dat, classification = 270, date = "2024-01-01")
expect_false(all(is.na(new)))

dat <- c("01", "03b")
new2 <- apply_klass(dat, classification = 207, date = "2024-01-01")
expect_false(all(is.na(new2)))
})


test_that("An error is correctly returned in the case of a null vector", {
Expand Down
Loading

0 comments on commit c4d48da

Please sign in to comment.