0

Good day, I've been stuck with my shiny app for the last couple of days, the application works fine locally but whenever I open it on shinyapps.io it won't show the plot_geo map. It just says Error: An error has occurred. Check your logs or contact app author for clarification .

this is the link to my app. The problem is in the "world Co2 emission" map.

I also saw a similar post here So I tried this solution but the problem still persists.

So I check my logs and this is what I find:

2022-05-15T15:43:22.365650+00:00 shinyapps[6305171]: Warning: Error in gsub: input 
 string 31 is invalid in this locale
2022-05-15T15:43:22.365776+00:00 shinyapps[6305171]:   123: FUN
2022-05-15T15:43:22.365976+00:00 shinyapps[6305171]:   119: FUN
2022-05-15T15:43:22.365875+00:00 shinyapps[6305171]:   121: FUN
2022-05-15T15:43:22.365927+00:00 shinyapps[6305171]:   120: lapply
2022-05-15T15:43:22.366368+00:00 shinyapps[6305171]:    96: output$Map
2022-05-15T15:43:22.366025+00:00 shinyapps[6305171]:   118: lapply
2022-05-15T15:43:22.366573+00:00 shinyapps[6305171]:     7: connect$retryingStartServer
2022-05-15T15:43:22.366126+00:00 shinyapps[6305171]:   116: plotly_build.plotly
2022-05-15T15:43:22.366410+00:00 shinyapps[6305171]:    15: <Anonymous>
2022-05-15T15:43:22.366521+00:00 shinyapps[6305171]:     8: retry
2022-05-15T15:43:22.366280+00:00 shinyapps[6305171]:   110: func
2022-05-15T15:43:22.365718+00:00 shinyapps[6305171]:   124: gsub
2022-05-15T15:43:22.366327+00:00 shinyapps[6305171]:    97: renderFunc
2022-05-15T15:43:22.366177+00:00 shinyapps[6305171]:   112: 
getFromNamespace("prepareWidget", "plotly")
2022-05-15T15:43:22.366470+00:00 shinyapps[6305171]:    13: fn
2022-05-15T15:43:22.366229+00:00 shinyapps[6305171]:   111: shinyRenderWidget
2022-05-15T15:43:22.366077+00:00 shinyapps[6305171]:   117: translate_linebreaks
2022-05-15T15:43:22.366622+00:00 shinyapps[6305171]:     6: eval
2022-05-15T15:43:22.365825+00:00 shinyapps[6305171]:   122: lapply
2022-05-15T15:43:22.366670+00:00 shinyapps[6305171]:     5: eval

Here's my code:

app.R

library(tidyverse)
library(rnaturalearth)
library(shiny)
library(plyr)
library(dplyr)
library(plotly)
library(ggplot2)
library(gganimate)
library(sf)
library(uuid)
library(shinydashboard)
library(fontawesome)


source("curl.R")
world <- ne_countries(scale = "small", returnclass = "sf")

#####################
worldEmission <- world %>% select(geometry,name,iso_a3) %>%
  right_join(data, by = c("iso_a3" = "ISO.3166.1.alpha.3" )) 
worldEmission <- worldEmission %>% mutate(selected = TRUE)

maxperCapita <- max(worldEmission$Per.Capita)
worldEmission[is.na(worldEmission)] = 0
worldContinents <- data1 %>% select(Entity, Continent) %>% 
  right_join(worldEmission, by = c("Entity" = "Country"))
worldContinents$Year <- as.numeric(worldContinents$Year)

countryNames <-unique(worldEmission$Country)

#####################


ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("World Co2 Emission", tabName = "Map", icon = icon('map')), 
      menuItem("Continent Co2 Emission", tabName = "Continent",icon = icon('chart-bar')),
      menuItem("Emission Factors", tabName = "Emission", icon = icon('chart-line')) ,
      menuItem("Linear Regression", tabName = "Regression", icon = icon('arrow-alt-circle-up')) 
    )
  ),
  dashboardBody(
    tabItems(
      tabItem("Map",
              h1("Map"),
              box(plotlyOutput("Map"), wdith = 3)
              ),
      tabItem("Continent",
              box(plotOutput("Bargraph"), wdith = 12)
              ),
      tabItem("Emission",
              box(plotOutput("Linegraph"), wdith = 12)
              ),
      tabItem("Regression",
              box(plotOutput("Linearregression"), wdith = 12)
              )
    )#tabItems
    
  )#dashboardBody
  
)#dashboardPage
      
      
server <- function(input, output) {
  
  output$Map <- renderPlotly({
    chorplethMap <- plot_geo(worldEmission,
                             locationmode = countryNames,
                             color = ("black"),
                            frame = ~Year) %>%
       add_trace(worldEmission,
                 type = "choropleth",
                 locations = ~iso_a3,
                z = ~Per.Capita,
                zmin = 0,
                color = ~Per.Capita,
                colorscale = 'reds') %>%
       layout(geo = list(scope = countryNames))
    chorplethMap
  })
  
  output$Bargraph <- renderPlot({
    
    worldContinents<-na.omit(worldContinents)
    barchart <- ggplot(data = worldContinents, 
                       aes(x = Continent, y = `Per.Capita`,fill = Continent)) +
      stat_summary(geom = "col", fun = mean, width= 0.7, col = "gray50") + 
      geom_bar(stat = "identity") +
      theme_light(base_size = 20) 
    barchart
  })
  
  output$Linegraph <- renderPlot({
    
    data2 <- na.omit(data)
    totalInYears <- aggregate(data2[c("Coal", "Oil", "Gas", "Cement")], by = data2["Year"], sum)
    
    linechart <- ggplot(data = totalInYears, aes(x = Year)) + 
      geom_line(aes(y = Coal), color = "orange", lwd = 1) +
      geom_line(aes(y = Oil), color = "pink",lwd = 1) +
      geom_line(aes(y = Gas), color = "yellow",lwd = 1) +
      geom_line(aes(y = Cement), color = "green",lwd = 1) + theme_dark()
    
    linechart
  })
  output$Linearregression <- renderPlot({
    
    linearRegression <- subset(data, select = c("Country", "Year", "Per.Capita"))
    linearRegression1 <- linearRegression[which(linearRegression$Country == 'Global'),]
    plot(linearRegression1$Year, linearRegression1$Per.Capita)
    linearRegression2 <- lm(Per.Capita ~ Year, data = linearRegression1)
    summary(linearRegression2)
    abline(linearRegression2, col = "blue")
    
    shapiro.test(linearRegression1$Per.Capita)
  })
}


shinyApp(ui = ui, server = server)

curl.R

library(curl)

x <- tempfile()
y <- tempfile()

curl_download("https://raw.githubusercontent.com/FrancisDiesto/CarbonEmmissionPerCapita/main/GCB2021v34_MtCO2_flat.csv", x)
data <- read.csv(x)

curl_download("https://raw.githubusercontent.com/FrancisDiesto/WorldPopulation/main/continents-according-to-our-world-in-data.csv", y)
data1 <- read.csv(y)

this is supposed to be the result: locally run result

Thank you

Angry birds
  • 73
  • 1
  • 5
  • I am presuming you have already tried changing `Sys.getlocale()` in shiny to whatever the value it shows in your local environment? – monte May 15 '22 at 16:44
  • @monte I used `Sys.setlocale(locale ="C")` as what the poster did in the similar post. – Angry birds May 15 '22 at 16:49
  • you can try comparing `Sys.getlocale()` from both the system and set them accordingly i.e. check what's the value of `Sys.getlocale()` on your local machine and what's the value of `Sys.getlocale()` in shiny environment. I think both are different and setting them same may resolve the issue. – monte May 16 '22 at 04:14
  • 1
    probably something relatable: https://stackoverflow.com/questions/61656119/setting-a-different-locale-for-specific-shiny-apps-only-on-shinyapps-io – monte May 16 '22 at 04:17

1 Answers1

0

My friend found the solution and @monte also pointed it out, I was just using a different syntax in my previous tries but this one worked Sys.setlocale("LC_ALL", "C") Thank you.

Angry birds
  • 73
  • 1
  • 5