Skip to content

Commit

Permalink
Apply style suggestions from the lintr and styler packages
Browse files Browse the repository at this point in the history
  • Loading branch information
hleegwater committed Dec 20, 2024
1 parent 87bd128 commit 356b282
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 154 deletions.
30 changes: 16 additions & 14 deletions R/documents.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ document_retrieve <- function(doc_id, api_key = get_api_key()) {
}

document_post <- function(body, api_key = get_api_key()) {
body$tags <- paste0(c("rspacer", body$tags), collapse= ",")
body$tags <- paste0(c("rspacer", body$tags), collapse = ",")
request() |>
httr2::req_url_path_append("documents") |>
httr2::req_headers(`apiKey` = api_key) |>
Expand All @@ -28,7 +28,7 @@ document_post <- function(body, api_key = get_api_key()) {
}

document_replace <- function(body, existing_document_id, api_key = get_api_key()) {
ifelse("rspacer" %in% stringr::str_split_1(body$tags, ","), body$tags, paste0(c("rspacer", body$tags), collapse= ","))
ifelse("rspacer" %in% stringr::str_split_1(body$tags, ","), body$tags, paste0(c("rspacer", body$tags), collapse = ","))
request() |>
httr2::req_url_path_append("documents", parse_rspace_id(existing_document_id)) |>
httr2::req_headers(`apiKey` = api_key) |>
Expand All @@ -45,7 +45,8 @@ document_replace <- function(body, existing_document_id, api_key = get_api_key()
#'
#' Global search for a term
#'
#' Global search for a term, works identically to the simple "All" search in RSpace Workspace. Must be >= 3 characters long.
#' Global search for a term, works identically to the simple "All" search in RSpace Workspace.
#' Must be >= 3 characters long.
#'
#' @param query description
#' @param ... query parameters as documented in
Expand All @@ -71,9 +72,9 @@ document_search <- function(query, ..., api_key = get_api_key()) {
#' @param verbose whether to print the matching document/form
#' @inheritParams document_retrieve
#' @export
doc_to_form_id <- function(doc_id, verbose = T, api_key = get_api_key()) {
doc_to_form_id <- function(doc_id, verbose = TRUE, api_key = get_api_key()) {
json <- document_retrieve(doc_id, api_key)
if(verbose) {
if (verbose) {
cli::cli_inform(c(
"{.field Document}: {json$globalId} ({json$name})",
"{.field Form}:\t {json$form$globalId} ({json$form$name})"
Expand All @@ -98,26 +99,27 @@ doc_to_form_id <- function(doc_id, verbose = T, api_key = get_api_key()) {
#' Returns `FALSE` if no files are attached to the field.
#' @export
document_list_attachments <- function(doc_id, field_id = NULL, field_name = NULL, api_key = get_api_key()) {
if(is.null(doc_id)) cli::cli_abort("Specify the documnt identifier `doc_id`")
if (is.null(doc_id)) cli::cli_abort("Specify the documnt identifier `doc_id`")
# Check field id and/or name
if(is.null(field_id)& is.null(field_name)) cli::cli_abort("Specify `field_id` or `field_name`")
if(!is.null(field_id)& !is.null(field_name)) cli::cli_abort("Specify only `field_id` or `field_name`")
if(!is.null(field_id) & !is.numeric(field_id)) cli::cli_abort("`field_id` should be a number")
if(!is.null(field_name) & !is.character(field_name)) cli::cli_abort("`field_name` should be a string")
if (is.null(field_id) && is.null(field_name)) cli::cli_abort("Specify `field_id` or `field_name`")
if (!is.null(field_id) && !is.null(field_name)) cli::cli_abort("Specify only `field_id` or `field_name`")
if (!is.null(field_id) && !is.numeric(field_id)) cli::cli_abort("`field_id` should be a number")
if (!is.null(field_name) && !is.character(field_name)) cli::cli_abort("`field_name` should be a string")

fields <- doc_get_fields(doc_id)

if(!is.null(field_name)) fields <- dplyr::filter(fields, name == field_name)
if(!is.null(field_id)) fields <- fields[field_id,]
if (!is.null(field_name)) fields <- dplyr::filter(fields, name == field_name)
if (!is.null(field_id)) fields <- fields[field_id, ]

fields |>
dplyr::pull(files) |>
unlist(recursive = F) -> files
# Return FALSE if no files are attached
if(is.null(files)) return(FALSE)
if (is.null(files)) {
return(FALSE)
}

files |>
fields_to_data_frame() -> attachment_list
return(attachment_list)

}
15 changes: 9 additions & 6 deletions R/fields.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

data_frame_to_fields <- function(fields_data_frame) {
fields <- lapply(1:nrow(fields_data_frame), function(row_nr) {
list("name" = fields_data_frame$name[row_nr],
"content" = fields_data_frame$content[row_nr])
list(
"name" = fields_data_frame$name[row_nr],
"content" = fields_data_frame$content[row_nr]
)
})
names(fields) <- fields_data_frame$name
return(fields)
}

fields_to_data_frame <- function(fields){
fields_to_data_frame <- function(fields) {
tibble::tibble(fields = fields) |> tidyr::unnest_wider("fields")
}

Expand All @@ -31,16 +33,17 @@ doc_get_fields <- function(doc_id, api_key = get_api_key()) {
#' doc_body$fields <- put_all_fields_in_one_field(doc_body$fields)
#' }
#'
put_all_fields_in_one_field <- function(doc_body_fields, use_html_sep = T){
put_all_fields_in_one_field <- function(doc_body_fields, use_html_sep = T) {
text_content <- fields_to_data_frame(doc_body_fields)

if(use_html_sep){
if (use_html_sep) {
text_content <- text_content |>
dplyr::mutate(content = paste0("<p>", as.character(content), "</p>"))
}
# Collapse content into one field
text_content <- text_content |>
dplyr::pull(content) |> paste(collapse = "\n")
dplyr::pull(content) |>
paste(collapse = "\n")

return(list(list("content" = text_content)))
}
6 changes: 3 additions & 3 deletions R/files.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ file_upload <- function(path, api_key = get_api_key()) {
#' If not, the download is canceled and `FALSE` is returned.
#' @export
file_download <- function(file_id, path = ".", api_key = get_api_key()) {
if(fs::is_dir(path)) {
if (fs::is_dir(path)) {
# determine file name
request() |>
httr2::req_url_path_append("files", parse_rspace_id(file_id)) |>
Expand All @@ -39,7 +39,7 @@ file_download <- function(file_id, path = ".", api_key = get_api_key()) {
path <- fs::path(path, json$name)
}

if(!can_overwrite(path)) {
if (!can_overwrite(path)) {
cli::cli_inform(" Cancelling download")
return(invisible(FALSE))
}
Expand All @@ -50,5 +50,5 @@ file_download <- function(file_id, path = ".", api_key = get_api_key()) {
httr2::req_perform(path = path) |>
httr2::resp_check_status() -> resp
cli::cli_inform("Downloaded to {.path {resp$body}} ({file.size(resp$body)} bytes)")
return(invisible(path))
return(invisible(path))
}
Loading

0 comments on commit 356b282

Please sign in to comment.