Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRAN feedbacks #14

Merged
merged 5 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(enrich_traffic)
export(enrich_uptime)
export(filter_agg)
export(get_api_state)
export(get_config_path)
export(get_custom_palette)
export(get_graph_subtitles)
export(get_segment_name)
Expand Down
8 changes: 3 additions & 5 deletions R/api_state.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@
#' @importFrom httr VERB
#'
#' @examples
#' \dontrun{
#' my_token <- 'ThisIsNotAValidToken'
#' get_api_state(my_token)
#' }
get_api_state <- function(key = get_telraam_token()){
file_path = "inst/config.yml"
file_path = get_config_path()
if(!file.exists(file_path)){
create_config()
create_config(create_directory = FALSE)
}
key <- c(
'X-Api-Key' = key
)
VERB("GET", url = config::get(file = "inst/config.yml")$url,
VERB("GET", url = config::get(file = file_path)$url,
add_headers(key))$status_code == 200 # the request suceeded if equal to 200
}

Expand Down
87 changes: 74 additions & 13 deletions R/create_config.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#' Create config file if needed.
#'
#' If the user needs to specify the IDs of his own sensors,
#' If you want to specify the IDs of your own sensors,
#' this function create a local configuration template file
#' in the `\inst` directory, to edit with specific information.
#' By default, the function doesn't create the file in the project directory
#' but in a temp directory. If you want to have a permanent configuration,
#' please use `r create_directory = TRUE`.
#'
#' @param overwrite If the file exist, should it be overwriten?
#' If you use the temporary options, please fill directly the name and number
#' of your sensors in the "segments" argument.
#'
#' @param segments Named List of segments ("name1" = "9000000000", ...). Default to the example version.
#' @param create_directory Boolean: Does the file need to be created in the project directory? Default to FALSE.
#' @param overwrite Boolean: if the file exist, should it be overwriten? Default to FALSE.
#'
#' @return Boolean: TRUE if the file is created, FALSE overwise (config already exists for example).
#'
Expand All @@ -13,25 +21,78 @@
#' @importFrom yaml write_yaml
#'
#' @examples
#' \dontrun{ #run if you want to create a inst/ directory containing config file
#' create_config()
#' }
create_config <- function(overwrite=FALSE){
file_path = "inst/config.yml"
#' create_config(create_directory=FALSE)
#' list_of_segments = list("Burel"= "9000002156", "Vitre" = "9000001844")
#' create_config(segments = list_of_segments,
#' create_directory = FALSE,
#' overwrite = TRUE) # the file already exists
create_config <- function(segments = list("segment-01"="9000000000",
"segment-02"="9000000000"),
create_directory=FALSE,
overwrite=FALSE){
# parameters
project_folder = "inst/"
config_name = "config.yml"
template <- list("default"=list("url"="https://telraam-api.net/v1",
"segments"=list("segment-01"="9000000000",
"segment-02"="9000000000")))
if(!file.exists(file_path) | overwrite){
if(!dir.exists("inst/")){
dir.create("inst/")
"segments"=segments))

if(!create_directory){ # if you don't want to create a directory in the project
temp_folder = tempdir() # temp directory
file_path = paste(temp_folder, config_name, sep = "/")
}
else{
if(!dir.exists(project_folder)){
dir.create(project_folder)
}
file_path = paste(project_folder, config_name, sep = "")
}
if(!file.exists(file_path) | overwrite){
file.create(file_path)
yaml::write_yaml(template, file_path)
result = TRUE
}
else {
warning("A configuration file already exists in the 'inst' directory")
warning("A configuration file already exists in the directory")
result = FALSE
}
return(result)
}

#' Get the path of configuration file.
#'
#' The configuration file could be in the `inst` directory, or in a temporary
#' directory, according to the `create_directory` option of the `create_config()`
#' function. If the configuration file doesn't exist, this function create a file
#' in a temporary directory and send a message to the user.
#'
#' @return Character: path of the configuration file, needed for a lot of functions.
#'
#' @export
#'
#' @keywords internal
#'
#' @examples
#' create_config(create_directory=FALSE)
#' get_config_path()
get_config_path <- function(){
project_folder = "inst/"
tmp_folder = paste(tempdir(), "/", sep = "")
config_name = "config.yml"
if(dir.exists(project_folder)){
project_file = paste(project_folder, config_name, sep = "")
if(file.exists(project_file)) {
return(project_file)
}
}
if(dir.exists(tmp_folder)){
tmp_file = paste(tmp_folder, config_name, sep = "")
if(file.exists(tmp_file)) {
return(tmp_file)
}
} else {
create_config()
message("Creation of a default config file in the temp directory, please use create_config() to overwrite.")
tmp_file = paste(tmp_folder, config_name, sep = "")
return(tmp_file)
}
}
10 changes: 8 additions & 2 deletions R/data_import.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @export
#'
#' @examples
#' \dontrun{
#' \dontrun{ # This example requires a valid API key
#' period <- as.Date(c('2022-01-01', '2022-12-31'))
#' write_update_data('RteVitre-06', period[1], period[2])
#' write_update_data('ParisArcEnCiel-05', period[1], period[2])
Expand All @@ -30,8 +30,14 @@ import_sensor <- function(list_sensor){
}
}

if(dir.exists('data/')){
folder_path = 'data/'
} else {
folder_path = paste(tempdir(), '/', sep = "")
}

data <- map_dfr(list_sensor, ~ {
file <- paste0('data/', .x, '.RData')
file <- paste0('folder_path', .x, '.RData')
if (file.exists(file)) {
# we select the data that we don't consider null (arbitrary choice)
import <- load(file)
Expand Down
7 changes: 4 additions & 3 deletions R/data_retrieve.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @export
#'
#' @examples
#' \dontrun{
#' \dontrun{ # This function requires a valid API key
#' period <- as.Date(c('2022-01-01', '2022-12-31'))
#' retrieve_sensor('RteVitre-06', period[1], period[2])
#' }
Expand All @@ -32,7 +32,7 @@ retrieve_sensor <- function(segment_name,start_date,end_date, key = get_telraam_
# Get Segment_id
if(!is.numeric(segment_name)){
segment_id <- get_segments()[segment_name]
if(is.na(segment_id)){
if(is.na(names(segment_id))){
stop("Segment name unknown")
}
}
Expand All @@ -41,7 +41,8 @@ retrieve_sensor <- function(segment_name,start_date,end_date, key = get_telraam_
}

# calling of the API
traffic_url <- paste(config::get(file = "inst/config.yml")$url,
config_file = get_config_path()
traffic_url <- paste(config::get(file = config_file)$url,
'/reports/traffic', sep='')
resTraffic_list <- pmap(list(dates$start, dates$end), ~ {
resTraffic <- POST(traffic_url,
Expand Down
20 changes: 15 additions & 5 deletions R/data_write_update.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#' Write or update the sensor data in the data folder
#'
#' Writes or updates the sensor data in the data folder. It retrieves the data for the specified sensor between \code{start_date} and \code{end_date} (inclusive) using the \code{retrieve_sensor} function, and then converts certain columns to character strings before writing the data to a RData file in the data folder.
#' Writes or updates the sensor data in the data folder.
#' It retrieves the data for the specified sensor between \code{start_date}
#' and \code{end_date} (inclusive) using the \code{retrieve_sensor} function,
#' and then converts certain columns to character strings before writing the data
#' to a RData file in the `data` folder (if `create_directory = TRUE`),
#' to a temporary folder otherwise.
#'
#' @param segment_name Character. Name of the segment, as specified in config.
#' @param start_date Date. Start date "aaaa-mm-jj"
#' @param end_date Date. End date "aaaa-mm-jj"
#' @param create_directory Boolean: Does the file need to be created in the project directory? Default to FALSE.
#'
#' @return Boolean: TRUE if the data is well saved/written, FALSE otherwise (no data for example)
#'
Expand All @@ -13,12 +19,12 @@
#' @export
#'
#' @examples
#' \dontrun{
#' \dontrun{ # This function requires a valid API key
#' period <- as.Date(c('2022-01-01', '2022-12-31'))
#' write_update_data('RteVitre-06', period[1], period[2])
#' }
#'
write_update_data <- function(segment_name, start_date, end_date){
write_update_data <- function(segment_name, start_date, end_date, create_directory = FALSE){

# Preparation of the dataset
data <- retrieve_sensor(segment_name,start_date, end_date)
Expand All @@ -33,12 +39,16 @@ write_update_data <- function(segment_name, start_date, end_date){
data$car_speed_hist_0to70plus <- sapply(data$car_speed_hist_0to70plus, function(x) paste(x, collapse = ", "))
data$car_speed_hist_0to120plus <- sapply(data$car_speed_hist_0to120plus, function(x) paste(x, collapse = ", "))

file_name <- paste0("data/",segment_name,".RData")


if (!is.null(data)){
if(!dir.exists("data/")){
if(!create_directory){
folder_path = paste(tempdir(), "/", sep = "")
} else if(!dir.exists("data/")){
dir.create("data/")
folder_path = "data/"
}
file_name <- paste0(folder_path, segment_name, ".RData")
if (file.exists(file_name)){
cleaning <- get(load(file_name))
data <- rbind(cleaning,data)
Expand Down
2 changes: 0 additions & 2 deletions R/plot_car_speed.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,12 @@ gg_car_speed_v85 <- function(enriched_data,
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' preprocess_car_speed(traffic,
#' date_range = c('2022-01-01','2022-03-01'),
#' segments = 'RteVitre-06',
#' weekday = c('monday','sunday'),
#' hours = 17:20,
#' aggregated_by = "weekday")
#' }
#'
preprocess_car_speed <- function(enriched_data,
aggregated_by,
Expand Down
11 changes: 4 additions & 7 deletions R/utils_global.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,18 @@ set_global_vars <- function(vacations = NULL, public_holidays = NULL){
#' @export
#'
#' @examples
#' \dontrun{ #run if you want to create a inst/ directory containing config file
#' create_config()
#' get_segments()
#' }
#' create_config(create_directory = FALSE)
#' get_segments()
get_segments <- function(){
file_path = "inst/config.yml"
file_path = get_config_path()
if(!file.exists(file_path)){
segments <- NULL
} else {
segments <- config::get(file = "inst/config.yml")$segments
segments <- config::get(file = file_path)$segments
}
return(segments)
}


#' Get the name of a segment giving its id
#'
#' @param segment_id ID of segment, should be present in inst/config.yml
Expand Down
9 changes: 9 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Resubmission
This is a resubmission. In this version I have:

* Removed unnecessary dontrun wrappers, except for the API calls that
require an API key.

* Modified the write functions so that they do not write to the user's
directory by default.

## R CMD check results

0 errors | 0 warnings | 1 note
Expand Down
25 changes: 16 additions & 9 deletions docs/articles/data-details.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pkgdown_sha: ~
articles:
data-details: data-details.html
data-visualization: data-visualization.html
last_built: 2024-05-22T13:39Z
last_built: 2024-05-26T11:35Z
urls:
reference: https://ketsiaguichard.github.io/telraamStats/reference
article: https://ketsiaguichard.github.io/telraamStats/articles
Expand Down
Loading
Loading