Skip to content

Commit

Permalink
move from add_ggplot to ggplot_add for upcoming ggplot release
Browse files Browse the repository at this point in the history
  • Loading branch information
yonicd committed Mar 2, 2018
1 parent 35b9bb5 commit ea7d89a
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 150 deletions.
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: ggedit
Type: Package
Title: Interactive 'ggplot2' Layer and Theme Aesthetic Editor
Version: 0.2.6
Date: 2017-10-01
Version: 0.2.6.1
Date: 2018-02-10
Author: Jonathan Sidi [aut, cre]
Maintainer: Jonathan Sidi <yonis@metrumrg.com>
Description: Interactively edit 'ggplot2' layer and theme aesthetics definitions.
Depends:
R (>= 2.3.0),
ggplot2 (>= 2.2.1)
ggplot2
Imports:
dplyr (>= 0.5),
shiny,
Expand All @@ -29,10 +29,11 @@ Imports:
Suggests:
testthat,
covr
License: GPL-2 | GPL-3
Remotes: tidyverse/ggplot2#2405
License: MIT + file LICENSE
URL: https://github.com/metrumresearchgroup/ggedit
BugReports: https://github.com/metrumresearchgroup/ggedit/issues
LazyData: true
NeedsCompilation: no
Packaged: 2017-01-31 14:00:00 UTC; Yoni
RoxygenNote: 6.0.1.9000
RoxygenNote: 6.0.1
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2018
COPYRIGHT HOLDER: Jonathan Sidi
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

S3method("+",gg)
S3method("-",gg)
S3method(merge_element,default)
S3method(merge_element,element)
S3method(print,ggedit)
S3method(print,gglist)
S3method(summary,ggedit)
Expand All @@ -26,7 +24,6 @@ export(ggedit_opts_current)
export(gggsave)
export(is.ggedit)
export(layersList)
export(merge_element)
export(proto_features)
export(remove_geom)
export(rgg)
Expand All @@ -35,6 +32,7 @@ export(vplayout)
import(dplyr)
import(ggplot2)
import(shiny)
importFrom(ggplot2,ggplot_add)
importFrom(graphics,plot)
importFrom(miniUI,gadgetTitleBar)
importFrom(miniUI,miniContentPanel)
Expand Down
40 changes: 14 additions & 26 deletions R/as.gglist.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#' @keywords internal
as.gglist <- function(p) {
cl <- c("ggedit", "gglist", "gg", "ggplot")

if (inherits(p, "ggmatrix")) {
cl <- c(cl, "ggmatrix")
}

structure(p, class = cl)
}

Expand Down Expand Up @@ -49,33 +49,21 @@ as.gglist <- function(p) {
}

