0

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)

1 Answers1

0

I can't reproduce your errors because I dont have the data. Can you reproduce the

NFA 2018 Edition.csv data?

What if you tried %in% instead of == in:

filter(record == input$type,
         year == input$year,
         country == input$country)
  • Welcome to Stack Overflow! This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. – Johan Mar 13 '19 at 15:48