Selecting record date with selectInput in shiny R

孤街醉人 提交于 2019-12-06 06:07:31

As you are populating the selectInput from your data, you should create the selectInput in your server.R using renderUI & uiOutput

You also need to make your plot reactive based on the selectInput - i.e., so the plot changes when you change your selection. You do this by accessing the value of the selectInput with 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")

  })
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!