根据Shiny图中的选择显示数据子集

时间:2018-01-21 11:44:11

标签: r ggplot2 shiny

我有以下Shiny Application(它实际上有点复杂但是这个更适合复制目的):

library(shiny)
library(rhandsontable)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(twitteR)

setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/bank_mining/test_data")
source("functions.R")

tweets <- data.frame(
  city = c("New york", "Texas"),
  tweet = c("Test1", "Test")
)


shinyApp(
  ui = dashboardPage(
    dashboardHeader(
      title = "Tweetminer",
      titleWidth = 350
    ),
    dashboardSidebar(
      width = 350,
      sidebarMenu(
        menuItem("Menu Item")
      )
    ),
    dashboardBody(
      fluidRow(
        tabBox(
          tabPanel("Set tweets2", 
          plotOutput('plot',
                     brush = brushOpts(
                       id = "plot1_brush"
                     )),
          h4("Brushed points"),
          verbatimTextOutput("brush_info")
        )
        )
      )
    )
  ),
  server = function(input, output) { 


    output$plot <- renderPlot({

      all_states <- map_data("state") 
      # Add more states to the lists if you want
      states_positive  <-c("new york")
      states_negative  <- c("texas")
      # Plot results
      ggplot(all_states, aes(x=long, y=lat, group = group)) +
        geom_polygon(fill="grey", colour = "white") +
        geom_polygon(fill="green", data = filter(all_states, region %in% states_positive)) +
        geom_polygon(fill="red", data = filter(all_states, region %in% states_negative))

      #brush = brushOpts(
      #  id = "plot1_brush"
      #)
    })

    output$brush_info <- renderPrint({
      brushedPoints(all_states, input$plot1_brush)
    })

  })

我想要包括的是选择一个城市并显示数据框子集“推文”的可能性。因此,如果我选择纽约,它应该显示纽约的相关推文。

现在我有了这个功能:

output$brush_info <- renderPrint({
  brushedPoints(all_states, input$plot1_brush)
})

输出坐标。但实际上我想创建另一个步骤,因此可以根据我选择的坐标选择推文。有关如何进行的任何想法?

1 个答案:

答案 0 :(得分:0)

这应该做我认为你所描述的。你在拉丝点功能上几乎就是那里。从那里开始,只需要提取刷子状态并对推文数据帧进行子集化:

library(shiny)
library(rhandsontable)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(twitteR)

setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/bank_mining/test_data")
source("functions.R")

tweets <- data.frame(
  city = c("new york", "texas"),
  tweet = c("Test1", "Test")
)


shinyApp(
  ui = dashboardPage(
    dashboardHeader(
      title = "Tweetminer",
      titleWidth = 350
    ),
    dashboardSidebar(
      width = 350,
      sidebarMenu(
        menuItem("Menu Item")
      )
    ),
    dashboardBody(
      fluidRow(
        tabBox(
          tabPanel("Set tweets2", 
                   plotOutput('plot',
                              brush = brushOpts(
                                id = "plot1_brush"
                              )),
                   h4("Brushed points"),
                   verbatimTextOutput("brush_info"),
                   h4("Selected States"),
                   verbatimTextOutput("select_states"),
                   h4("Selected States' Tweets"),
                   verbatimTextOutput("tweets")
          )
        )
      )
    )
  ),
  server = function(input, output) { 


    output$plot <- renderPlot({

      all_states <- map_data("state") 
      # Add more states to the lists if you want
      states_positive  <-c("new york")
      states_negative  <- c("texas")
      # Plot results
      ggplot(all_states, aes(x=long, y=lat, group = group)) +
        geom_polygon(fill="grey", colour = "white") +
        geom_polygon(fill="green", data = filter(all_states, region %in% states_positive)) +
        geom_polygon(fill="red", data = filter(all_states, region %in% states_negative))

      #brush = brushOpts(
      #  id = "plot1_brush"
      #)
    })

    output$brush_info <- renderPrint({
      brushedPoints(all_states, input$plot1_brush)
    })

    output$brush_info <- renderPrint({
      brushedPoints(all_states, input$plot1_brush)
    })

    #get states from brushed coordinates
    brushed_states <- reactive({
      brushed <- brushedPoints(all_states, input$plot1_brush)
      unique(brushed$region)
    })

    #this is to show the selected states
    output$select_states <- renderText({
      brushed_states()
    })

    #subset the tweets dataframe by the states
    output$tweets <- renderPrint({
      tweets[(tweets$city %in% brushed_states()),]
    })


  })