I'm new to shiny and this one has been giving me such a difficult. I tried several suggestions I found to the last reactive but none worked. I am not sure what I am doing wrong.
I tried vis <- reactive({}) and vis %>% bind_shiny() that did not work. Any suggestions will be greatly appreciated.
The ui.R appears but the visualization does not and I do not get an error message
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")
)
)
Data
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
You are defining a reactive inside a reactive, which is bad. You should define your reactive (changing) data breaches
using reactive
- that's fine. Then, you should observe changes of that data using observe
:
observe({
breaches() ... <do something>
...
%>% bind_shiny("ggvis", "ggvis_ui")
})
and then, at the end, use bind_shiny
. See the following minimal example for an introduction how to do it (inspired by 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")
})
}
))