Skip to content

Commit

Permalink
Can now write to Excel (xlsx)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdneuzerling committed Jun 12, 2018
1 parent 17b0c42 commit bb958e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Title: Programmatic access to Microsoft Outlook on Windows through RDCOMClient
Version: 0.0.0.9000
Authors@R: person("Murray David", "Neuzerling", email = "[email protected]", role = c("aut", "cre"))
Description: Provides a wrapper to access a locally installed Microsoft Outlook application on Windows through RDCOMClient, an R package that allows R to act as a DCOM client. Access is provided through two key functions: prepare_email, which allows an email to be prepared in R with R objects (data frames, tibbles, ggplots), and find_emails, which allows the user to search their emails and extract attachments.
Depends: R (>= 3.4.4), RDCOMClient, purrr, xtable, readr, readbitmap, ggplot2
Depends: R (>= 3.4.4), RDCOMClient, purrr, xtable, readr, writexl, readbitmap, ggplot2
License: GNU General Public License v3.0
Encoding: UTF-8
LazyData: true
Expand Down
16 changes: 10 additions & 6 deletions R/data_to_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' @param data A data frame or tibble to be converted into a file.
#' @param file_format A file_format which will determine how the data is saved
#' and the extension of the resulting file. Currently supports "csv" (comma-
#' separated) or tsv (tab-separated).
#' Defaults to "csv" (comma-separated)
#' separated), "tsv" (tab-separated), or "xlsx"/"excel".
#' Defaults, "csv" (comma-separated)
#' @param file_name Sets the name of the saved file, without the extension. If
#' this isn't provided, the name of the `data` variable will be used. "." is a
#' forbidden `file_name` for this function, and is usually provided by a pipe
Expand All @@ -19,11 +19,14 @@
#' @keywords

data_to_file <- function(data, file_format = "csv", file_name = NULL, ...) {

# Excel files are saved as .xlsx
if (file_format == "excel") {file_format <- "xlsx"}

file_name <- if (!is.null(file_name)) {
file_name
} else {
deparse(substitute(gg)) # Name the file after the variable
deparse(substitute(data)) # Name the file after the variable
}

if (file_name == ".") {
Expand All @@ -34,12 +37,13 @@ data_to_file <- function(data, file_format = "csv", file_name = NULL, ...) {
file_path <- paste0(tempdir(), "/", file_name, ".", file_format)
if (file_format == "csv") {
readr::write_csv(data, path = file_path, ...)
} else if (file_format == "txt") {
} else if (file_format == "tsv") {
readr::write_tsv(data, path = file_path, ...)
} else if (file_format == "xlsx") {
stop("No method implemented for writing data to excel files")
writexl::write_xlsx(data, path = file_path, ...)
} else {
stop(paste0("Don't know how to write to ", file_format))
stop(paste0("Don't know how to write to ", file_format, ". ",
"Can only write to 'csv', 'tsv' or 'xlsx'/'excel'."))
}

return(file_path)
Expand Down
6 changes: 3 additions & 3 deletions R/ggplot_to_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' given an extension that corresponds to the method by which the file was
#' saved. The `device` (eg. "png" or "jpeg") is passed to the `ggsave` function.
#' @param gg A ggplot to be converted into a file.
#' @param device An argument passed onto the ggsave function that determines
#' @param file_format An argument passed onto the ggsave function that determines
#' how the plot is saved. It is recommended to use either "png" or "jpeg".
#' Defaults to "png".
#' @param file_name Sets the name of the saved file, without the extension. If
Expand All @@ -15,7 +15,7 @@
#' string of random numbers.
#' @keywords

ggplot_to_file <- function(gg, device = "png", file_name = NULL, ...) {
ggplot_to_file <- function(gg, file_format = "png", file_name = NULL, ...) {

file_name <- if (!is.null(file_name)) {
file_name
Expand All @@ -28,6 +28,6 @@ ggplot_to_file <- function(gg, device = "png", file_name = NULL, ...) {
}

file_path <- paste0(tempdir(), "/", file_name, ".", device)
ggplot2::ggsave(file_path, plot = gg, device = device, ...)
ggplot2::ggsave(file_path, plot = gg, device = device = file_format, ...)
return(file_path)
}

0 comments on commit bb958e6

Please sign in to comment.