Guest User

Untitled

a guest
Jan 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. library(shiny)
  2. library(rhandsontable)
  3. library(shinydashboard)
  4. library(ggplot2)
  5. library(dplyr)
  6.  
  7. setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/shiny_interactive_graph")
  8.  
  9. tweets <- data.frame(
  10. city = c("new york", "texas", "texas"),
  11. tweet = c("Test1", "Test", "tst")
  12. )
  13.  
  14.  
  15. shinyApp(
  16. ui = dashboardPage(
  17. dashboardHeader(
  18. title = "Tweetminer",
  19. titleWidth = 350
  20. ),
  21. dashboardSidebar(
  22. width = 350,
  23. sidebarMenu(
  24. menuItem("Menu Item")
  25. )
  26. ),
  27. dashboardBody(
  28. fluidRow(
  29. tabBox(
  30. tabPanel("Set tweets2",
  31. plotOutput('plot',
  32. brush = brushOpts(
  33. id = "plot1_brush"
  34. )),
  35. h4("Selected States"),
  36. verbatimTextOutput("select_states"),
  37. h4("Selected States' Tweets"),
  38. verbatimTextOutput("tweets")
  39. )
  40. )
  41. )
  42. )
  43. ),
  44. server = function(input, output) {
  45.  
  46. output$plot <- renderPlot({
  47.  
  48. all_states <- map_data("state")
  49. # Add more states to the lists if you want
  50. states_positive <-c("new york")
  51. states_negative <- c("texas")
  52. # Plot results
  53. ggplot(all_states, aes(x=long, y=lat, group = group)) +
  54. geom_polygon(fill="grey", colour = "white") +
  55. geom_polygon(fill="green", data = filter(all_states, region %in% states_positive)) +
  56. geom_polygon(fill="red", data = filter(all_states, region %in% states_negative))
  57.  
  58. })
  59.  
  60. selected_points <- reactiveVal()
  61.  
  62. observeEvent(input$plot1_brush,{
  63. all_states <- map_data("state")
  64. selected_points( brushedPoints(all_states, input$plot1_brush))
  65. })
  66.  
  67. observeEvent(selected_points(), {
  68. showModal(modalDialog(
  69. title = "Important message",
  70. tweets[(tweets$city %in% brushed_states()),],
  71. easyClose = TRUE
  72. ))
  73. })
  74.  
  75. output$brush_info <- renderPrint({
  76. all_states <- map_data("state")
  77. brushedPoints(all_states, input$plot1_brush)
  78. })
  79.  
  80. #get states from brushed coordinates
  81. brushed_states <- reactive({
  82. all_states <- map_data("state")
  83. brushed <- brushedPoints(all_states, input$plot1_brush)
  84. unique(brushed$region)
  85. })
  86.  
  87. #this is to show the selected states
  88. output$select_states <- renderText({
  89. brushed_states()
  90. })
  91.  
  92. output$tweets <- renderPrint({
  93. tweets[(tweets$city %in% brushed_states()),]
  94. })
  95.  
  96.  
  97. })
  98.  
  99. tweets[(tweets$city %in% brushed_states()),]
  100.  
  101. texas Test
  102.  
  103. texas Test
  104. texas tst
  105.  
  106. Warning in charToRaw(enc2utf8(text)) :
  107. argument should be a character vector of length 1
  108. all but the first element will be ignored
Add Comment
Please, Sign In to add comment