#' @export
#' @import ggplot2
#' @importFrom ggplot2 ggplot_add
#' @importFrom plyr llply
"+.gg" <- function(e1, e2) {

# check for na names in themes

if (inherits(e1, "gglist")) {
e3 <- as.gglist(
plyr::llply(e1, function(t1) {
add_ggplot(t1, e2)
})
)
if(inherits(e1,'gglist')){

e3 <- plyr::llply(e1,ggplot_add,object=e2)

attributes(e3) <- attributes(e1)

} else {
if (inherits(e1, "theme")) {
idx <- which(is.na(names(e2)))

if (length(idx) > 0) {
e2[idx] <- NULL
}

e3 <- add_theme(e1, e2)
} else {
e3 <- add_ggplot(e1, e2)
}

e3 <- ggplot_add(e2,e1)

}

attributes(e3) <- attributes(e1)


e3
}
}
11 changes: 10 additions & 1 deletion R/compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ compare <- function(e1, e2, verbose=TRUE) {

dfOut$class <- ifelse(dfOut$call == "unit", "unit", dfOut$class)
dfOut$value <- ifelse(grepl("#", dfOut$value), gsub("#", "'#'*", dfOut$value), dfOut$value)
#dfOut$value <- ifelse(grepl("^[.]$", dfOut$value), NA, dfOut$value)
dfOut$value <- ifelse(grepl("TRUE|FALSE|[=]", dfOut$value), paste0("'", dfOut$value, "'"), dfOut$value)
dfOut$call <- ifelse(dfOut$call %in% c("character", "unit", dfOut$subTheme), "", dfOut$call)
dfOut$element <- ifelse(dfOut$element == dfOut$subTheme, "", dfOut$element)
Expand All @@ -59,22 +60,30 @@ compare <- function(e1, e2, verbose=TRUE) {

d <- objDF %>%
select_("idTheme:value", "subTheme") %>%
reshape2::dcast(Theme + subTheme + element + name~idTheme, value.var = "value") %>%
reshape2::dcast(Theme + subTheme + element + name~idTheme, value.var = "value") %>% #,fill='.'
dplyr::filter_(~compare != base) %>%
dplyr::left_join(objDF, by = c("Theme", "subTheme", "element", "name")) %>%
dplyr::filter_(~idTheme == "compare") %>%
dplyr::select_("Theme", "subTheme", "call", "element", value = "compare")

d$Theme <- as.character(d$Theme)

d <- d[d$value != ".", ]
d$value[grepl("[^0-9]", d$value)] <- paste0("'", d$value[grepl("[^0-9]", d$value)], "'")

#d$value[d$value== "."] <- NA
#d$value[grepl("[^0-9]", d$value)&!grepl('^(TRUE|FALSE)$',d$value)] <- paste0("'", d$value[grepl("[^0-9]", d$value)&!grepl('^(TRUE|FALSE)$',d$value)], "'")

x1 <- d %>%
dplyr::group_by_("Theme", "subTheme", "call") %>%
dplyr::summarise_(y = "paste0(paste0(element,'=',value),collapse=',')") %>%
dplyr::ungroup()

x1$y <- ifelse(x1$call == "", gsub("=", "", x1$y), paste0(x1$call, "(", x1$y, ")"))

# x1$y <- gsub("''TRUE''","TRUE",x1$y)
# x1$y <- gsub("''FALSE''","FALSE",x1$y)

x1$x <- ifelse(x1$subTheme == "", x1$Theme, paste(x1$Theme, x1$subTheme, sep = "."))
out <- paste0("theme(", paste0(paste(x1$x, x1$y, sep = "="), collapse = ","), ")")

Expand Down
1 change: 1 addition & 0 deletions R/print.gglist.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
print.gglist <- function(x, ...) plyr::l_ply(x, print.ggplot, ...)

print.ggplot <- function(x, newpage = is.null(vp), vp = NULL, ...) {

set_last_plot(x)
if (newpage) {
grid.newpage()
Expand Down
7 changes: 7 additions & 0 deletions inst/examples/SingleFile/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Title: ggedit Shiny Module
Author: Metrum Research Group
AuthorUrl: https://github.com/metrumresearchgroup/ggedit
License: GPL-3
DisplayMode: Showcase
Tags: ggplot2
Type: Shiny
98 changes: 98 additions & 0 deletions inst/examples/SingleFile/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
library(ggedit)
library(shinyAce)

ui <- fluidPage(
tags$head(tags$script(' var setInitialCodePosition = function() { setCodePosition(false, false); }; ')),
conditionalPanel("input.tbPanel=='a'",
sidebarPanel(
h3('Static Plots'),
p('This is a standard rendered ggplot in shiny.'),
p('It is an image that can not be edited. You can drag it and even right click and save it.'),
p('But what if you wanted to change something about the aesthetics of the plot or the theme while the app is running?'),
p('Move to next tab ...'))
),
conditionalPanel("input.tbPanel=='b'",
sidebarPanel(
h3('Interactive Plot Editing'),
shiny::HTML('<a href="https://www.youtube.com/watch?v=pJ1kbd_OVwg" target="_blank">Short video clip showing how to use the module</a>'),
p('Using ggedit modules you can edit plots (to the right) and see the updated script (below) being generated as you work!'),
p(),
p('This tab gives a basic usage example of working with one plot in the module'),
p('To work on multiple plots in a layout at the same time go to the next tab ...'),
h4('Layer Editing'),
p('Click on the "Update Plot Layer" link and a pop up window will appear with aesthetics relevant to the layer to edit'),
p(),
h4('Theme Editing'),
p('Click on the "Update Plot Theme" link and a pop up window will appear with all the theme elements to edit'),
p(),
h4('Live Script Comparisons'),
p(paste0('Click on the "View Layer Code" link and a text window will expand to show the original script that was used to ',
'create the plot layer and the new script needed to create the layer with the new aesthetic settings.')),
hr(),
p(),
h4('Shiny Observers'),
p(paste0('The ggedit modules have observers built in for shiny to use, so any changes can be acted upon within the app as the user is editing.',
'This can be used for auto-updating reactive documents when collaborating with many researchers or logging user activity on a company server.')),
uiOutput('x1'),
uiOutput('x2'))
),
conditionalPanel("input.tbPanel=='c'",
sidebarPanel(
h3('Editing Multiple Plots'),
shiny::HTML('<a href="https://www.youtube.com/watch?v=pJ1kbd_OVwg" target="_blank">Short video clip showing how to use the module</a>'),
h4('Choosing a plot'),
p('When working with multiple plots use the dropdown menu to the plot you want to edit'),
h4('Choosing a layer'),
p('When choosing the plot the layers found it in will update in the radio buttons'),
p('If there is more than one layer in the plot choose the layer you want to edit'),
p(),
p('Click on the "Update Plot Layer" link and a pop up window will appear with aesthetics relevant to the layer to edit'),
p(),
h4('Sharing Themes'),
p('After choosing a plot to edit, click on the "Update Plot Theme" link and a pop up window will appear with all the theme elements to edit'),
p('You can share themes between plots in the layout using the "Update Grid Theme" link'),
p('You can also set the R session theme by selecting the "Update Global Theme" link')
)),
mainPanel(
tabsetPanel(id = 'tbPanel',
tabPanel('renderPlot/plotOutput',value = 'a',
plotOutput('p')
),
tabPanel('ggEdit/ggEditUI',value = 'b',
ggEditUI("pOut1")),
tabPanel('ggEdit/ggEditUI with lists of plots',value = 'c',ggEditUI("pOut2"))
)
)

)

server <- function(input, output,session) {
p1=iris%>%ggplot(aes(x=Sepal.Length,y=Sepal.Width,colour=Species))+geom_point()
p2=iris%>%ggplot(aes(x=Sepal.Length,y=Sepal.Width))+geom_line()+geom_point((aes(colour=Petal.Width)))
p3=list(p1=p1,p2=p2)

output$p<-renderPlot({p1})
outp1<-callModule(ggEdit,'pOut1',obj=reactive(list(p1=p1)))
outp2<-callModule(ggEdit,'pOut2',obj=reactive(p3),showDefaults=T,height=300)

output$x1<-renderUI({
layerTxt=outp1()$UpdatedLayerCalls$p1[[1]]
aceEditor(outputId = 'layerAce',value=layerTxt,
mode = "r", theme = "chrome",
height = "100px", fontSize = 12,wordWrap = T)
})

output$x2<-renderUI({

themeTxt=outp1()$UpdatedThemeCalls$p1

if(is.null(themeTxt)) themeTxt <- ' '

aceEditor(outputId = 'themeAce',value=themeTxt,
mode = "r", theme = "chrome",
height = "100px", fontSize = 12,wordWrap = T)
})

}

shinyApp(ui, server)
42 changes: 0 additions & 42 deletions inst/examples/app.R

This file was deleted.

37 changes: 0 additions & 37 deletions man/grapes-greater-than-grapes.Rd

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

Loading

0 comments on commit ea7d89a

Please sign in to comment.