使用闪亮的R中的selectInput选择记录日期

时间:2016-04-30 03:44:13

标签: r shiny-server shiny user-interaction

我是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代码。我可以让应用程序向我显示包含所有地区数据的条形图,但我不确定如何更新应用程序,以便例如只有南方发生的攻击出现在条形图上或只有东北部的攻击从区域下拉列表中选择的区域,同时默认为在所有区域中发生的攻击。<​​/ p>

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 个答案:

答案 0 :(得分:1)

当您从数据中填充selectInput时,您应该使用renderUI & uiOutput

selectInput中创建server.R

您还需要根据selectInput制作您的情节被动 - 即,当您更改选择时情节会发生变化。您可以通过使用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

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

  })
})