Skip to content

Commit

Permalink
correct drop_read karthik#199
Browse files Browse the repository at this point in the history
add drop_save karthik#190
  • Loading branch information
lewishounkpevi committed Dec 30, 2021
1 parent 4cbbdce commit 067c4b5
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 30 deletions.
12 changes: 9 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Version: 0.8.2.1
Authors@R: c(person("Karthik", "Ram", email = "[email protected]", role = c("aut", "cre")),
person("Clayton", "Yochum", role = "aut"),
person("Caleb", "Scheidel", role = "ctb"),
person("Akhil", "Bhel", role = "cph")
person("Akhil", "Bhel", role = "cph"),
person(given = "Lewis",
family = "Hounkpevi",
role = "ctb",
email = "[email protected]",
comment = c(ORCID = "0000-0001-5111-8568"))
)
Description: Provides full programmatic access to the 'Dropbox' file hosting platform <https://dropbox.com>, including support for all standard file operations.
Depends: R (>= 3.1.1)
Expand All @@ -19,7 +24,8 @@ Imports:
jsonlite,
magrittr,
purrr,
readxl
readxl,
openxlsx
Suggests: testthat,
uuid
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ export(drop_history)
export(drop_list_shared_links)
export(drop_media)
export(drop_move)
export(drop_read)
export(drop_read_csv)
export(drop_save)
export(drop_search)
export(drop_share)
export(drop_upload)
import(httr)
importFrom(magrittr,"%>%")
importFrom(openxlsx,write.xlsx)
importFrom(readxl,read_excel)
importFrom(utils,write.csv)
11 changes: 6 additions & 5 deletions R/drop_read.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#' drop_read
#'
#' @description wrapper for importing read.csv, read_excel readRDS and load from dropbox
#' @description wrapper for importing read.csv,
#' read_excel readRDS and load from dropbox
#'
#' @param file path on dropbox
#' @param dest local path. tempdir for default
#' @param dtoken token
#' @param ... other arguments according to file format into \code{read.csv} or \code{readxl} or \code(readRDS) or \code(load)
#' @importFrom rdrop2 drop_download drop_upload
#' @param ... other arguments according to file format
#' into \code{read.csv} or \code{read_excel} or \code{readRDS} or \code{load}
#' @importFrom readxl read_excel
#' @author Lewis Hounkpevi
#' @export
#' @examples \dontrun{
#'
#' save(airquality, file = "airquality.RData")
#' save(attenu, file = "attenu.RData")
#' save(austres, file = "austres.RData")
Expand Down Expand Up @@ -41,7 +42,7 @@

drop_read <- function (file,
dest = tempdir(),
dtoken = rdrop2:::get_dropbox_token(),
dtoken = get_dropbox_token(),
...){
localfile = paste0(dest, "/", basename(file))
drop_download(file, localfile, overwrite = TRUE, dtoken = dtoken)
Expand Down
74 changes: 74 additions & 0 deletions R/drop_save.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#' drop_save
#'
#'@param object R object to save
#'@param path The relative path on Dropbox where the file should get uploaded.
#'@param mode - "add" - will not overwrite an existing file in case of a
#' conflict. With this mode, when a a duplicate file.txt is uploaded, it will
#' become file (2).txt. - "overwrite" will always overwrite a file -
#'@param autorename This logical determines what happens when there is a
#' conflict. If true, the file being uploaded will be automatically renamed to
#' avoid the conflict. (For example, test.txt might be automatically renamed to
#' test (1).txt.) The new name can be obtained from the returned metadata. If
#' false, the call will fail with a 409 (Conflict) response code. The default is `TRUE`
#'@param mute Set to FALSE to prevent a notification trigger on the desktop and
#' mobile apps
#'@template verbose
#'@template token
#'@references \href{https://www.dropbox.com/developers/documentation/http/documentation#files-upload}{API documentation}
#'@param ext file extension that will be saved. here we suggest csv, excel, rds, RData
#'@param ... other arguments for write.csv, write.xlsx, readRDS, save
#'@importFrom openxlsx write.xlsx
#'@importFrom utils write.csv
#'@author Lewis Hounkpevi
#'@export
#'
#' @examples \dontrun{
#' drop_save(BOD, ext = "rds")
#' drop_save(BOD, ext = "RData")
#' drop_save(BOD, ext = "xlsx")
#' drop_save(BOD, ext = "csv")
#'}
drop_save <- function (object,
path = NULL,
mode = "overwrite",
autorename = TRUE,
mute = FALSE,
verbose = FALSE,
dtoken = get_dropbox_token(),
ext = c("csv", "xlsx", "rds", "RData"),
...){


localpath <- paste0(tempdir(), "/", deparse(substitute(object)), ".", ext)



if(ext == "csv") {

write.csv(object, file = localpath, ...)

}else if (ext == "xlsx" | ext == "xls"){

openxlsx::write.xlsx(object, file = localpath, ...)

} else if(ext == "rds" ){

saveRDS(object, file = localpath, ...)

} else if (ext == "RData" | ext == "rdata" | ext == "RDATA") {

save(object, file = localpath, ...)
}




drop_upload(file = localpath,
path = path,
mode = mode,
autorename = autorename,
mute = mute,
verbose = verbose,
dtoken = dtoken)

}
53 changes: 53 additions & 0 deletions man/drop_read.Rd

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

66 changes: 66 additions & 0 deletions man/drop_save.Rd

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

44 changes: 22 additions & 22 deletions tests/testthat/test-99-rdrop2.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test_that("drop_share works correctly", {
})


# drop_search
drop_search
test_that("drop_search works correctly", {
skip_on_cran()

Expand Down Expand Up @@ -82,27 +82,27 @@ test_that("drop_history works correctly", {
})


# drop_exists
# test_that("drop_exists works correctly", {
# skip_on_cran()
#
# folder_name <- traceless("drop_exists")
# drop_create(folder_name)
#
# expect_true(drop_exists(folder_name))
# expect_false(drop_exists(traceless("stuffnthings")))
#
# # Now test files inside subfolders
# write.csv(iris, file = "iris.csv")
# drop_upload("iris.csv", path = folder_name)
# expect_true(drop_exists(paste0(folder_name, "/iris.csv")))
#
# #cleanup
# drop_delete(folder_name)
# unlink("iris.csv")
# })
#
#
drop_exists
test_that("drop_exists works correctly", {
skip_on_cran()

folder_name <- traceless("drop_exists")
drop_create(folder_name)

expect_true(drop_exists(folder_name))
expect_false(drop_exists(traceless("stuffnthings")))

# Now test files inside subfolders
write.csv(iris, file = "iris.csv")
drop_upload("iris.csv", path = folder_name)
expect_true(drop_exists(paste0(folder_name, "/iris.csv")))

#cleanup
drop_delete(folder_name)
unlink("iris.csv")
})


# # drop_media
# test_that("drop_media works correctly", {
# skip_on_cran()
Expand Down

0 comments on commit 067c4b5

Please sign in to comment.