Thanks so much @raytong I changed the code just a tiny bit because I want to be able to delete the cloned block by dragging it anywhere into the original area, but now I have another question:
I want the draggable element to have a selectInput, but since the selected option is queried using input$id it's only printing the id of the original block in the Drag area - can I use JQuery to find all the select elements in the Put Here area and add their selected option to the dataframe?
I have it working in a fiddle but I'm struggling to adapt it to Shiny: http://jsfiddle.net/MayaGans/dszvynj5/42/
library(shiny)
library(shinyjs)
library(shinyjqui)
BLOCKS <- c("dropdown", "Block")
Blocks <- function(data, name)
{
div(style = "
text-align: center;
font-size: 12px;
background-color: #A9A9A9;
border-radius: 10px;
color: black; margin-bottom: 5px;
min-width: 80px;
",
drag = name,
id = name,
class = "cloned",
if (name == "dropdown") {
# how do I abstractly make the ids ttest_1 and ttest_2
# based on the occurances in the vector?
selectInput(name, "Dropdown", choices = c("1", "2", "3"), selectize = FALSE)
} else {
name
}
)
}
ui <- fluidPage(
tags$head(
tags$script(
"$( function() {
$( '.delete_dropped' ).droppable({
drop: function(e, ui) {
$(ui.helper).remove();
}
})
});
$( function() {
$( '.cloned' ).draggable({
helper: 'clone',
connectToSortable: '#dest',
drop: function(e, ui) {
$(ui.helper).remove();
}
})
});"
)),
sidebarPanel(
fluidRow(column(6, h5("Drag"),
jqui_sortable(div(id = "source", class="delete_dropped",
lapply(BLOCKS, Blocks, data = BLOCKS),
style = "border: 1px solid black;padding:10px;min-height:50px;"),
options = list(connectWith = "#dest"))),
column(6, h5("Put Here"),
jqui_sortable(div(id = "dest", style = "border: 1px solid black;padding:10px;min-height:50px"),
options = list(connectWith = "#source")))
)),
mainPanel(tableOutput("debug"))
)
server <- function(input, output, session) {
output$debug <- renderTable({
# print the block text and make unique
# maybe I can use these unique names to rename the ids of the selectInputs
# then I can input[["Dropdown"]], input[["Dropdown.1"]], etc etc to get the selected option?
t <- data.frame( id = make.unique(unlist(input$dest_order$text)))
t$input <- ifelse(grepl('^Block', t$id), NA, paste0("input[[", t$id, "]]"))
t
})
}
shinyApp(ui, server)
Thoughts? Again, I really can't thank you enough!