From a1398acc575ca4f39dd502d0f3eb9c878a52795c Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Mon, 26 Oct 2015 12:50:57 +1300 Subject: [PATCH 01/10] Add onRefresh callback --- inst/htmlwidgets/rpivotTable.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/inst/htmlwidgets/rpivotTable.js b/inst/htmlwidgets/rpivotTable.js index ef53cc3..7759dd2 100755 --- a/inst/htmlwidgets/rpivotTable.js +++ b/inst/htmlwidgets/rpivotTable.js @@ -27,6 +27,17 @@ HTMLWidgets.widget({ x.params.sorters = eval("("+x.params.sorters[0]+")") } } + + if (typeof x.params.onRefresh != "undefined") { + console.log("Found onrefresh"); + console.log(x.params.onRefresh); + if (typeof x.params.onRefresh == "string") { + console.log("its a string"); + x.params.onRefresh = eval("("+x.params.onRefresh+")"); + console.log("after eval"); + console.log("x.params.onRefresh"); + } + } $('#'+el.id).pivotUI( x.data, x.params, true From 60fc24324921ad4b21394b32f200476944415f3a Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Mon, 26 Oct 2015 13:14:54 +1300 Subject: [PATCH 02/10] Handle JS or string --- inst/htmlwidgets/rpivotTable.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/inst/htmlwidgets/rpivotTable.js b/inst/htmlwidgets/rpivotTable.js index 7759dd2..052bd21 100755 --- a/inst/htmlwidgets/rpivotTable.js +++ b/inst/htmlwidgets/rpivotTable.js @@ -31,11 +31,17 @@ HTMLWidgets.widget({ if (typeof x.params.onRefresh != "undefined") { console.log("Found onrefresh"); console.log(x.params.onRefresh); - if (typeof x.params.onRefresh == "string") { + if (typeof x.params.onRefresh[0] == "string") { console.log("its a string"); - x.params.onRefresh = eval("("+x.params.onRefresh+")"); + x.params.onRefresh = eval("("+x.params.onRefresh[0]+")"); console.log("after eval"); - console.log("x.params.onRefresh"); + console.log(x.params.onRefresh); + } + else { + console.log("its NOT a string"); + x.params.onRefresh = onRefresh[0]; + console.log("after eval"); + console.log(x.params.onRefresh); } } From 44bc416100b16624b68184e53d034ed3c737b075 Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Mon, 26 Oct 2015 13:36:45 +1300 Subject: [PATCH 03/10] test --- R/rpivotTable.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/rpivotTable.R b/R/rpivotTable.R index 1f886ea..dcb051d 100755 --- a/R/rpivotTable.R +++ b/R/rpivotTable.R @@ -28,6 +28,7 @@ rpivotTable <- function( cols = NULL, aggregatorName = NULL, vals = NULL, + onRefresh = NULL, ..., width = NULL, height = NULL From fbdede47b77ae7886980066edbd8d8ed0edf103b Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Mon, 26 Oct 2015 13:37:41 +1300 Subject: [PATCH 04/10] test --- inst/htmlwidgets/rpivotTable.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/inst/htmlwidgets/rpivotTable.js b/inst/htmlwidgets/rpivotTable.js index 052bd21..f541277 100755 --- a/inst/htmlwidgets/rpivotTable.js +++ b/inst/htmlwidgets/rpivotTable.js @@ -29,19 +29,8 @@ HTMLWidgets.widget({ } if (typeof x.params.onRefresh != "undefined") { - console.log("Found onrefresh"); - console.log(x.params.onRefresh); - if (typeof x.params.onRefresh[0] == "string") { - console.log("its a string"); - x.params.onRefresh = eval("("+x.params.onRefresh[0]+")"); - console.log("after eval"); - console.log(x.params.onRefresh); - } - else { - console.log("its NOT a string"); - x.params.onRefresh = onRefresh[0]; - console.log("after eval"); - console.log(x.params.onRefresh); + console.log(x.params.onRefresh) + // x.params.onRefresh = onRefresh[0]; } } From 26bdaa169a71eee9b39f5d5f66c91a365effa713 Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Mon, 26 Oct 2015 13:38:07 +1300 Subject: [PATCH 05/10] Update rpivotTable.js --- inst/htmlwidgets/rpivotTable.js | 1 - 1 file changed, 1 deletion(-) diff --git a/inst/htmlwidgets/rpivotTable.js b/inst/htmlwidgets/rpivotTable.js index f541277..58fa346 100755 --- a/inst/htmlwidgets/rpivotTable.js +++ b/inst/htmlwidgets/rpivotTable.js @@ -31,7 +31,6 @@ HTMLWidgets.widget({ if (typeof x.params.onRefresh != "undefined") { console.log(x.params.onRefresh) // x.params.onRefresh = onRefresh[0]; - } } $('#'+el.id).pivotUI( From 8dd6b5b0e09406271528a46cf2daa41d158d0fbc Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Mon, 26 Oct 2015 13:40:40 +1300 Subject: [PATCH 06/10] Update rpivotTable.R --- R/rpivotTable.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/rpivotTable.R b/R/rpivotTable.R index dcb051d..6b79a9f 100755 --- a/R/rpivotTable.R +++ b/R/rpivotTable.R @@ -45,6 +45,7 @@ stop( "data should be a data.frame or data.table", call.=F) cols = cols, aggregatorName = aggregatorName, vals = vals, + onRefresh = onRefresh, ... ) From e27b31c3a25447aedae4e1bd3245b062db84fa34 Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Mon, 26 Oct 2015 13:44:14 +1300 Subject: [PATCH 07/10] undo --- R/rpivotTable.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/rpivotTable.R b/R/rpivotTable.R index 6b79a9f..1f886ea 100755 --- a/R/rpivotTable.R +++ b/R/rpivotTable.R @@ -28,7 +28,6 @@ rpivotTable <- function( cols = NULL, aggregatorName = NULL, vals = NULL, - onRefresh = NULL, ..., width = NULL, height = NULL @@ -45,7 +44,6 @@ stop( "data should be a data.frame or data.table", call.=F) cols = cols, aggregatorName = aggregatorName, vals = vals, - onRefresh = onRefresh, ... ) From 36b1e1ee387329fb9e4e7d23cee96d9a2d1540ff Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Mon, 26 Oct 2015 14:51:56 +1300 Subject: [PATCH 08/10] fix for x.params arriving as arrays --- inst/htmlwidgets/rpivotTable.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inst/htmlwidgets/rpivotTable.js b/inst/htmlwidgets/rpivotTable.js index 58fa346..531a499 100755 --- a/inst/htmlwidgets/rpivotTable.js +++ b/inst/htmlwidgets/rpivotTable.js @@ -29,8 +29,7 @@ HTMLWidgets.widget({ } if (typeof x.params.onRefresh != "undefined") { - console.log(x.params.onRefresh) - // x.params.onRefresh = onRefresh[0]; + x.params.onRefresh = x.params.onRefresh[0]; } $('#'+el.id).pivotUI( From 5f895237ed90729d4e778d1d7df6858e2191ea29 Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Sat, 11 Jun 2016 17:33:24 +1200 Subject: [PATCH 09/10] RStudio addin wrapper --- R/rpivotAddin.R | 56 +++++++++++++++++++++++++++++++++++++++++ inst/rstudio/addins.dcf | 4 +++ 2 files changed, 60 insertions(+) create mode 100644 R/rpivotAddin.R create mode 100644 inst/rstudio/addins.dcf diff --git a/R/rpivotAddin.R b/R/rpivotAddin.R new file mode 100644 index 0000000..3c630fb --- /dev/null +++ b/R/rpivotAddin.R @@ -0,0 +1,56 @@ +getDataFrames = function() +{ + # ls(envir=.GlobalEnv)[sapply(ls(envir=.GlobalEnv), function(x){is.data.frame(get(x))})] + if ((length(ls()) > 0) & (length(sapply(.GlobalEnv, is.data.frame)) > 0) & any(sapply(.GlobalEnv, is.data.frame))) + return(names(which(sapply(.GlobalEnv, is.data.frame)))) + else + { + data(iris) + return(names(which(sapply(.GlobalEnv, is.data.frame)))) + } +} + +rpivotAddin <- function() { + library(shiny) + library(rpivotTable) + library(miniUI) + library(rstudioapi) + + ui <- miniPage( + + # css hack to provide space for a select input in the gadgetTitleBar + tags$head(tags$style(HTML(" + .gadget-title .shiny-input-container { + position: relative; + height: 30px; + margin: 6px 10px 0; + z-index: 10; + }"))), + + gadgetTitleBar("Pivot Table Gadget", + left=miniTitleBarButton("done", "Done", primary=T), + right=selectInput("dataset", NULL, choices = getDataFrames())), + + miniContentPanel( + rpivotTableOutput("mypivot") + ) + ) # minipage + + server <- function(input, output, session) { + + output$mypivot <- renderRpivotTable({ + rpivotTable(getSelectedDF()) + }) + + observeEvent(input$done, { + stopApp(TRUE) + }) + + getSelectedDF <- reactive({ + eval(parse(text = input$dataset)) + }) + + } # server + + runGadget(shinyApp(ui, server), viewer = paneViewer()) +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf new file mode 100644 index 0000000..343519d --- /dev/null +++ b/inst/rstudio/addins.dcf @@ -0,0 +1,4 @@ +Name: rpivotTable +Description: Pivot Table +Binding: rpivotAddin +Interactive: true From af03e853c822b2bf1c87b49c769f3f111f7dce50 Mon Sep 17 00:00:00 2001 From: dkilfoyle Date: Sat, 11 Jun 2016 17:36:44 +1200 Subject: [PATCH 10/10] cleaner code for getdataframes --- R/rpivotAddin.R | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/R/rpivotAddin.R b/R/rpivotAddin.R index 3c630fb..e93c8dd 100644 --- a/R/rpivotAddin.R +++ b/R/rpivotAddin.R @@ -1,13 +1,8 @@ getDataFrames = function() { - # ls(envir=.GlobalEnv)[sapply(ls(envir=.GlobalEnv), function(x){is.data.frame(get(x))})] - if ((length(ls()) > 0) & (length(sapply(.GlobalEnv, is.data.frame)) > 0) & any(sapply(.GlobalEnv, is.data.frame))) - return(names(which(sapply(.GlobalEnv, is.data.frame)))) - else - { + if ((length(ls()) == 0) | (length(sapply(.GlobalEnv, is.data.frame)) == 0) | (any(sapply(.GlobalEnv, is.data.frame))==F)) data(iris) - return(names(which(sapply(.GlobalEnv, is.data.frame)))) - } + return(names(which(sapply(.GlobalEnv, is.data.frame)))) } rpivotAddin <- function() {