Skip to content

Instantly share code, notes, and snippets.

@jalapic
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalapic/76f1d9c5789dcbbc9860 to your computer and use it in GitHub Desktop.
Save jalapic/76f1d9c5789dcbbc9860 to your computer and use it in GitHub Desktop.
## app.R ##
library(shiny)
library(sortableR)
ui = shinyUI(fluidPage(
fluidRow(
,tags$h4("sortableR in Shiny - Moving plots")
,tags$div(id="veryUniqueId", class="list-group"
,tags$div(class="list-group-item", plotOutput("myPlot1"))
,tags$div(class="list-group-item", plotOutput("myPlot2"))
,tags$div(class="list-group-item", plotOutput("myPlot3"))
)
),sortableROutput( "mySort" )
)
)
server = function(input,output){
output$mySort <- renderSortableR({
sortableR("veryUniqueId")
})
output$myPlot1 <- renderPlot({
plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
main = "rpois(100, lambda = 5)")
})
output$myPlot2 <- renderPlot({
x <- 0:12
y <- sin(pi/5 * x)
plot(x,y)
})
output$myPlot3 <- renderPlot({
# Define 2 vectors
cars <- c(1, 3, 6, 4, 9)
trucks <- c(2, 5, 4, 5, 12)
plot(cars, type="o", col="blue", ylim=c(0,12))
lines(trucks, type="o", pch=22, lty=2, col="red")
title(main="Autos", col.main="red", font.main=4)
})
}
shinyApp(ui=ui,server=server)`
@timelyportfolio
Copy link

note, you can just replace sortableROutput within ui.R for now inside of fluidPage(...) and remove all the sortableR stuff in server.R.

@timelyportfolio
Copy link

also, there are a couple little bugs in the original. here is my working version.

library(shiny)
library(sortableR)


ui = shinyUI(fluidPage(
  fluidRow(
    tags$h4("sortableR in Shiny - Moving plots")
    ,tags$div(id="veryUniqueId", class="list-group"
      ,tags$div(class="list-group-item", plotOutput("myPlot1"))
      ,tags$div(class="list-group-item", plotOutput("myPlot2"))
      ,tags$div(class="list-group-item", plotOutput("myPlot3"))
    )
  )
  ,sortableR( "veryUniqueId" )
))


server = function(input,output){
   output$myPlot1 <- renderPlot({
    plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
         main = "rpois(100, lambda = 5)")
  })

  output$myPlot2 <- renderPlot({
    x <- 0:12
    y <- sin(pi/5 * x)
    plot(x,y)
  })

  output$myPlot3 <- renderPlot({
    # Define 2 vectors
    cars <- c(1, 3, 6, 4, 9)
    trucks <- c(2, 5, 4, 5, 12)
    plot(cars, type="o", col="blue", ylim=c(0,12))
    lines(trucks, type="o", pch=22, lty=2, col="red")
    title(main="Autos", col.main="red", font.main=4)
  })


}

shinyApp(ui=ui,server=server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment