pacman::p_load(tidyverse,shiny,ggvis,maps,reshape2)
Q1
##Q1
ui_0 <- shinyUI(fluidPage(
h3("Interactive Normal Distribution"),
sidebarLayout(
sidebarPanel(
sliderInput("Mean",label = "Mean of normal distribution",min = -10,max = 10,value = 0),
sliderInput("Std",label = "Standard deviation of normal distribution",min = 0,max = 2,value = 1,
step = 0.1)
),
mainPanel(
fluidRow(
plotOutput("out.plot")
)
)
)
))
server_0 <- shinyServer(function(input,output){
output$out.plot <- renderPlot({
curve(dnorm(x,mean = input$Mean,sd = input$Std),xlim = c(-10,10),ylim = c(0,5))
abline(v = input$Mean,lty = 2,col = "red")
text(7, 4.5, paste0("Mean = ",input$Mean,", SD = ",input$Std))
})
})
#shinyApp(ui_0,server_0)
Q2
tw <- read.csv("C:/Users/Cheng_wen_sung/Desktop/tw_export_import.csv",header = F)
# unit: 1000 USD
tw$V1 <- as.character(tw$V1)
tw <- data.frame(
matrix(unlist(strsplit(tw$V1, 'M')), 192, 2, byrow = TRUE),
tw[, -1])
EI <- rep(c("_E", "_I"), 33)
Country <- rep(c("Total",
"Asia", "HK", "ID", "JP", "KR", "CN", "MY", "SG", "THA", "VEN",
"Nahost", "KW", "SB",
"EU", "FR", "DE", "IT", "EN", "Africa", "NG",
"NAmerica", "CA", "US",
"MAmerica", "MX", "PA",
"SAmerica", "AR", "BR",
"Oceania", "AU", "NZ"), each = 2)
names(tw) <- c("Year", "Month", paste0(Country, EI))
# tw reshape + map + static
tw_long <- reshape(tw, varying = names(tw)[3:68],
v.names = 'USD', times = names(tw)[3:68],
direction = "long")
tmp <- matrix(unlist(strsplit(tw_long$time, "_")),
12672, 2, byrow = TRUE)
tw_new <- data.frame(tw_long[, c(1, 2, 5)], tmp)
names(tw_new)[4:5] <- c("Country", "Type")
# group by Year and Country
tw_year <- tw_new %>%
group_by(Year, Country, Type) %>%
summarise(Sum_of_Year = sum(USD))
tw_year <- as.data.frame(tw_year)
levels(tw_year$Country) <- c("Africa", "Argentina", "Asia",
"Australia", "Brazil", "Canada", "China", "Germany", "UK",
"Europe", "France", "Hong Kong", "Indonesia", "Italy", "Japan",
"South Korea", "Kuwait", "MAmerica", "Mexico", "Malaysia",
"Nahost", "NAmerica", "Nigeria", "New Zealand", "Oceania",
"Panama", "SAmerica", "Saudi Arabia", "Singapore", "Thailand",
"Total", "USA", "Vietnam")
levels(tw_year$Type) <- c("Export", "Import")
#
tmp <- tw_year %>%
subset(Year == 2015) %>%
subset(Country != "Africa") %>%
subset(Country != "Asia") %>%
subset(Country != "Europe") %>%
subset(Country != "Hong Kong") %>%
subset(Country != "Nahost") %>%
subset(Country != "MAmerica") %>%
subset(Country != "NAmerica") %>%
subset(Country != "SAmerica") %>%
subset(Country != "Oceania") %>%
subset(Country != "Total")
#
export <- subset(tmp, Type == "Export")
import <- subset(tmp, Type == "Import")
Diff_of_Year <- export$Sum_of_Year - import$Sum_of_Year
Type <- ifelse(Diff_of_Year > 0, "export > import", "import > export")
tw_2015 <- data.frame(export[, 1:2], Diff_of_Year, Type)
#
wrld <- map_data(map = "world")
wrld$cc <- rep("0", 99338)
USD <- vapply(wrld$regi, function(x) {
if(x %in% as.character(tw_2015$Country)) {
tw_2015[which(tw_2015$Country == x), 'Diff_of_Year']
} else {0}
}, numeric(1))
wrld$USD <- USD
wrld$balance <- ifelse(wrld$USD == 0, "",
ifelse(wrld$USD < 0, "Surplus", "Deficit"))
#wrld$USD[which(wrld$USD == 0)] <- NA
wrld$USD2 <- ifelse(wrld$USD < 0, -1,
ifelse(wrld == 0, 0, 1))
##ggvis
#p <- ggvis(data = wrld,x = ~long,y = ~lat,fill = ~USD)%>%
# group_by(group)%>%
# layer_paths()
#p %>% add_tooltip(function(wrld) wrld$USD)
Q3
server.3 <- shinyServer(function(input, output){
df <- replicate(100, sample(1:6, 100, repl = TRUE))
tot_m <- mean(df)
df <- as.data.frame(df)
df_m <- colMeans(df)
df_s <- colSums(df)
df_exp <- data.frame(index = 1:100,
expec = cumsum(df_s) / (seq(1:100) * 100))
theme_set(theme_bw())
output$bar <- renderPlot({
ggplot(df, aes_string(x = paste0("V", input$roll))) +
geom_bar() + labs(x = "Roll 100 dices") +
lims(x = c("1", "2", "3", "4", "5", "6"),y = c(0,35))
})
output$expectation <- renderPlot({
ggplot(df_exp[1:input$roll,], aes(x = index, y = expec)) +
geom_line() +
geom_hline(yintercept = tot_m, linetype = "dashed") +
xlim(1, 105) + ylim(2, 5)
})
})
# ui
ui.3_1 <- shinyUI(fluidPage(
h3("Rolling dice"),
actionButton("roll", label = "Roll the dice!"),
# actionButton("reset", label = "Reset"),
fluidRow(
column(4, plotOutput("bar")),
column(4, plotOutput("expectation")))
))
ui.3_2 <- shinyUI(fluidPage(
h3("Rolling 100 dice"),
sliderInput("roll",label = "Rolling times",min = 1,max = 100,value = 1,step = 1),
# actionButton("reset", label = "Reset"),
fluidRow(
column(4, plotOutput("bar")),
column(4, plotOutput("expectation")))
))
#shinyApp(ui.3_1,server.3)
#shinyApp(ui.3_2,server.3)