My plot is totally false when I ran my shinyapp. I have selected all the "country" variable using ShinyWidget's pickerInput, and it was supposed to generate a geom_col stacked plot with all the country on the x-axis, the value of the available variable (based on the selected "Year" and "Record") on the y-axis. But the result turns out to be missing 90% of the countries list, with "Records" not stacked, but separately and not all of them are generated. Everytime I change the variable, I will receive an error:
Warning in ==.default
(country, input$country) :
longer object length is not a multiple of shorter object length
Warning in is.na(e1) | is.na(e2) :
longer object length is not a multiple of shorter object length
Before, I melted the dataset, and received this too:
Warning: attributes are not identical across measure variables; they will be dropped
I need help with errors that I am receiving, especially in my code
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(DT)
library(shinythemes)
library(shinyWidgets)
library(gganimate)
library(reshape2)
NFA <- read.csv("NFA 2018 Edition.csv")
NFA$year <- as.factor(NFA$year)
levels(NFA$record)
levels(NFA$record)[levels(NFA$record) == "BiocapPerCap"] <- "Biocapacity (gha per person)"
levels(NFA$record)[levels(NFA$record) == "BiocapTotGHA"] <- "Biocapacity (gha)"
levels(NFA$record)[levels(NFA$record) == "EFConsPerCap"] <- "Ecological Footprint (gha per person)"
levels(NFA$record)[levels(NFA$record) == "EFConsTotGHA"] <- "Ecological Footprint (gha)"
levels(NFA$record)
NFA <- NFA %>%
select(country,year, record, crop_land,grazing_land,forest_land,fishing_ground,built_up_land,carbon)
NFA <- melt(data = NFA, id = c("country","year","record"))
levels(NFA$variable) levels(NFA$record)
Define UI for application
ui <- fluidPage(
titlePanel(title = "Global Footprint Network: Comparing Countries"),
hr(),
fluidRow(
#FirstInput
column(width = 6,
pickerInput(
inputId = "country",
label = "Select Country or Region:",
choices = levels(NFA$country),
options = list('actions-box' = TRUE),
multiple = TRUE,
selected = levels(NFA$country),
width = "100%"
)
),
#SecondInput
column(width = 6,
selectInput(
inputId = "year",
label = "Select Year:",
choices = levels(NFA$year),
selected = 2013,
width = "120%"
)
)
),
fluidRow(
#ThirdInput
tags$style(HTML(".radio-inline {margin-right: 23px;}")),
column(width = 12,
radioButtons(
inputId = "type",
label = "Select Type:",
choices = c("Biocapacity (gha per person)",
"Biocapacity (gha)",
"Ecological Footprint (gha per person)",
"Ecological Footprint (gha)"),
selected = "Ecological Footprint (gha per person)",
inline = T
)
)
),
fluidRow(
column(width = 12,
tabsetPanel(
tabPanel(
"Plot",
plotlyOutput(outputId = "NFAplot")
),
tabPanel(
"Data Table",
dataTableOutput(
outputId = "nfadata"),
style = 'overflow-x : scroll, overflow-y : scroll'
)
)
)
)
)
Define server logic
server <- function(input, output, session) {
output$NFAplot <- renderPlotly({
NFAep <- NFA %>%
filter(record == input$type,
year == input$year,
country == input$country) %>%
ggplot(aes(x = country,y = value)) +
geom_col(aes(fill = variable), position = "stack")
ggplotly(NFAep)
})
model.data <- reactive({
subset(NFA, record %in% input$type & year %in% input$year & country %in% input$country)
})
output$nfadata <- renderDataTable({model.data()})
}
Run the application
shinyApp(ui = ui, server = server)