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

Validate filebrowser/upload fields #288

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 4 additions & 25 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,9 @@ data_block <- function(...) {
#' @export
new_upload_block <- function(...) {

data_path <- function(file) {
if (length(file)) file$datapath else character()
}

new_block(
fields = list(
file = new_upload_field(),
expression = new_hidden_field(data_path)
),
expr = quote(c(.(expression))),
fields = list(file = new_upload_field()),
expr = quote(.(file)),
...,
class = c("upload_block", "data_block")
)
Expand All @@ -241,23 +234,9 @@ upload_block <- function(...) {
#' @export
new_filesbrowser_block <- function(volumes = c(home = path.expand("~")), ...) {

data_path <- function(file) {

if (length(file) == 0 || is.integer(file) || length(file$files) == 0) {
return(character())
}

files <- shinyFiles::parseFilePaths(volumes, file)

unname(files$datapath)
}

new_block(
fields = list(
file = new_filesbrowser_field(volumes = volumes),
expression = new_hidden_field(data_path)
),
expr = quote(c(.(expression))),
fields = list(file = new_filesbrowser_field(volumes = volumes)),
expr = quote(.(file)),
...,
class = c("filesbrowser_block", "data_block")
)
Expand Down
44 changes: 33 additions & 11 deletions R/field.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,7 @@
#' @rdname new_field
#' @export
new_upload_field <- function(value = character(), ...) {
new_field(
value,
...,
class = "upload_field"
)
new_field(value, ..., class = "upload_field")
}

#' @rdname new_field
Expand All @@ -279,17 +275,25 @@
#' @rdname new_field
#' @export
validate_field.upload_field <- function(x) {

val <- value(x)

if ("datapath" %in% names(val) && file.exists(val$datapath)) {
value(x) <- val$datapath

Check warning on line 282 in R/field.R

View check run for this annotation

Codecov / codecov/patch

R/field.R#L282

Added line #L282 was not covered by tests
} else {
value(x) <- character()
}

x
}

#' @param volumes Paths accessible by the shinyFiles browser.
#' @rdname new_field
#' @export
new_filesbrowser_field <- function(value = character(), ...) {
new_field(
value,
...,
class = "filesbrowser_field"
)
new_filesbrowser_field <- function(value = character(),
volumes = c(home = path.expand("~")), ...) {

new_field(value, volumes = volumes, ..., class = "filesbrowser_field")
}

#' @rdname new_field
Expand All @@ -301,6 +305,24 @@
#' @rdname new_field
#' @export
validate_field.filesbrowser_field <- function(x) {

val <- value(x)

if (is.list(val) && "files" %in% names(val) && length(val$files)) {

tmp <- shinyFiles::parseFilePaths(value(x, "volumes"), val)

Check warning on line 313 in R/field.R

View check run for this annotation

Codecov / codecov/patch

R/field.R#L313

Added line #L313 was not covered by tests

if ("datapath" %in% names(tmp) && file.exists(tmp$datapath)) {
value(x) <- unname(tmp$datapath)

Check warning on line 316 in R/field.R

View check run for this annotation

Codecov / codecov/patch

R/field.R#L315-L316

Added lines #L315 - L316 were not covered by tests
} else {
value(x) <- character()

Check warning on line 318 in R/field.R

View check run for this annotation

Codecov / codecov/patch

R/field.R#L318

Added line #L318 was not covered by tests
}

} else {

value(x) <- character()
}

x
}

Expand Down
8 changes: 7 additions & 1 deletion man/new_field.Rd

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

Loading