简体   繁体   中英

"Error in match: 'match' requires vector arguments in R Shiny

I am trying to create a dashboard using R Shiny from NYC Tree Census 2015 . The dashboard should look something like in the picture here > Dashboard in Shiny Picture

My code is mentioned below:

library(shiny)
library(tidyverse)
library(ggplot2)

my_data <- read.csv("/Users/abhikpaul/Documents/Documents/Github/Fiverr/2015_Street_Tree_Census_-_Tree_Data.csv")

ui <- fluidPage(
titlePanel("The Dashboard of Tree Distribution in New York City"),
sidebarLayout(
sidebarPanel(
  
  # Description ----
  helpText("In this page you can get information about the tree distribution, status, health conditions, and species rank in New York City. Please choose the borough that you want to check. It may take 10 seconds for the graphics to load. Thank you for your patience!"),
  
  #Input: Check boxes for Boroughs ----
  checkboxGroupInput("checkboxInput",
                     label = "Borough",
                     choices = list("Bronx",
                                    "Brooklyn", 
                                    "Manhattan",
                                    "Queens",
                                    "Staten Island"),
                     selected = "Bronx"),
),

# Main panel for displaying outputs ----
mainPanel(
  
  # Tabs panel for displaying outputs ----
  tabsetPanel(type = "tabs",
    
    #Output: About ----
    tabPanel("About",
             h3("About this dataset", align = "left"),
             p("The dataset displays the information of trees (including health, status, species, etc.) within the five boroughs in New York City. The dataset is organized by NYC parks & Recreation and partner organizations."),
             
             h3("How to make NYC an urban forest?", align = "left"),
             p("As a group, we are concerned about planting tree and green environments. Therefore, we will focus on identifying the locations that require more taking care of trees, the top species that have the most number of trees in each borough, the health conditions of those species, and the distribution of trees in each borough."),
             
             HTML("<p>For more information, visit: <a href='https://data.cityofnewyork.us/Environment/2015-Street-Tree-Census-Tree-Data/uvpi-gqnh'>2015 NYC Tree Census</a></p>")
             ),
    
    #Output: Status ----
    tabPanel("Status", plotOutput(outputId = "statusplot")),
             )
    )
  )

) )

    server <- function(input, output) {
my_data <- as_tibble(my_data)
my_data <- my_data[my_data$borough %in% checkboxInput,]
my_data <- data.frame(table(my_data$borough,my_data$status))
my_data <- my_data[apply(my_data!=0, 1, all),]

my_data <- my_data %>%
group_by(Var1) %>%
mutate(Percent = (Freq/sum(Freq) * 100))

output$statusplot <- renderPlot({

ggplot(my_data, aes(fill = Var2, y = Percent, x = Var1)) + 
  geom_bar(position = "dodge", stat = "identity")
})
}
shinyApp(ui = ui, server = server)

However, while running the app, I am getting an error as mentioned below

Warning: Error in match: 'match' requires vector arguments
50: %in%
47: server [/Users/abhikpaul/Documents/Documents/GitHub/Fiverr/my_app.R#90]
Error in match(x, table, nomatch = 0L): 'match' requires vector arguments

Can someone help me fix this issue as I am a newbie in R Shiny?

Try this

server <- function(input, output) {
  
  output$statusplot <- renderPlot({
    my_data <- as_tibble(my_data)
    my_data <- my_data[my_data$borough %in% input$checkboxInput,]
    my_data <- data.frame(table(my_data$borough,my_data$status))
    my_data <- my_data[apply(my_data!=0, 1, all),]
    
    my_data <- my_data %>%
      group_by(Var1) %>%
      mutate(Percent = (Freq/sum(Freq) * 100))
    
    ggplot(my_data, aes(fill = Var2, y = Percent, x = Var1)) + 
      geom_bar(position = "dodge", stat = "identity")
  })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM