Hi, I'm having difficulty running this code i learnt online. It seems to sho
Remark was "SP.POP.TL" not found and then "Year" Not found.
I'm quite new, and i've uploaded the dataset on R just to make sure the server can read it.
## reference code for UI.R and Server.R
# from code to name
codeToName <- c(
SP.POP.TOL = "Total Population",
happiness.index = "Happiness Index",
gdp.per.capita = "GDP per capita",
social.support = "Social Support",
life.expectancy = "Life Expectancy",
generosity = "Generosity",
perception.of.corruption = "Perception of Corruption",
confidence.national.government = "Confidence in National Government",
democratic.quality= "Democratic Quality",
delivery.quality = "Delivery Quality"
)
# from name to code
nameToCode <- names(codeToName)
names(nameToCode) <- codeToName
# server side
library(shiny)
library(ggplot2)
bubbleChart <- function(X,x,y,radius,colour, alpha = 0.6){
p <- ggplot(X,aes_string(x = x, y = y )) +
geom_point(aes_string(size = radius, colour = colour), alpha = alpha) +
geom_point(aes_string(size = radius), colour = 'black', alpha = 1, shape = 1 ) +
guides(colour = guide_legend(override.aes = list(alpha = 1, size = 5)))
p
}
whr <- read.csv(file="C:./shinyapp/WHRO.csv",sep="\t",header=TRUE)
shinyServer(function(input, output){
output$bubbleChart <- renderPlot({
p <- bubbleChart(
subset(whr, "Year"==input$year),
x = input$xAxis,
y = input$yAxis,
radius = "SP.POP.TL",
colour = "continent")
p <- p + scale_color_brewer(type="qual",palette='Set1') +
guides(size = guide_legend(title = codeToName['population'])) +
xlab(codeToName[input$xAxis]) +
ylab(codeToName[input$yAxis]) +
scale_size_area(max_size=14) +
ggtitle(paste("Year: ",input$year))
print(p)
})
}
)
# UI for the bubbleChart
library(shiny)
shinyUI(
pageWithSidebar(
headerPanel("Happiness Index Over Time"),
sidebarPanel(
h1("Interactive bubble chart"),
sliderInput(
"year", "Select a year:",
min = 1997, max = 2017,
step = 1, value = 2000, format = "####",
animate=TRUE),
selectInput("xAxis","Select the first variable:", choices = nameToCode, selected = "Happiness Index"),
selectInput("yAxis","Select the second variable:", choices = nameToCode, selected = "GDP per capita")
),
mainPanel(
plotOutput("bubbleChart")
)
)
)