I am very new to R programming and I am trying to build an shiny r app that takes in data on shark attack data and builds a bar chart that specifies the frequency of a kind of attack e.g. Provoked, Unprovoked, Boating etc. I want to be able to filter by the region however. Here is a sample of the dataset.
CaseNumber Year Type Country Area OriginalOrder Region
1642.00.00 1642 Unprovoked USA New York 136 Northeast
1779.00.00 1779 Unprovoked USA Hawaii 152 West
1805.09.00 1805 Invalid USA New York 160 Northeast
1816.09.03 1816 Unprovoked USA Rhode Island 164 Northeast
1817.06.24 1817 Unprovoked USA South Carolina 168 South
1828.00.00 1828 Unprovoked USA Hawaii 181 West
1830.07.26 1830 Unprovoked USA Massachusetts 187 Northeast
1837.00.00 1837 Invalid USA South Carolina 196 South
1837.00.00 1837 Unprovoked USA South Carolina 197 South
Here is what I have so far with my R code. I am able to have the app show me a bar chart with data for all regions but I'm unsure how to update the app so that for example only attacks that happened in the South show up on the bar chart or only attacks in the Northeast region when selected from the dropdown list of regions while having it default to attacks that happened in all regions.
UI
# Link to Shark Attack Data
setwd("C:\\MyData\\Shark-App");
data <- read.csv("Shark.csv");
attach(data);
#Call in shiny
library(shiny)
library(dplyr);
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Name Page
titlePanel("Type of Shark attacks by Region"),
#Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("Area", "Select your Region:",
choices=names(Area)),
hr(),
helpText("Data from The Global Shark Attack File ")
),
# Sprcify Bar Plot input
mainPanel(
plotOutput("sharkPlot")
)
)
)
)
Server
# Link to Shark Attack Data
setwd("C:\\MyData\\Shark-App");
data <- read.csv("Shark.csv");
attach(data);
#Check Data for consistency
dim(data);
names(data);
library(shiny)
#Define a server for the Shiny app
shinyServer(function(input, output) {
# Plot the Shark graph
output$sharkPlot <- renderPlot({
# Render a barplot
barplot(xtabs(~data$Type),
space=F,
col=rainbow(length(levels(data$Type))),
main = input$Area,
ylab= "Number of Attacks",
xlab= "Type")
})
})
I appreciate any help I can get
As you are populating the selectInput
from your data, you should create the selectInput
in your server.R
using renderUI & uiOutput
You also need to make your plot reactive based on the selectInput
- i.e., so the plot changes when you change your selection. You do this by accessing the value of the selectInput
with input$Area
UI
library(shiny)
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Name Page
titlePanel("Type of Shark attacks by Region"),
#Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
## the choices for the selectInput come from the data,
## so we've moved this into the server.R
uiOutput("Area"),
hr(),
helpText("Data from The Global Shark Attack File ")
),
# Sprcify Bar Plot input
mainPanel(
plotOutput("sharkPlot")
)
)
)
)
server.R
library(shiny)
#Define a server for the Shiny app
shinyServer(function(input, output) {
data <- read.csv("Shark.csv");
## create selectInput based on the regions of the data
output$Area <- renderUI({
choices <- unique(as.character(data$Region))
selectInput("Area", "Select your Region:", choices=choices)
})
# Plot the Shark graph
output$sharkPlot <- renderPlot({
# Render a barplot
## filter the data based on the value in `selectInput(Area)`
data_plot <- data[data$Region == input$Area, ]
barplot(xtabs(~data_plot$Type),
space=F,
col=rainbow(length(levels(data_plot$Type))),
main = input$Area,
ylab= "Number of Attacks",
xlab= "Type")
})
})
来源:https://stackoverflow.com/questions/36950456/selecting-record-date-with-selectinput-in-shiny-r