diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..cc58978 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,3 @@ +^notly\.Rproj$ +^\.Rproj\.user$ +^LICENSE\.md$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b732e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata +.DS_Store diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..8a4e4f4 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,18 @@ +Package: notly +Title: Reversible ggplot to plotly conversion +Version: 0.0.0.9000 +Authors@R: c( + person("Gordon", "McDonald", ,"gordon.mcdonald@sydney.edu.au", role = c("aut", "cre")), + person("Henry", "Lydecker", ,"henry.lydecker@gmail.com", role = c("aut")) + ) +Description: Adds the ability to turn a ggplotly object plot back into a ggplot in Plotly R. Notly has the following functions: + notly::ggplotly() which should override plotly::ggplotly() by adding the ggplot object to the output plotly object. + notly::notly() which is basically the inverse function to plotly::ggplotly(), i.e. it extracts the ggplot object back out of the plotly object. + This way if you've saved an interactive plotly object you can recover the ggplot from it for static output e.g. pdf! +License: MIT + file LICENSE +Encoding: UTF-8 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.1.2 +Imports: + plotly, + ggplot2 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e9b0b26 --- /dev/null +++ b/LICENSE @@ -0,0 +1,2 @@ +YEAR: 2022 +COPYRIGHT HOLDER: notly authors diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..0ad2361 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +Copyright (c) 2022 notly authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..d340942 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,3 @@ +# Generated by roxygen2: do not edit by hand + +export(ggplotly) diff --git a/R/notly.R b/R/notly.R new file mode 100644 index 0000000..67f309f --- /dev/null +++ b/R/notly.R @@ -0,0 +1,55 @@ +#' Turn a ggplot into a plotly object +#' +#' This function takes a ggplot object and turns it into a plotly object. +#' The original ggplot object is retained inside of the new plotly object. +#' This makes this conversion reversible, allowing you to freely go back and forth betweeen ggplot and plotly. +#' +#' @param p ggplot object +#' @return Plotly object with a ggplot nested inside +#' @export + +ggplotly <- function( + p = ggplot2::last_plot(), + width = NULL, + height = NULL, + tooltip = "all", + dynamicTicks = FALSE, + layerData = 1, + originalData = TRUE, + source = "A", + ... +){ + #make plotly object + plotly_object <- plotly::ggplotly( + p = p, + width = width, + height = height, + tooltip = tooltip, + dynamicTicks = dynamicTicks, + layerData = layerData, + originalData = originalData, + source = source, + ... + ) + + #append ggplot + plotly_object$ggplot <- p + + #append notly class + class(plotly_object) <- append(class(plotly_object), "notly") + + #return object + return(plotly_object) +} + +#' Turn a notly back into a ggplot object +#' +#' This function takes a notly object (plotly with embedded ggplot) and turns it into a ggplot object. +#' It is the inverse function to `ggplotly()`, allowing you to freely go back and forth betweeen ggplot and plotly. +#' +#' @param notly_object notly object +#' @return ggplot object +#' @export +notly <- function(notly_object){ + notly_object$ggplot +} diff --git a/man/ggplotly.Rd b/man/ggplotly.Rd new file mode 100644 index 0000000..e24f0eb --- /dev/null +++ b/man/ggplotly.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/notly.R +\name{ggplotly} +\alias{ggplotly} +\title{Turn a ggplot into a plotly object} +\usage{ +ggplotly( + p = ggplot2::last_plot(), + width = NULL, + height = NULL, + tooltip = "all", + dynamicTicks = FALSE, + layerData = 1, + originalData = TRUE, + source = "A", + ... +) +} +\arguments{ +\item{p}{ggplot object} +} +\value{ +Plotly object with a ggplot nested inside +} +\description{ +This function takes a ggplot object and turns it into a plotly object. +The original ggplot object is retained inside of the new plotly object. +This makes this conversion reversible, allowing you to freely go back and forth betweeen ggplot and plotly. +} diff --git a/notly.R b/notly.R deleted file mode 100644 index c80f599..0000000 --- a/notly.R +++ /dev/null @@ -1,40 +0,0 @@ -library(ggplot2) -library(plotly) - -ggplotly <- function( - p = ggplot2::last_plot(), - width = NULL, - height = NULL, - tooltip = "all", - dynamicTicks = FALSE, - layerData = 1, - originalData = TRUE, - source = "A", - ... -){ - #make plotly object - plotly_object <- plotly::ggplotly( - p = p, - width = width, - height = height, - tooltip = tooltip, - dynamicTicks = dynamicTicks, - layerData = layerData, - originalData = originalData, - source = source, - ... - ) - - #append ggplot - plotly_object$ggplot <- p - - #append notly class - class(plotly_object) <- append(class(plotly_object), "notly") - - #return object - return(plotly_object) -} - -notly <- function(notly_object){ - notly_object$ggplot -} diff --git a/notly.Rproj b/notly.Rproj new file mode 100644 index 0000000..69fafd4 --- /dev/null +++ b/notly.Rproj @@ -0,0 +1,22 @@ +Version: 1.0 + +RestoreWorkspace: No +SaveWorkspace: No +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes +LineEndingConversion: Posix + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace