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

Selecting record date with selectInput in shiny R

我是 R 编程的新手,我正在尝试构建一个闪亮的 r 应用程序,它接收鲨鱼攻击数据并构建一个条形图来指定一种攻击的频率,例如Provoded、Unprovoked、Boating 等。不过,我希望能够按地区过滤。这是数据集的示例。

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

  })
})

我很感激能得到的任何帮助

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

server.R 中创建 selectInput

您还需要根据 selectInput 使您的情节 具有反应性 - 即,当您更改选择时情节会发生变化。您可以通过使用 input$Area

访问 selectInput 的值来执行此操作

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

  })
})