#' Fetches all available area names for properties
#'
#' @return dataframe \describe{
#' \item{area_name}{Area name}
#' }
#'
#' @examples
#'
#' fetch_property_area_name()
#'
#' @export
fetch_property_area_name <- function() {
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
"/property/area_name")
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json())
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
check_error(resp)
tryCatch({
return(parsed)
},
error=function(error_message) {
return(data.frame())
})
}
#' Fetches all available house types for properties
#'
#' @return dataframe \describe{
#' \item{house_type}{House type}
#' }
#'
#' @examples
#'
#' fetch_property_house_type()
#'
#' @export
fetch_property_house_type <- function() {
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
"/property/house_type")
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json())
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
check_error(resp)
tryCatch({
return(parsed)
},
error=function(error_message) {
return(data.frame())
})
}
#' Fetches all available postal codes for properties
#'
#' @return dataframe \describe{
#' \item{postal_code}{Postal code}
#' }
#'
#' @examples
#'
#' fetch_property_postal_code()
#'
#' @export
fetch_property_postal_code <- function() {
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
"/property/postal_code")
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json())
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
check_error(resp)
tryCatch({
return(parsed)
},
error=function(error_message) {
return(data.frame())
})
}
#' Title
#'
#' @param area_name
#' @param street_name
#'
#' @return dataframe \describe{
#' \item{property_id}{Unique identifier for property, !!can change between days!!}
#' \item{street_name}{Street name}
#' \item{area_name}{Area name}
#' \item{postal_code}{Postal code}
#' \item{house_type}{House type}
#' \item{square_meters}{Sqare meters m2}
#' \item{year_built}{Year the property was build}
#' \item{rooms}{Total number of rooms}
#' \item{bathrooms}{Number of bathrooms}
#' \item{bedrooms}{Number of bedrooms}
#' \item{price}{Price in kr.}
#' \item{date}{Date advertised}
#' }
#'
#' @examples
#' fetch_property_sale_aggregate('house_type.area_name', area_name = 'hafnarfjörður')
#'
#' @export
fetch_property_sale_aggregate <- function(by = c('area_name', 'house_type', 'postal_code', 'house_type.area_name', 'house_type.postal_code',
'house_type.area_name.postal_code', 'house_type.area_name.year_built'),
area_name = '',
house_type = c('fjölbýlishús', 'einbýlishús', 'par/raðhús', 'hæð'),
frequency = c('day', 'week', 'month', NULL),
from = '2017-10-01', to = NULL) {
# Check input arguments
collection <- checkmate::makeAssertCollection()
checkmate::assertString(by, na.ok = FALSE, min.chars = 0, add = collection)
checkmate::assertString(area_name, na.ok = FALSE, min.chars = 0, add = collection)
checkmate::reportAssertions(collection)
if (!is.null(house_type)) house_type <- match.arg(house_type)
frequency <- match.arg(frequency)
by <- match.arg(by)
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
paste0("/property/sale/", gsub('.', '/', by, fixed = T)))
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json(),
query = list(house_type = tolower(house_type),
area_name = tolower(area_name),
frequency = tolower(frequency),
from = from,
to = to))
check_error(resp)
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
#* Stop if errors
if (httr::http_error(resp)) {
stop(
sprintf(
"DataHub API request failed [%s]\n%s\n<%s>",
httr::status_code(resp),
parsed$message,
parsed$errors
),
call. = FALSE
)
}
tryCatch({
#parsed <- unnest(parsed)
parsed$date <- as.Date(parsed$date)
return(parsed)
},
error=function(error_message) {
return(setNames(data.frame(matrix(ncol = 12, nrow = 0)),
c("house_type", "area_name", "year_built", "postal_code",
"date", "price", "stddev_price",
"sqm_price", "stddev_sqm_price",
"sqm", "stddev_sqm", "total")))
})
}
#' Title
#'
#' @param area_name
#' @param street_name
#'
#' @return dataframe \describe{
#' \item{property_id}{Unique identifier for property, !!can change between days!!}
#' \item{street_name}{Street name}
#' \item{area_name}{Area name}
#' \item{postal_code}{Postal code}
#' \item{house_type}{House type}
#' \item{square_meters}{Sqare meters m2}
#' \item{year_built}{Year the property was build}
#' \item{rooms}{Total number of rooms}
#' \item{bathrooms}{Number of bathrooms}
#' \item{bedrooms}{Number of bedrooms}
#' \item{price}{Price in kr.}
#' \item{date}{Date advertised}
#' }
#'
#' @examples
#' fetch_property_sale(area_name = 'hafnarfjörður')
#'
#' @export
fetch_property_sale <- function(area_name = '', street_name = '') {
# Check input arguments
collection <- checkmate::makeAssertCollection()
checkmate::assertString(area_name, na.ok = FALSE, min.chars = 0, add = collection)
checkmate::assertString(street_name, na.ok = FALSE, min.chars = 0, add = collection)
checkmate::reportAssertions(collection)
if (area_name == '' && street_name == '') {
stop(
sprintf(
"Either area_name or street_name must be filled."
),
call. = FALSE
)
}
if (street_name != '' && nchar(street_name) < 3) {
stop(
sprintf(
"street_name must contain a string longer than 2."
),
call. = FALSE
)
}
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
"/property/sale/")
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json(),
query = list(street_name = tolower(street_name), area_name = tolower(area_name)))
check_error(resp)
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
#* Stop if errors
if (httr::http_error(resp)) {
stop(
sprintf(
"DataHub API request failed [%s]\n%s\n<%s>",
httr::status_code(resp),
parsed$message,
parsed$errors
),
call. = FALSE
)
}
tryCatch({
parsed <- unnest(parsed)
parsed$date <- as.Date(parsed$date)
return(parsed)
},
error=function(error_message) {
return(setNames(data.frame(matrix(ncol = 11, nrow = 0)), c("property_id", "street_name", "area_name", "postal_code", "house_type", "square_meters", "rooms", "bedrooms", "bathrooms", "price", "date")))
})
}
#' Title
#'
#' @param area_name
#' @param street_name
#'
#' @return dataframe \describe{
#' \item{property_id}{Unique identifier for property, !!can change between days!!}
#' \item{street_name}{Street name}
#' \item{area_name}{Area name}
#' \item{postal_code}{Postal code}
#' \item{house_type}{House type}
#' \item{square_meters}{Sqare meters m2}
#' \item{year_built}{Year the property was build}
#' \item{rooms}{Total number of rooms}
#' \item{bathrooms}{Number of bathrooms}
#' \item{bedrooms}{Number of bedrooms}
#' \item{price}{Rent price in kr.}
#' \item{date}{Date advertised}
#' }
#'
#' @examples
#' fetch_property_rent(area_name = 'hafnarfjörður')
#'
#' @export
fetch_property_rent <- function(area_name = '', street_name = '') {
# Check input arguments
collection <- checkmate::makeAssertCollection()
checkmate::assertString(area_name, na.ok = FALSE, min.chars = 0, add = collection)
checkmate::assertString(street_name, na.ok = FALSE, min.chars = 0, add = collection)
checkmate::reportAssertions(collection)
if (area_name == '' && street_name == '') {
stop(
sprintf(
"Either area_name or street_name must be filled."
),
call. = FALSE
)
}
if (street_name != '' && nchar(street_name) < 3) {
stop(
sprintf(
"street_name must contain a string longer than 2."
),
call. = FALSE
)
}
# Create url
url <- paste0(
getOption("base_path", default = "https://api.datahub.is"),
"/property/rent/")
# Make API call and parse
resp <- httr::GET(
url,
httr::add_headers("Authorization" = getOption("authentication_key", default = "")),
httr::accept_json(),
query = list(street_name = tolower(street_name), area_name = tolower(area_name)))
check_error(resp)
parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
#* Stop if errors
if (httr::http_error(resp)) {
stop(
sprintf(
"DataHub API request failed [%s]\n%s\n<%s>",
httr::status_code(resp),
parsed$message,
parsed$errors
),
call. = FALSE
)
}
tryCatch({
parsed <- unnest(parsed)
parsed$date <- as.Date(parsed$date)
return(parsed)
},
error=function(error_message) {
return(setNames(data.frame(matrix(ncol = 11, nrow = 0)), c("property_id", "street_name", "area_name", "postal_code", "house_type", "square_meters", "rooms", "bedrooms", "bathrooms", "price", "date")))
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.