Skip to content

Commit 0f4906e

Browse files
committed
upd
1 parent f4d61c2 commit 0f4906e

35 files changed

+104
-173
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
^.*\.Rproj$
22
^\.Rproj\.user$
33
^LICENSE\.md$
4+
^sandbox$

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Title: What the Package Does (One Line, Title Case)
33
Version: 0.0.0.9000
44
Authors@R: c(
55
person("Tom", "Vladeck", email = "[email protected] ", role = c("aut")),
6-
person("Stefan", "Musch", email = "[email protected]", role = c("aut", "cre")),
7-
person("Iaroslav", "Domin", email = "[email protected]", role = c("aut")),
6+
person("Stefan", "Musch", email = "[email protected]", role = c("aut")),
7+
person("Iaroslav", "Domin", email = "[email protected]", role = c("aut", "cre")),
88
person("Michal", "Czyz", email = "[email protected]", role = c("aut")),
99
person("Emmanuel", "Ugochukwu", email = "[email protected]", role = c("aut")),
1010
person("Gradient Metrics", role = c("cph"))

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ importFrom(cli,cli_abort)
3030
importFrom(dplyr,`%>%`)
3131
importFrom(dplyr,case_when)
3232
importFrom(dplyr,group_by)
33+
importFrom(dplyr,if_else)
3334
importFrom(dplyr,left_join)
3435
importFrom(dplyr,mutate)
36+
importFrom(dplyr,relocate)
3537
importFrom(dplyr,select)
3638
importFrom(dplyr,summarise)
3739
importFrom(dplyr,tibble)

R/cli_interface.R renamed to R/cli.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ check_scalar <- function(..., arg_class, alt_null = FALSE) {
137137
}
138138
}
139139

140+
#' @description [check_scalar] for `arg_class` equal to "character".
141+
#' @noRd
142+
check_string <- function(..., alt_null = FALSE) {
143+
check_scalar(..., arg_class = "character", alt_null = alt_null)
144+
}
145+
140146
#' @title Check Argument's Class
141147
#'
142148
#' @description Check if argument is of proper class.
@@ -319,4 +325,4 @@ check_null_cond <- function(x, alt_null){
319325
check_null <- is.null(x)
320326
}
321327
check_null
322-
}
328+
}

R/common.R

