2016-04-30 55 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