Skip to content

Commit

Permalink
Merge pull request #46 from gongcastro/test
Browse files Browse the repository at this point in the history
Format code
  • Loading branch information
gongcastro authored Aug 11, 2024
2 parents 85a35e5 + 76ba20b commit 795ebc7
Show file tree
Hide file tree
Showing 21 changed files with 1,161 additions and 1,078 deletions.
80 changes: 40 additions & 40 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,43 @@ export(prop_adj)
export(track_progress)
import(dplyr)
import(rlang)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert_success)
importFrom(cli,cli_alert_warning)
importFrom(cli,cli_progress_done)
importFrom(cli,cli_progress_step)
importFrom(cli,cli_progress_update)
importFrom(cli,qty)
importFrom(dplyr,across)
importFrom(dplyr,case_when)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,if_any)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(formr,formr_connect)
importFrom(formr,formr_raw_results)
importFrom(googlesheets4,gs4_auth)
importFrom(googlesheets4,gs4_has_token)
importFrom(googlesheets4,read_sheet)
importFrom(janitor,make_clean_names)
importFrom(lifecycle,deprecated)
importFrom(lubridate,as_date)
importFrom(lubridate,as_datetime)
importFrom(lubridate,time_length)
importFrom(lubridate,today)
importFrom(magrittr,"%>%")
importFrom(purrr,map)
importFrom(purrr,set_names)
importFrom(readxl,excel_sheets)
importFrom(readxl,read_xlsx)
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,as_label)
importFrom(rlang,as_name)
importFrom(rlang,enquo)
importFrom(rlang,enquos)
importFrom(tibble,tibble)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,pivot_wider)
importFrom(utils,browseURL)
importFrom(cli, cli_abort)
importFrom(cli, cli_alert_success)
importFrom(cli, cli_alert_warning)
importFrom(cli, cli_progress_done)
importFrom(cli, cli_progress_step)
importFrom(cli, cli_progress_update)
importFrom(cli, qty)
importFrom(dplyr, across)
importFrom(dplyr, case_when)
importFrom(dplyr, distinct)
importFrom(dplyr, filter)
importFrom(dplyr, if_any)
importFrom(dplyr, mutate)
importFrom(dplyr, n)
importFrom(formr, formr_connect)
importFrom(formr, formr_raw_results)
importFrom(googlesheets4, gs4_auth)
importFrom(googlesheets4, gs4_has_token)
importFrom(googlesheets4, read_sheet)
importFrom(janitor, make_clean_names)
importFrom(lifecycle, deprecated)
importFrom(lubridate, as_date)
importFrom(lubridate, as_datetime)
importFrom(lubridate, time_length)
importFrom(lubridate, today)
importFrom(magrittr, "%>%")
importFrom(purrr, map)
importFrom(purrr, set_names)
importFrom(readxl, excel_sheets)
importFrom(readxl, read_xlsx)
importFrom(rlang, ":=")
importFrom(rlang, .data)
importFrom(rlang, as_label)
importFrom(rlang, as_name)
importFrom(rlang, enquo)
importFrom(rlang, enquos)
importFrom(tibble, tibble)
importFrom(tidyr, pivot_longer)
importFrom(tidyr, pivot_wider)
importFrom(utils, browseURL)
1 change: 0 additions & 1 deletion R/compat-purrr.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ reduce <- function(.x, .f, ..., .init) {
f <- function(x, y) .f(x, y, ...)
Reduce(f, .x, init = .init)
}