Lines changed: 22 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,15 @@
44
#' @param project Character. Path to a project. By default it is current working
55
#' directory.
66
#'
7-
proj_desc_get <- function(key, project = getwd()) {
8-
stopifnot(is.character(key) & length(key) == 1)
9-
stopifnot(is.character(project) & length(project) == 1)
7+
proj_desc_get <- function(key, project = ".") {
8+
check_string(key)
9+
check_string(project)
1010
validate_desc(project)
1111
desc_file <- file.path(project, "DESCRIPTION")
1212
value <- desc::desc_get(key, desc_file)
1313
unname(value)
1414
}
1515

16-
#' @title Warn if cloud function is used not for current working directory
17-
#'
18-
#' @description Functions for uploading/downloading files from project cloud
19-
#' locations are designed to synchronize local and cloud folder structures.
20-
#' That is e.g. when you call `cloud_s3_upload` with `file` parameter set to
21-
#' "data/demo.csv" and `project` parameter set to something different from the
22-
#' current working directory it is always assumed that "data/demo.csv" from
23-
#' **the project's folder** and not from the current working directory needs
24-
#' to be uploaded to S3. But for development purposes it is handy to be able
25-
#' to call the functions not only for the current wd. This function checks
26-
#' that project is set to the current wd. If not, it throws a warning and asks
27-
#' if user wants to continue.
28-
#'
29-
#' @param project Path to a project. By default it is current working directory.
30-
#'
31-
cloud_not_wd_warning <- function(project) {
32-
stopifnot(is.character(project) & length(project) == 1)
33-
if (cloud_talk()) {
34-
wd <- normalizePath(getwd())
35-
project <- normalizePath(project)
36-
if (wd != project) {
37-
cli::cli_warn(
38-
"This function is meant to be used without changing the \\
39-
{.arg project} parameter."
40-
)
41-
yeah <- cli_yeah("Do you want to continue?")
42-
if (!yeah) {
43-
cli::cli_abort("Aborting")
44-
}
45-
}
46-
}
47-
}
48-
4916
#' @title Validate file path for cloud functions
5017
#'
5118
#' @description Makes sure that file path passed to a cloud function is in the
@@ -57,7 +24,7 @@ cloud_not_wd_warning <- function(project) {
5724
#' file path.
5825
#'
5926
cloud_validate_file_path <- function(file, error = TRUE) {
60-
stopifnot(is.character(file))
27+
check_string(file)
6128
res <- grepl("^([A-Za-z]|[0-9]|-|_|\\.| |/)+$", file)
6229
if (error) {
6330
if (file == "") stop("A valid file name should not be empty.")
@@ -81,7 +48,7 @@ cloud_validate_file_path <- function(file, error = TRUE) {
8148
#'
8249
#' @noRd
8350
cloud_validate_file_names <- function(x) {
84-
stopifnot(is.character(x))
51+
check_class(x, arg_class = "character")
8552
bad_na <- is.na(x)
8653
bad_symbols <- !grepl("^([A-Za-z]|[0-9]|-|_| |\\.)+$", x)
8754
x_trimmed <- gsub("^[ ]+", "", gsub("[ ]+$", "", x))
@@ -98,60 +65,17 @@ cloud_validate_file_names <- function(x) {
9865
return(invisible(TRUE))
9966
}
10067

101-
#' @title Assert that a key in project's DESCRIPTION file has a certain value
102-
#'
103-
#' @description Given a path do DESCRIPTION file or to a project containing such
104-
#' file makes sure that field `key` in it has value `value`.
105-
#' - If this field is absent, proposes to populate it with `value`.
106-
#' - If this field exists, but is populated with a different value, throws an
107-
#' error.
108-
#' - If this field exists and is populated with the right value, silently
109-
#' returns TRUE.
110-
#'
111-
#' @param key field name, character
112-
#' @param value required field value, character
113-
#' @param file path to DESCRIPTION file
114-
#'
115-
#' @noRd
116-
assert_desc_field <- function(key, value, file) {
117-
stopifnot(is.character(key) & length(key) == 1)
118-
stopifnot(is.character(value) & length(value) == 1)
119-
desc_value <- desc::desc_get(keys = key, file = file)
120-
if (is.na(desc_value)) {
121-
cli::cli_warn("Field {.field key} does not exist in {.path DESCRIPTION}.")
122-
yeah <- cli_yeah("Fill it with {.val value}?", straight = TRUE)
123-
if (yeah) {
124-
desc::desc_set_list(key, value, file = file)
125-
return(invisible(TRUE))
126-
} else {
127-
cli::cli_abort("Stopping")
128-
}
129-
}
130-
if (desc_value != value)
131-
cli::cli_abort(
132-
"Value found in {.path DESCRIPTION}, {.val {desc_value}}, is different \\
133-
from what should be there - {.val {value}}."
134-
)
135-
return(invisible(TRUE))
136-
}
137-
13868
#' @title Validate project's DESCRIPTION file
13969
#'
140-
#' @description Given a path to a project, figures out project name and base
141-
#' package. Checks that `Name` and `BasePkg` fields in project's
142-
#' DESCRIPTION file have corresponding values.
143-
#' - If DESCRIPTION file is not found, proposes to create one and populate all
144-
#' the main fields (including `Name` and `BasePkg`) automatically.
145-
#' - If DESCRIPTION exists but `Name` and/or `BasePkg` are not populated,
146-
#' proposes to populate these fields.
147-
#' - If applied to a package folder, throws a warning.
70+
#' @description Checks that DESCRIPTION file exists in a project folder. If it's
71+
#' not the case, proposes to create a DESCRIPTION file from template.
14872
#'
14973
#' @inheritParams cloud_not_wd_warning
15074
#'
15175
#' @noRd
152-
validate_desc <- function(project = getwd()) {
76+
validate_desc <- function(project = ".") {
15377

154-
desc_path <- file.path(project, "DESCRIPTION")
78+
desc_path <- normalizePath(file.path(project, "DESCRIPTION"))
15579

15680
if (!file.exists(desc_path)) {
15781

@@ -164,16 +88,19 @@ validate_desc <- function(project = getwd()) {
16488
desc_content <- c(
16589
"Package: -",
16690
"Name: [Project Name]",
167-
"Title: [Description about the project]"
91+
"Title: [Project Title]",
92+
"Description: [Project Description]"
16893
)
16994

17095
writeLines(con = desc_path, desc_content)
17196

17297
cli::cli_bullets(c(
17398
"v" = "A sample DESCRIPTION file has been created at \\
174-
{.path {project}/DESCRIPTION}.",
175-
" " = "Feel free to edit the {.field Name} and {.field Title} fields \\
176-
as needed to reflect your current project (optional)."
99+
{.path {desc_path}}.",
100+
" " = "Feel free to edit the {.field Name}, {.field Title} and \\
101+
{.field Description} fields as needed to reflect your current project \\
102+
(optional).",
103+
" " = "Please don't change the {.field Package} field."
177104
))
178105
return(invisible(TRUE))
179106
} else {
@@ -207,8 +134,12 @@ validate_desc <- function(project = getwd()) {
207134
#' names to give a relative file path.
208135
#'
209136
cloud_prep_ls <- function(data, path, recursive, full_names) {
210-
stopifnot(is.data.frame(data))
211-
stopifnot(all(c("short_name", "last_modified", "size_b") %in% names(data)))
137+
check_class(data, arg_class = "data.frame")
138+
required_cols <- c("short_name", "last_modified", "size_b")
139+
if (!all(required_cols %in% names(data)))
140+
cli::cli_abort("{.arg data} must contain the following column names: \\
141+
{.val {required_cols}}")
142+
212143
data <- data[data$short_name != "", ]
213144

214145
if (nrow(data) == 0) {

R/drive_setup.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
#' @export
2020
cloud_drive_attach <- function(project = getwd()) {
2121

22-
## Check for description file and add if not found
23-
validate_desc(project)
24-
2522
name <- proj_desc_get("Name", project)
2623
drive_desc <- proj_desc_get("CloudDrive", project)
2724

R/package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#' @importFrom dplyr `%>%` mutate select group_by summarise left_join case_when
2-
#' tibble
2+
#' tibble if_else relocate
33
#'
44
#' @keywords internal
55
"_PACKAGE"

R/s3_setup.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#'
1919
#' @export
2020
cloud_s3_attach <- function(project = getwd()) {
21-
validate_desc(project)
2221

2322
name <- proj_desc_get("Name", project)
2423
s3_desc <- proj_desc_get("CloudS3", project)

R/s3_show.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ cloud_s3_browse_prefix <- function(prefix = "") {
3333
#' }
3434
#'
3535
#' @export
36-
cloud_s3_browse <- function(path = "", project = getwd()) {
36+
cloud_s3_browse <- function(path = "", root = NULL) {
3737
stopifnot(is.character(path) & length(path) == 1)
3838
s3_folder <- cloud_s3_get_location(project = project)
3939
prefix <- file.path(s3_folder, path, "/")
@@ -67,19 +67,24 @@ cloud_s3_browse <- function(path = "", project = getwd()) {
6767
cloud_s3_ls <- function(path = "", recursive = FALSE, full_names = FALSE,
6868
project = getwd()) {
6969
stopifnot(is.character(path) & length(path) == 1)
70-
s3_folder <- cloud_s3_get_location(project = project)
7170
stopifnot(isTRUE(recursive) | isFALSE(recursive))
7271
stopifnot(isTRUE(full_names) | isFALSE(full_names))
73-
prefix <- clean_file_path(s3_folder, path, "/")
74-
cli::cli_text("{.field prefix}: {.path {prefix}}")
72+
73+
s3_location <- cloud_s3_get_location(project = project)
74+
s3_path <- clean_file_path(s3_location, path, "/")
75+
cli::cli_text("{.field S3 path}: {.path {s3_path}}")
76+
77+
s3_path_split <- strsplit(s3_path, "/")[[1]]
78+
bucket <- s3_path_split[[1]]
79+
prefix <- paste0(paste(s3_path_split[-1], collapse = "/"), "/")
7580

7681
# NOTE: this lists all contents recursively regardless of `recursive`
7782
# parameter because this way it is easier to parse the response. Shouldn't
7883
# work any slower anyways unless a project contains hundreds nested folders
7984
# with thousands of files (highly unlikely).
8085
resp_df <-
8186
aws.s3::get_bucket_df(
82-
bucket = "bucket-name",
87+
bucket = bucket,
8388
delimiter = "",
8489
prefix = prefix,
8590
max = Inf

cloudfs.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ LaTeX: XeLaTeX
1515
BuildType: Package
1616
PackageUseDevtools: Yes
1717
PackageInstallArgs: --no-multiarch --with-keep.source
18+
PackageRoxygenize: rd,collate,namespace

0 commit comments

Comments
 (0)