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

Develop #36

Merged
merged 14 commits into from
May 17, 2024
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
^revdep$
clasificacion_ui
test_recipes.R
test.R
test.R
recetas_ech.R
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Imports:
bit64 (>= 4.0.5),
httr (>= 1.4.7),
lifecycle (>= 1.0.0),
jsonlite (>= 1.7.2),
R6 (>= 2.5.0),
emoji (>= 15.0),
visNetwork (>= 2.0.9)
Expand Down
10 changes: 10 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ export(default_engine)
export(get_data)
export(get_engine)
export(get_metadata)
export(get_recipe)
export(get_steps)
export(load_survey)
export(load_survey_example)
export(read_recipe)
export(recipe)
export(save_recipe)
export(set_engine)
export(set_use_copy)
export(show_engines)
export(step_compute)
export(step_recode)
export(steps_to_recipe)
export(survey_empty)
export(survey_to_data.table)
export(survey_to_data_frame)
Expand Down Expand Up @@ -39,6 +44,11 @@ importFrom(glue,glue_col)
importFrom(glue,identity_transformer)
importFrom(httr,GET)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(jsonlite,parse_json)
importFrom(jsonlite,read_json)
importFrom(jsonlite,write_json)
importFrom(lifecycle,deprecated)
importFrom(visNetwork,addFontAwesome)
importFrom(visNetwork,visEdges)
Expand Down
256 changes: 256 additions & 0 deletions R/Recipes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
Recipe <- R6Class("Recipe",
public = list(
name = NULL,
edition = NULL,
survey_type = NULL,
default_engine = NULL,
depends_on = list(),
user = NULL,
description = NULL,
steps = list(),
initialize = function(name, edition, survey_type, default_engine, depends_on, user, description, steps) {
self$name <- name
self$edition <- edition
self$survey_type <- survey_type
self$default_engine <- default_engine
self$depends_on <- depends_on
self$user <- user
self$description <- description
self$steps <- steps
}
)
)




metadata_recipe <- function() {
return(
c(
"name",
"user",
"svy",
"description"
)
)
}

#' Recipe
#' @export
#' @param ... A list with the following metadata: name, user, svy, description
#' @keywords Survey methods
#' @keywords Recipes
#' @return A Recipe object

recipe <- function(...) {

dots <- substitute(list(...))

metadata_recipes_names <- metadata_recipe()

check_args = sum(metadata_recipes_names %in% names(dots))



if (!(check_args == length(metadata_recipes_names))) {
stop(
message(
"The recipe must have the following metadata: ",
paste(metadata_recipe(), collapse = ", ")
)
)
}

index_steps <- which(names(dots) %in% metadata_recipe())


if ("steps" %in% names(dots)) {
return(
Recipe$new(
name = dots$name,
user = dots$user,
edition = dots$svy$edition,
survey_type = dots$svy$type,
default_engine = default_engine(),
depends_on = list(),
description = dots$description,
steps = eval(dots$steps)
)
)
} else {
return(
Recipe$new(
name = dots$name,
user = dots$user,
edition = dots$svy$edition,
survey_type = dots$svy$type,
default_engine = default_engine(),
depends_on = list(),
description = dots$description,
steps = dots[-index_steps]
)
)
}
}

#' Encoding and decoding recipes
#' @param recipe A Recipe object
#' @return A Recipe object
#' @keywords internal
#' @noRd

encoding_recipe <- function(recipe) {

recipe$steps <- lapply(recipe$steps, function(step) {
step_string <- deparse(step)
return(step_string)
})

return(recipe)
}

#' Encoding and decoding recipes
#' @param recipe A Recipe object
#' @return A Recipe object
#' @keywords internal
#' @noRd

decode_recipe <- function(recipe) {
recipe$steps <- as.call(
lapply(
recipe$steps,
function(step_string) as.call(parse(text = step_string))[[1]]
)
)

return(
recipe
)
}