114 changes: 57 additions & 57 deletions R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,69 @@
#' it is assumed to be the same as `formr_email`.
#' @param password Character string with the password to formr (`NULL` by
#' default).
#'
#'
#' @return Logical. `TRUE` if Google and formr authentication was successful,
#' `FALSE` if authentication of any of the two failed.
#'
#'
#' @examples
#' \dontrun{bvq_connect()}
#'
#' \dontrun{
#' bvq_connect()
#' }
#'
bvq_connect <- function(google_email = NULL,
password = NULL) {
formr_email <- "[email protected]"

# ask for email in console is everything is NULL
if (is.null(google_email)) google_email <- formr_email

if (is.null(password)) {
password <- Sys.getenv("FORMR_PWD", unset = NA)
if (is.na(password)) {
cli_abort("Please, provide a password")
}
formr_email <- "[email protected]"

# ask for email in console is everything is NULL
if (is.null(google_email)) google_email <- formr_email

if (is.null(password)) {
password <- Sys.getenv("FORMR_PWD", unset = NA)
if (is.na(password)) {
cli_abort("Please, provide a password")
}

# if key exists, use it to log in
tryCatch(
suppressWarnings(
formr::formr_connect(
email = formr_email,
password = password,
host = "https://formr.org/"
)
),
error = function(e) {
cli_abort(
strwrap(
prefix = " ",
initial = "",
"Could not connect to {.url https://formr.org/}. Please check your internet connection or make sure you have set the right formr password in a {.file .Renviron} file, under the name {.envvar FORMR_PWD}."
)
)
}
)

# check if Google credentials exists, ask for them if not
if (!gs4_has_token()) {
tryCatch(
suppressWarnings({
googlesheets4::gs4_auth(
email = google_email,
token = Sys.getenv("GOOGLE_TOKEN", unset = NA)
)
}),
error = function(e) {
cli_abort(
strwrap(
prefix = " ",
initial = "",
"Could not connect to Google.\
Please check your internet connection or \
grant the necessary permissions."
)
)
}
}

# if key exists, use it to log in
tryCatch(
suppressWarnings(
formr::formr_connect(
email = formr_email,
password = password,
host = "https://formr.org/"
)
),
error = function(e) {
cli_abort(
strwrap(
prefix = " ",
initial = "",
"Could not connect to {.url https://formr.org/}. Please check your internet connection or make sure you have set the right formr password in a {.file .Renviron} file, under the name {.envvar FORMR_PWD}."
)
)
}

invisible(googlesheets4::gs4_has_token())
)

# check if Google credentials exists, ask for them if not
if (!gs4_has_token()) {
tryCatch(
suppressWarnings({
googlesheets4::gs4_auth(
email = google_email,
token = Sys.getenv("GOOGLE_TOKEN", unset = NA)
)
}),
error = function(e) {
cli_abort(
strwrap(
prefix = " ",
initial = "",
"Could not connect to Google. Please check your internet connection or grant the necessary permissions."
)
)
}
)
}

invisible(googlesheets4::gs4_has_token())
}
46 changes: 23 additions & 23 deletions R/items.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#' @importFrom readxl excel_sheets
#' @importFrom readxl read_xlsx
#' @importFrom cli cli_abort
#'
#'
#' @export bvq_items
#'
#' @param section Name of the questionnaire section to retrieve items for. Check the output of `get_bvq_runs()` to see the available sections for each version of the questionnaire.
#' @param section Name of the version of the questionnaire for which the items of a section will be retrieved. Check the output of `names(get_bvq_runs())` to see the available versions of the questionnaire.
#'
#'
#' @returns A list of length 3, which includes:
#' * survey: A [tibble::tibble] containing the items included in the questionnaire and several properties. Each row corresponds to a single item, and each column corresponds to a particular property:
#' - type: a character string indicating the type of the item (see [formr documentation](https://formr.org/documentation#available_items)).
Expand All @@ -29,31 +29,31 @@
#' - item: name of the setting.
#' - value: value of the setting.
#' @author Gonzalo Garcia-Castro
#'
#'
#' @examples
#' \dontrun{
#' bvq_items("bvq_06_words_catalan", version = "bvq-1.0.0")
#' }
#'
#'
#' @md
bvq_items <- function(section, version = "bvq-1.0.0") {
runs <- get_bvq_runs()
if (!(version %in% names(runs))) {
cli::cli_abort("Invalid {.field version} name '{version}'. See {.fn get_bvq_runs} for valid options")
}

valid_sections <- runs[[version]]
if (!(section %in% valid_sections)) {
cli::cli_abort("Invalid {.field section} '{section}'. See {.fn get_bvq_runs} for valid options")
}

path <- system.file(file.path("formr", version, paste0(section, ".xlsx")),
package = "bvq",
mustWork = TRUE)
sheets <- readxl::excel_sheets(path)
q <- lapply(sheets, \(x) readxl::read_xlsx(path, sheet = x))
names(q) <- sheets

return(q)
}
runs <- get_bvq_runs()
if (!(version %in% names(runs))) {
cli::cli_abort("Invalid {.field version} name '{version}'. See {.fn get_bvq_runs} for valid options")
}

valid_sections <- runs[[version]]
if (!(section %in% valid_sections)) {
cli::cli_abort("Invalid {.field section} '{section}'. See {.fn get_bvq_runs} for valid options")
}

path <- system.file(file.path("formr", version, paste0(section, ".xlsx")),
package = "bvq",
mustWork = TRUE
)
sheets <- readxl::excel_sheets(path)
q <- lapply(sheets, \(x) readxl::read_xlsx(path, sheet = x))
names(q) <- sheets

return(q)
}
Loading

0 comments on commit 795ebc7

Please sign in to comment.