MyNixOS website logo
Description

Shiny Drag-n-Drop.

Add functionality to create drag and drop div elements in shiny.

shinyDNDBuild Status

shinyDND is an R package to create Shiny drag and drop elements in R.

See the example at: https://ayayron.shinyapps.io/dragndrop/. That example shown below is also available in the package /examples/app.R. (It will be added to shinyapps.io once the package is accepted in CRAN.)

The package can be installed and loaded with:

devtools::install_github('ayayron/shinydnd')
library(shinyDND)

Functions

  • dragUI: Draggable div element.
  • dragSetUI: Set of draggable div elements.
  • dropUI: Droppable div element.

###dragUI Draggable div elements can now be easily created in your shiny code by running:

dragUI("div6", "bar")

where div6 is the name of the div element and bar is the text in the element. The elements can be styled using css (the class is currently dragelement) and setting the class parameter.

dragUI("div5", "foo", style = "background-color:red", class = "dragelement")

Also, the elements don't have to just be text. You can use HTML tag elements (e.g. tags$a("mylink", href="#mylink")) or HTML() inside the element.

dragUI("div4",tags$a("a",href = "foo"))

###dropUI A drop area can be created for these draggable elements with the function:

dropUI("div3")

If you try to drop more than one draggable element into a drop area, they are placed horizontally. If you want to place them vertically, you can add the parameters vertical = TRUE and nrow = X, where X is the number of rows that will be generated in the drop area.

dropUI("div3", vertical = TRUE, nrow = 4)

The drop area can be made reactive, such that when something is dragged into it a reactive function will run. Using the observeEvent function where the event is the input name of the dropUI div, can trigger a server action. Here, if you drag each element into the dropUI it will print the name of the element.

observeEvent(input$div2, {
    output$foo = renderText(paste("The dropUI element currently contains:", input$div2))
})

Similar to the dragUI elements, the element can be styled using the style parameter or the class parameter (the class is currently dropelement) in css.

###dragSetUI To make it easier to create multiple draggable elements there is a secoond function called DragSetUI. Here you can specify each of the elements in a list and it will create multiple elements with the div name prefix you feed it.

dragSetUI("div1", textval = list("foo", tags$a("a", href = "bar"), "baz"))

Example

After installing the package you can run this example app: runApp(system.file('examples', package='shinyDND')). For readability, this code below excludes the explanations above.

library(shiny)
library(shinyDND)

# Define UI for application that draws a histogram
ui <- shinyUI(
  mainPanel(
    h2("DragUI"),
    dragUI("div6","bar"),
    dragUI("div5","foo", style = "background-color:red", class = "dragelement"),
    dragUI("div4",tags$a("a",href = "foo")),
    h2("Drop UI"),
    dropUI("div3",vertical = TRUE, nrow = 4),
    dropUI("div2"),
    h2("DragSetUI"),
    dragSetUI("div1", textval = list("foo",tags$a("a",href = "bar"),"baz"))
  )
)

# server with reactive for observing reactive drop event
server = shinyServer(function(input, output,session) {
  observeEvent(input$div2,{
    output$foo = renderText(
      paste("The dropUI element currently contains:", input$div2))
  })
})

# Run the application 
shinyApp(ui = ui, server = server)
Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows