Shiny App String Detection in Shiny App

Hi Everyone,

I am having a problem when I publish my Shiny app and use string detect in their shiny app to create a sidebar panel to show some bar graphs. The code works fine whenever I run my r markdown file locally and then run my app, but when I clear my environment and run the code again, there is an error which reads "Error: object 'Country' not found" . Country a column in my data which I am trying to have as the input to the bar graph code, such that when I select a country that is listed, the bar graph visualizes the data for any observation I have for that particular country.

Here is the code for my R markdown in which I write the file into the folder where my Shiny app is stored. I tried converting my data into a dataframe from a csv at the suggestion of one of my colleagues, and then tried to write in the dataframe as an rds from my markdown and project folder into the Shiny app folder.

If anyone knows where my string detect is going wrong, I'd appreciate any advice on how to fix it!

knitr::opts_chunk$set(echo = FALSE)

# Putting in all of the necessary libraries

library(httr)
library(knitr)
library(dplyr)
library(glue)
library(readxl)
library(janitor)
library(reprex)
library(readxl)
library(lubridate)
library(xml2)
library(anytime)
library(moderndive)
library(gt)
library(maps)
library(png)
library(purrr)
library(reprex)
library(stargazer)
library(tibble)
library(forcats)
library(infer)
library(fs)
library(devtools)
library(rworldmap)
library(googlesheets4)
library(infer)
library(ggplot2)
library(scales)
library(tidyverse)

code for reading in data as csv, making data into dataframe, and saving as rds into folder which has shiny app data


# Loading in previously cleaned data from Wikipedia

diasporas <- read_csv(file = 'diaspora_data.csv') 

diasporas %>%
  slice(1:68) %>%
  mutate(Population = Population/1000)

diasporas.df <- as.data.frame(diasporas)

write_rds(diasporas.df, "diaspora_final_project/diasporasA.rds")

Reading in libraries and data into Shiny App:

# Here I am loading in the library

library(shiny)
library(readr)
library(httr)
library(knitr)
library(anytime)
library(moderndive)
library(gt)
library(maps)
library(png)
library(ggplot2)
library(fs)
library(sf)
library(reprex)
library(stringr)
library(gganimate)

diasporas <- read_rds("./diasporasA.rds")

Here is the UI for my project:

ui <- fluidPage(
    navbarPage("Armenian Diaspora Project",
               tabPanel("Community Information",
                        column(align = "center", width = 12,
                        h1("Welcome to the Armenian Diaspora Project"),
                        h2("Highlights of Communities on Different Continents"),
                        br()
               )),
               tabPanel("Populations of Diaspora Communities",
                        h2("Population of Diaspora Communities"),
                        h4("Check out the bar graphs below to see how populous the Armenian communities
                           are in various countries around the world."),
                        br(),
                        br(),
                        br(),
                        sidebarLayout(
                          sidebarPanel(
                            selectInput(
                              inputId = "input_1",
                              label = "Population of Communities in Various Countries",
                              choices = c(
                                "Argentina" = "Argentina",
                                "Brazil" = "Brazil",
                                "Bulgaria" = "Bulgaria",
                                "Canada" = "Canada",
                                "Egypt" = "Egypt",
                                "France" = "France",
                                "Georgia" = "Georgia",
                                "Germany" = "Brazil",
                                "Greece" = "Greece",
                                "Iran" = "Iran",
                                "Iraq" = "Iraq",
                                "Israel" = "Israel",
                                "Kazakhstan" = "Kazakhstan",
                                "Lebanon" = "Lebanon",
                                "Russia" = "Russia",
                                "Sweden" = "Sweden",
                                "Switzerland" = "Switzerland",
                                "Syria" = "Syria",
                                "Turkey" = "Turkey",
                                "Turkmenistan" = "Turkmenistan",
                                "United Kingdom" = "UK",
                                "Ukraine" = "Ukraine",
                                "United States" = "United States",
                                "Uruguay" = "Uruguay",
                                "Uzbekistan" = "Uzbekistan"
                              )
                            )
                          ),
                        mainPanel(plotOutput("Eurasian_Diaspora")))),
               tabPanel("Diaspora Data Highlights",
                        column(align = "center", width = 12,
                        h1("Types of Diaspora Communities"),
                        plotOutput("historic"),
                        p("Armenian communities existed in the countries you see on above during the Soviet Union Many Armenians had moved around the USSR.")
               )),
               tabPanel("What Influences Number of Communities & Population",
                        column(align = "center", width = 12,
                        br(),
                        p("
                        My data and model is problematic, to say the least. "),
                        )),
               tabPanel("About", 
                        column(align = "center", width = 12,
                        h2("About the Armenian Diaspora Data Project"),
                        p("This project is "),
                        h2("Data on the Diaspora"),
                        p("I am a senior at Harvard College studying Economics with a minor in Government.")
               ))))

# Here, server defines what that output we specified earlier will be
# and now we are actually assigning that output to be our graphic
# and our description for the about.

server <- function(input, output){
    
    output$world_map <- renderImage({
        filename <- "world_map.png"
        list(src = filename)
        },deleteFile = FALSE)
    
    output$latin_map <- renderImage({
        filename <- "latin_map.png"
        list(src = filename)
    },deleteFile = FALSE)
     
    
    output$Eurasian_Diaspora <- renderPlot({
      diasporas %>%
        filter(str_detect(Country,fixed(input$input_1))) %>%
        ggplot(aes(x=Area, y=Population)) + geom_col(color="red", fill = "red") +
        labs(
          title=paste("Population of Communities in", input$input_1),
          subtitle = "Population measured in 1000s",
          x=paste("Communities in", input$input_1),
          y="Population") +
        theme(
          axis.text.x = element_text(face = "bold", size = 12, angle = 45, hjust = 1),
              axis.text.y = element_text(face = "bold",  size = 12, angle = 45, hjust = 1)
              )
    })
    
    getPage <- function() {
      return(includeHTML("regression.html"))
    }
    
    output$regression1 <- renderUI({
      
      getPage()})
    
    
    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- histo.df[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'black', border = 'white',
             xlab = "Number of Communities in a Country",
             ylab = "Number of Countries",
             xlim = c(0,20),
             main = "Frequencies of Community Sizes")
       
    })
}
shinyApp(ui = ui, server = server)

I do not see dplyr in the list of libraries for your shiny app so the filter() function would be the filter function from base R.

2 Likes

That was it, thank you!