r语言 - 根据闪亮图形中的选择显示数据子集



>我有以下闪亮的应用程序(它实际上有点复杂,但这个更适合复制目的(:

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)
})

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

这应该按照我认为您所描述的去做。你几乎在那里使用画笔点功能。从那里开始,只需提取刷状态并子集化推文数据帧:

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()),]
    })

  })

最新更新