Zaktualizuj 'projekt.R'

This commit is contained in:
Patryk Łukasiewicz 2023-05-05 00:01:05 +02:00
parent 5d44e8de2b
commit e8cd163012

382
projekt.R
View File

@ -1,159 +1,223 @@
library(shiny) # Main library library(shiny) # Main library
library(ggplot2) # Plots library(ggplot2) # Plots
library(dplyr) # Data manipulate library(dplyr) # Data manipulate
library(shinythemes) library(shinythemes)
library(plotly) library(plotly)
CO_data <- read.csv("./data.csv", header= TRUE) library(sf)
library(rnaturalearth)
col_names = colnames(CO_data) library(ggspatial)
library(ggrepel)
countries <- unique(CO_data['country'])
options(scipen=999)
ui <- navbarPage(
titlePanel(title=div(img(src="https://siw.amu.edu.pl/__data/assets/file/0004/162751/logo_wersja-podstawowa_granat_1.jpg", width = 50, height = 50), 'explore CO2 data')), CO_data <- read.csv("./data.csv", header= TRUE)
tabPanel("Linear Chart",
sidebarLayout( CO_data2 <- CO_data[,-c(1,2,3)]
sidebarPanel( col_names = colnames(CO_data2)
selectInput('country',
'Select Country',
selected = 'Afghanistan', countries <- unique(CO_data['country'])
choices = countries years <- unique(sort(CO_data$year))
), world <- ne_countries(scale = 'medium', returnclass = 'sf')
selectInput('category', country_list <- unique(sort(world$name))
'Select Category',
selected = 'population', CO_data_filtered <- subset(CO_data, country %in% country_list)
choices = col_names
) only_co2_and_year <- CO_data[,c('year', 'country', 'co2')]
),
mainPanel( ui <- navbarPage(
plotlyOutput('linear_chart') titlePanel(title=div(img(src="https://siw.amu.edu.pl/__data/assets/file/0004/162751/logo_wersja-podstawowa_granat_1.jpg", width = 50, height = 50), 'explore CO2 data')),
), tabPanel("Linear Chart",
) sidebarLayout(
), sidebarPanel(
tabPanel( selectInput('country',
'GDP', 'Select Country',
plotlyOutput('gdp') selected = 'Afghanistan',
), choices = countries
tabPanel( ),
'TOP Production', selectInput('category',
fluidRow( 'Select Category',
column(6,plotlyOutput(outputId="the_most_1")), selected = 'population',
column(6,plotlyOutput(outputId="the_most_2")), choices = col_names
column(6,plotlyOutput(outputId="the_most_3")) )
) ),
),
tabPanel( mainPanel(
'Smallest production', plotlyOutput('linear_chart')
fluidRow( ),
column(6,plotlyOutput(outputId="the_least_1")), )
column(6,plotlyOutput(outputId="the_least_2")), ),
column(6,plotlyOutput(outputId="the_least_3")) tabPanel(
) 'GDP',
), plotlyOutput('gdp')
),
tabPanel( tabPanel("Map: CO2 by year",
'Data', sidebarLayout(
DT::dataTableOutput('tableData') sidebarPanel(
), selectInput('year',
'Select year',
tabPanel( selected = '2011',
'Theme', choices = years
shinythemes::themeSelector(), )
theme = shinythemes::shinytheme('flatly'), ),
)
) mainPanel(
plotlyOutput('map'),
),
server <- function(input, output, session) { )
output$tableData <- DT::renderDataTable({ ),
CO_data %>% tabPanel("Map: statistics in year 2011",
filter(country == input$country) %>% sidebarLayout(
DT::datatable() sidebarPanel(
}) selectInput('category2',
'Select category',
output$linear_chart <- renderPlotly({ selected = 'population',
CO_data %>% choices = col_names
filter(country == input$country) %>% )
ggplot(aes(x = year, y = get(input$category))) + ),
geom_line()
}) mainPanel(
output$gdp <- renderPlotly({ plotlyOutput('map2'),
CO_data %>% ),
filter(year == 2011) %>% )
ggplot(aes(x = gdp, y = co2, label = country)) + ),
geom_line() + tabPanel(
geom_point() + 'Biggest CO2 Production',
ggtitle('Placement of countries by CO2 and GDP production') fluidRow(
}) column(6,plotlyOutput(outputId="the_most_1")),
output$the_most_1 = renderPlotly({ column(6,plotlyOutput(outputId="the_most_2")),
CO_data %>% column(6,plotlyOutput(outputId="the_most_3"))
filter(year==2011) %>% )
slice_max(n=7, order_by = co2_per_gdp) %>% ),
ggplot(aes(x=country, y=co2_per_gdp, fill=country)) + tabPanel(
xlab('Country') + 'Smallest CO2 production',
ylab('CO2 per GDP') + fluidRow(
ggtitle('The TOP production per GDP') + column(6,plotlyOutput(outputId="the_least_1")),
theme(axis.text.x = element_blank()) + column(6,plotlyOutput(outputId="the_least_2")),
geom_bar(stat='identity') column(6,plotlyOutput(outputId="the_least_3"))
}) )
output$the_most_2 = renderPlotly({ ),
CO_data %>%
filter(year==2011) %>% tabPanel(
slice_max(n=7, order_by = co2_per_capita) %>% 'Data',
ggplot(aes(x=country, y=co2_per_capita, fill=country), custom) + DT::dataTableOutput('tableData')
xlab('Country') + ),
ylab('CO2 per capita') +
ggtitle('The TOP production per capita') + tabPanel(
theme(axis.text.x = element_blank()) + 'Theme',
geom_bar(stat='identity') shinythemes::themeSelector(),
}) theme = shinythemes::shinytheme('flatly'),
output$the_most_3 = renderPlotly({ )
CO_data %>% )
filter(year==2011) %>%
slice_max(n=7, order_by = co2_per_capita) %>%
ggplot(aes(x=country, y=co2_per_capita, fill=country)) + server <- function(input, output, session) {
xlab('Country') + output$tableData <- DT::renderDataTable({
ylab('CO2 overall') + CO_data %>%
ggtitle('The TOP production overall') + filter(country == input$country) %>%
theme(axis.text.x = element_blank()) + DT::datatable()
geom_bar(stat='identity') })
})
output$the_least_1 = renderPlotly({ output$linear_chart <- renderPlotly({
CO_data %>% CO_data %>%
filter(year==2011) %>% filter(country == input$country) %>%
slice_min(n=7, order_by = co2_per_gdp) %>% ggplot(aes(x = year, y = get(input$category))) +
ggplot(aes(x=country, y=co2_per_gdp, fill=country)) + ylab(input$category) +
xlab('Country') + geom_line()
ylab('CO2 per GDP') + })
ggtitle('The smalest production per GDP') + output$gdp <- renderPlotly({
theme(axis.text.x = element_blank()) + CO_data_filtered %>%
geom_bar(stat='identity') filter(year == 2011) %>%
}) ggplot(aes(x = gdp, y = co2, label = country)) +
output$the_least_2 = renderPlotly({ geom_line() +
CO_data %>% geom_point() +
filter(year==2011) %>% ylim(0,10000) +
slice_min(n=7, order_by = co2_per_capita) %>% ggtitle('Placement of countries by CO2 and GDP production')
ggplot(aes(x=country, y=co2_per_capita, fill=country)) + })
xlab('Country') + output$map = renderPlotly({
ylab('CO2 per capita') + countries_data <- filter(only_co2_and_year, year==input$year)
ggtitle('The smalest production per capita') + data <- merge(world, countries_data, by.y="country", by.x="name")
theme(axis.text.x = element_blank()) + ggplot(data = data) +
geom_bar(stat='identity') geom_sf(aes(fill = co2, label = name)) +
}) scale_fill_viridis_c(option = "plasma", trans = "sqrt") # colorblind-friendly palette
output$the_least_3 = renderPlotly({ })
CO_data %>% output$map2 = renderPlotly({
filter(year==2011) %>% countries_data <- filter(CO_data, year==2011)
slice_min(n=7, order_by = co2) %>% data2 <- merge(world, countries_data, by.y="country", by.x="name")
ggplot(aes(x=country, y=co2, fill=country)) + ggplot(data = data2) +
xlab('Country') + geom_sf(aes(fill = get(input$category2), label = input$category2)) +
ylab('CO2 overall') + labs(title=input$category2) +
ggtitle('The smalest production overall') + scale_fill_discrete(labels = input$category2) +
theme(axis.text.x = element_blank()) + scale_fill_viridis_c(option = "plasma", trans = "sqrt") # colorblind-friendly palette
geom_bar(stat='identity') })
}) output$the_most_1 = renderPlotly({
} CO_data_filtered %>%
filter(year==2011) %>%
slice_max(n=7, order_by = co2_per_gdp) %>%
shinyApp(ui = ui, server = server) ggplot(aes(x=country, y=co2_per_gdp, fill=country)) +
xlab('Country') +
ylab('CO2 per GDP') +
ggtitle('the biggest CO2 production per GDP') +
theme(axis.text.x = element_blank()) +
geom_bar(stat='identity')
})
output$the_most_2 = renderPlotly({
CO_data_filtered %>%
filter(year==2011) %>%
slice_max(n=7, order_by = co2_per_capita) %>%
ggplot(aes(x=country, y=co2_per_capita, fill=country), custom) +
xlab('Country') +
ylab('CO2 per capita') +
ggtitle('the biggest CO2 production per capita') +
theme(axis.text.x = element_blank()) +
geom_bar(stat='identity')
})
output$the_most_3 = renderPlotly({
CO_data_filtered %>%
filter(year==2011) %>%
slice_max(n=7, order_by = co2) %>%
ggplot(aes(x=country, y=co2, fill=country)) +
xlab('Country') +
ylab('CO2 overall') +
ggtitle('the biggest CO2 production overall') +
theme(axis.text.x = element_blank()) +
geom_bar(stat='identity')
})
output$the_least_1 = renderPlotly({
CO_data_filtered %>%
filter(year==2011) %>%
slice_min(n=7, order_by = co2_per_gdp) %>%
ggplot(aes(x=country, y=co2_per_gdp, fill=country)) +
xlab('Country') +
ylab('CO2 per GDP') +
ggtitle('the smallest CO2 production per GDP') +
theme(axis.text.x = element_blank()) +
geom_bar(stat='identity')
})
output$the_least_2 = renderPlotly({
CO_data_filtered %>%
filter(year==2011) %>%
slice_min(n=7, order_by = co2_per_capita) %>%
ggplot(aes(x=country, y=co2_per_capita, fill=country)) +
xlab('Country') +
ylab('CO2 per capita') +
ggtitle('the smallest CO2 production per capita') +
theme(axis.text.x = element_blank()) +
geom_bar(stat='identity')
})
output$the_least_3 = renderPlotly({
CO_data_filtered %>%
filter(year==2011) %>%
slice_min(n=7, order_by = co2) %>%
ggplot(aes(x=country, y=co2, fill=country)) +
xlab('Country') +
ylab('CO2 overall') +
ggtitle('the smallest CO2 production overall') +
theme(axis.text.x = element_blank()) +
geom_bar(stat='identity')
})
}
shinyApp(ui = ui, server = server)