ggvis可视化不会出现在主窗格中

时间:2017-11-30 02:59:39

标签: r shiny ggvis

我刚才有光泽,而且这个给了我这么难。我尝试了几个建议,我发现最后反应但没有工作。我不确定我做错了什么。

我尝试了vis< - reactive({})和vis%>%bind_shiny()无法正常工作。任何建议将不胜感激。

ui.R出现但可视化没有,我没有收到错误消息

server.R

library(shiny)
library(ggvis)
library(dplyr)


dataS <-read.csv("https://raw.githubusercontent.com/indianspice/IS608/master/Final%20Project/Data/shinydata.csv",
              stringsAsFactors = FALSE)


function(input, output, session) {
#Filter breaches
breaches <- reactive({
    records <- input$records
    minyear <- input$year[1]
    maxyear <- input$year[2]

    # Apply filters
    b <- dataS %>%
    filter(
    TotalRecords >= records,
    Year >= minyear,
    Year <= maxyear
) %>%
arrange(records)

#Filter by breach
if (input$breach != "All") {
    breach <- paste0("%", input$breach, "%")
    b <- b %>% filter(Breach %like% breach)
}

#Filter by company
if (!is.null(input$company) && input$company != "") {
    company<- paste0("%", input$director, "%")
    b <- b %>% filter(Company %like% company)
}     

reactive({
    xvar_name <- names(axis_vars)[axis_vars == input$year]
    yvar_name <- names(axis_vars)[axis_vars == input$records]

    xvar <- prop("x", as.symbol(input$xvar))
    yvar <- prop("y", as.symbol(input$yvar))

    breaches %>%
        ggvis(x=xvar, y=yvar, stroke = ~breach) %>%
        layer_points() %>%
        add_axis("x", title = xvar_name) %>%
        add_axis("y", title = yvar_name) %>%
        add_legend("stroke", title = "Breach Type", 
                   values = c("Hacking or Malware", 
                              "Unintended Disclosure",
                              "Insider",
                              "Portable Device",
                              "Stationary Device",
                              "Unknown",
                              "Payment Card Fraud",
                              "Physical Loss")) %>%
        scale_nominal("stroke", domain = c("Hacking", 
                                           "Unintended",
                                           "Insider",
                                           "Portable",
                                           "Stationary",
                                           "Unknown",
                                           "Payment",
                                           "Physical"),
                          range = c("red", "orange"))  %>%
        bind_shiny("ggvis", "ggvis_ui")
   }) 

})


}

ui.R

library(shiny)
library(ggvis)

dataS <- read.csv("https://raw.githubusercontent.com/indianspice/IS608/master/Final%20Project/Data/shinydata.csv",
             stringsAsFactors = FALSE)


fluidPage(
    titlePanel("Data Breaches in the United States"),
    #fluidRow(
       column(4,
               h4("Filter Data"),
               sliderInput("records", "Number of records breached",
                           min = 10,
                           max = 1000000,
                           value = 10000,
                           step = 500),
               sliderInput("year", "Year breach reported",
                           sep = "",
                           min = 2005, 
                           max = 2017, 
                           value = c(2007, 2010)),
               selectInput("breach", "Type of breach",
                           c("All", 
                             "Hacking or Malware", 
                             "Unintended Disclosure", 
                             "Insider", 
                             "Portable Device", 
                             "Stationary Device",
                             "Unknown", 
                             "Payment Card Fraud", 
                             "Physical Loss")),
               selectInput("organzation", "Select type of organization",
                           choices = unique(dataS$TypeofOrganization)),
               selectInput("company", "Select company",
                           choices = unique(dataS$Company)
               ),
              textInput("companyName", "Enter company name")
           ),

    #),
    mainPanel(
        uiOutput("ggvis_ui"),
        ggvisOutput("ggvis")
    )


    )

数据

Company TypeofBreach            TypeofOrganization     TotalRecords Year
Bullitt Unintended Disclosure   Educational Institutions    676     2009
Roane   Portable Device         Educational Institutions    14783   2009
Halifax Portable Device         Healthcare Medical Provider 33000   2009
Suffolk Unintended Disclosure   Educational Institutions     300    2009
Penrose Physical Loss           Healthcare Medical Providers  175   2009

1 个答案:

答案 0 :(得分:1)

你在反应中定义了反应,这是不好的。您应该使用breaches定义您的被动(更改)数据reactive - 这很好。然后,您应该使用observe

观察该数据的更改
observe({
   breaches() ... <do something>
   ...
   %>% bind_shiny("ggvis", "ggvis_ui")
})

然后,最后使用bind_shiny。请参阅以下最小示例,了解如何执行此操作(受ggvis help pages启发):

library(shiny)
runApp(list(
  ui = fluidPage(
    sliderInput("slider", "Select rows from mtcars to consider", min=1, max = nrow(mtcars), step = 1, value = c(1,10)),
    ggvisOutput("p"),
    uiOutput("p_ui")
  ),
  server = function(input, output) {

    # define the data according to some input
    data <- reactive({
      mtcars[ input$slider[1] : input$slider[2], ]
    })

    # observe changes in the data and update ggvis plot accordingly
    observe({
     data %>%
        ggvis(~wt, ~mpg) %>%
        layer_points() %>%
        bind_shiny("p", "p_ui")
    })
  }
))

enter image description here