Why haven't I built my own Shiny widgets?
Why haven't I installed this awesome drag and drop package?
September 13, 2016
Why haven't I built my own Shiny widgets?
Why haven't I installed this awesome drag and drop package?
Data %>% InsightsData %>% Insights %>% Dashboard!
But if you just want to share your insights, why not just use pandoc/knitr/Rmarkdown?
Data %>% Insights %<>% Dashboard!
dragUI("mydrag", "Element Name", style = "background-color:red", class = "dragelement")
dropUI("mydrop", row_n = 4, col_n = 3)
dragSetUI("mydragset", textval = names(mtcars))
observeEvent(input$mydrop, {
output$myplot = renderPlot(input$drop_xval, input$drop_yval)
})
Available on CRAN (shinyDND) or devtools::install_github('ayayron/shinydnd')
dragUI <- function (id, ..., style = NULL, class = "dragelement") {
dragUI = htmltools::tags$div(id = id, class = class, draggable = TRUE,
style = style, list(...))
htmltools::attachDependencies(dragUI, shinyDNDDep) }
dropUI <- function (id, style = NULL, class = "dropelement", row_n = 1, col_n = 1) {
if (row_n > 1 | col_n > 1) {
row_pct = 100/row_n; col_pct = 100/col_n;
div_element = htmltools::tags$div(style = paste0("height:",
row_pct, "%;", "width:", col_pct, "%;", "display:table-cell;"))
div_row = list(htmltools::tags$div(rep(list(div_element),
col_n), style = paste0("height:", row_pct, "%;",
"width:", col_pct, "%;", "display:table-row;")))
divlist = rep(div_row, row_n)
}
...
//on document ready doesn't work with newly created renderUI() elements
$(document).bind('DOMNodeInserted', function(){
$(".dropelement").on("dragover",function(e){
e.preventDefault();
});
$(".dragelement").on("dragstart",function(e){
e.originalEvent.dataTransfer.setData("Text",e.target.id);
});
$(".dropelement").on("drop",function(e){
e.preventDefault();
var data=e.originalEvent.dataTransfer.getData("Text");
e.target.appendChild(document.getElementById(data));
var el = $(e.target);
el.trigger("change");
});
});
var dragDropBinding = new Shiny.InputBinding();
// Create reactivity for a drop element
$.extend(dragDropBinding, {
find: function(scope) {return $(scope).find(".dropelement");},
getValue: function(el) {return $(el).text();},
setValue: function(el) {$(el).text();},
subscribe: function(el, callback) {
$(el).on("change", function(e) {callback();});},
unsubscribe: function(el) {$(el).off(".dragDropBinding");},
getType: function() {return "dragdropshiny.dropper";}
});
Shiny.inputBindings.register(dragDropBinding);
.onAttach <- function(...) {
# register the js input handler to make the element reactive
shiny::registerInputHandler("dragdropshiny.dropper", function(data,...) {
if (is.null(data) | data == "")
return(NULL)
else
return(data)
}, force = TRUE)
# Create link to javascript and css files for package, referenced below
shiny::addResourcePath("shinydnd", system.file("www", package = "shinyDND"))
}
# htmlDependency js and css will be used in other functions with attachDependency
shinyDNDDep = htmltools::htmlDependency("shinyDND", packageVersion("shinyDND"),
src = c("href" = "shinydnd"), script = "dragndrop.js", stylesheet = "dragndrop.css")
Don't need 's, need PRs!
Good Artists Copy; Great Artists (take open source tools built in other languages and adapt them to be available in their language of choice)
Thank you!
ayayron.shinyapps.io/ShinyDND_BARUG_Lightning_Talk_Sept2016