From 8fbbeeaf6dc23164660ad95ffa9fca4d6c34fa56 Mon Sep 17 00:00:00 2001 From: Chris Salvato Date: Mon, 13 Jun 2016 09:32:00 -0400 Subject: [PATCH] Create file output for all plots that are generated --- SalvatoUtilities/R/report.plot.r | 46 +++++++++++++++++++++++++++++ SalvatoUtilities/man/report.plot.Rd | 32 ++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 SalvatoUtilities/R/report.plot.r create mode 100644 SalvatoUtilities/man/report.plot.Rd diff --git a/SalvatoUtilities/R/report.plot.r b/SalvatoUtilities/R/report.plot.r new file mode 100644 index 0000000..45f4e9f --- /dev/null +++ b/SalvatoUtilities/R/report.plot.r @@ -0,0 +1,46 @@ +#' Report Plot +#' +#' Creates a plot for an exported report. +#' Writes a ggplot object to a plot using plot() or writes the plot to a PNG file. +#' +#' @param ggplot_object ggplot object to be plotted. +#' @param file (optional) File name for the file to be saved. If not supplied +#' then the data frame's name is used. Note, ".csv" is appended automatically. +#' @export +#' @examples +#' test <- ggplot( +#' keywords_weekly_conversion_metrics, +#' aes(x=week, y=est_search_impression_share, fill=campaign_name)) + +#' geom_bar(stat="identity", position="dodge") + +#' ggtitle("AdWords - Weekly Impression Share by Geo") + +#' ylim(0, 1) + +#' facet_wrap(~keyword, ncol=2) +#' ) +#' report.plot(test, "example.png") # Writes to file "example.png" +#' report.plot(test, "") # Writes to file test.png +#' report.plot(test, NULL) # Plots without writing to file. + +report.plot <- function(ggplot_object, file=""){ + # If the file name is not specified, use the data frame variable name. + if( file == "" ) { + file <- deparse(substitute(data_frame)) #gets variable name as string + } + + # If doesn't have .png extension in the name, add it. + if ( !grepl("\\.png$", file) ) { + file <- paste0(file,".png") + } + + if( !is.null(file) ) { + require(SalvatoUtilities) + png( filename = file, + width = 1280, + height = 800) + plot( ggplot_object ) + dev.off() + } else { + plot( ggplot_object ) + } + + +} \ No newline at end of file diff --git a/SalvatoUtilities/man/report.plot.Rd b/SalvatoUtilities/man/report.plot.Rd new file mode 100644 index 0000000..56e84d4 --- /dev/null +++ b/SalvatoUtilities/man/report.plot.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/report.plot.r +\name{report.plot} +\alias{report.plot} +\title{Report Plot} +\usage{ +report.plot(ggplot_object, file = "") +} +\arguments{ +\item{ggplot_object}{ggplot object to be plotted.} + +\item{file}{(optional) File name for the file to be saved. If not supplied +then the data frame's name is used. Note, ".csv" is appended automatically.} +} +\description{ +Creates a plot for an exported report. +Writes a ggplot object to a plot using plot() or writes the plot to a PNG file. +} +\examples{ +test <- ggplot( + keywords_weekly_conversion_metrics, + aes(x=week, y=est_search_impression_share, fill=campaign_name)) + + geom_bar(stat="identity", position="dodge") + + ggtitle("AdWords - Weekly Impression Share by Geo") + + ylim(0, 1) + + facet_wrap(~keyword, ncol=2) + ) +report.plot(test, "example.png") # Writes to file "example.png" +report.plot(test, "") # Writes to file test.png +report.plot(test, NULL) # Plots without writing to file. +} +