2016-04-30 95 views
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") 

    }) 
}) 

我感谢所有帮助我能

回答

1

当你正在填充从您的数据selectInput,你应该使用建立在server.RselectInputrenderUI & uiOutput

根据selectInput,您还需要根据作出反应 - 即,当您更改选择时情节会发生变化。您可以通过与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访问selectInput的价值做到这一点

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

    }) 
}) 
+0

谢谢你,它的工作,我明白我是不是做对了。 – Barzul