#' Save a recipe to a file
#' @param recipe A Recipe object
#' @param file A character string with the file path
#' @importFrom jsonlite write_json
#' @return NULL
#' @keywords Survey methods
#' @keywords Recipes
#' @export

save_recipe <- function(recipe, file) {

recipe = list(
name = recipe$name,
user = recipe$user,
svy_type = recipe$survey_type,
edition = recipe$edition,
description = recipe$description,
steps = recipe$steps
)

recipe |>
encoding_recipe() |>
write_json(path = file, simplifyVector = TRUE)

message(
glue::glue("The recipe has been saved in {file}")
)
}

#' Load a recipe from a file
#' @param file A character string with the file path
#' @importFrom jsonlite read_json
#' @return A Recipe object
#' @keywords Survey methods
#' @keywords Recipes
#' @export

read_recipe <- function(file) {

file |>
read_json(simplifyVector = TRUE) |>
decode_recipe()

}

#' Get a recipe from the API
#' @param topic A character string with the topic of the recipe
#' @param svy_type A character string with the survey type of the recipe
#' @param svy_edition A character string with the survey edition of the recipe
#' @importFrom httr POST
#' @importFrom jsonlite parse_json
#' @importFrom httr content
#' @importFrom httr add_headers
#' @return A Recipe object
#' @keywords Survey methods
#' @keywords Recipes
#' @export

get_recipe <- function(
svy_type = NULL,
svy_edition = NULL,
topic = NULL
) {

filterList = list(
svy_type = svy_type,
svy_edition = svy_edition,
topic = topic
)

filterList <- filterList[!sapply(filterList, is.null)]

baseUrl = "https://sa-east-1.aws.data.mongodb-api.com/app/data-vonssxi/endpoint/data/v1/action/"

url = paste0(
baseUrl,
"findOne"
)

headers <- c(
"Content-Type" = "application/json",
"Access-Control-Request-Headers" = "*",
"api-key" = "MKwpkpQCX1meBSN6jmsS5XpIiPvJgfOdxzjinsDC83AX5Mx18j3o16cdhtgPYXQj"
)

body <- list(
collection = "recipes",
database = "metasurvey",
dataSource = "Cluster0",
filter = filterList
)

response <- POST(
url,
body = body,
encode = "json",
add_headers(.headers = headers)
)

recipe <- decode_recipe(parse_json(content(response, "text", encoding = "UTF-8"))[['document']])
# return(content(response, "text", encoding = "UTF-8"))
return(recipe)
}

#' Convert a list of steps to a recipe
#' @param steps A list with the steps of the recipe
#' @return A Recipe object
#' @keywords Survey methods
#' @keywords Recipes
#' @export

steps_to_recipe <- function(steps) {
return(
recipe(
user = "Mauro Loprete",
svy = survey_empty(type = "eaii", edition = "2019-2021"),
description = "Receta para la encuesta de Actividad Industrial, Comercial y de Servicios",
steps = unname(lapply(
steps,
function(step) {
unname(step$call)
}
))
)
)
}
4 changes: 3 additions & 1 deletion R/Step.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Step <- R6Class("Step",
svy_before = NULL,
default_engine = NULL,
depends_on = list(),
initialize = function(name, edition, survey_type, type, new_var, exprs, call, svy_before, default_engine, depends_on) {
comments = NULL,
initialize = function(name, edition, survey_type, type, new_var, exprs, call, svy_before, default_engine, depends_on, comments) {
self$name <- name
self$edition <- edition
self$survey_type <- survey_type
Expand All @@ -21,6 +22,7 @@ Step <- R6Class("Step",
self$svy_before <- svy_before
self$default_engine <- default_engine
self$depends_on <- depends_on
self$comments <- comments
}
)
)
3 changes: 3 additions & 0 deletions R/metaSurvey-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
#' @importFrom bit64 integer64
## usethis namespace: end
NULL


utils::globalVariables(c("j"))
Loading
Loading