2
我对R编程非常陌生,我试图构建一个闪亮的应用程序,它接收有关鲨鱼攻击数据的数据并构建指定某种攻击频率的条形图例如挑衅,无端,划船等我想能够过滤该地区然而。这是一个数据集的示例。用selectInput选择记录日期闪亮R
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
这是我到目前为止与我的R代码。我可以让应用程序向我展示包含所有区域数据的条形图,但我不确定如何更新应用程序,以便例如只有发生在南方的攻击显示在条形图上,或者只显示在东北部的攻击从区域的下拉列表中选择区域,同时使其默认为发生在所有区域中的攻击。
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")
)
)
)
)
服务器
# 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")
})
})
我感谢所有帮助我能
谢谢你,它的工作,我明白我是不是做对了。 – Barzul