您好,登錄后才能下訂單哦!
怎樣用R-Shiny打造在線App,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
今天做一個小小的案例,算是shiny動態可視化的小開端……
這個案例是之前發過的中國人口結構動態金字塔圖,這個圖還是蠻不錯,數據取自UN的官網,非常有現實意義的人口性別結構數據。
library(ggplot2)
library(animation)
library(dplyr)
library(tidyr)
library(xlsx)
library(ggthemes)
library(shiny)
library(shinythemes)
做簡單的數據清洗工作,為shiny提供可用的數據源:
setwd("D:/R/File")
windowsFonts(myfont=windowsFont("微軟雅黑"))
female<-read.xlsx("Population.xlsx",sheetName="Female",header=T,encoding='UTF-8',check.names = FALSE)
male<-read.xlsx("Population.xlsx",sheetName="Male",header=T,encoding='UTF-8',check.names = FALSE)
female<-female%>%gather(Year,Poputation,-1)
male<-male%>%gather(Year,Poputation,-1)
female$Poputation<-female$Poputation*-1
male$sex<-"male";female$sex<-"female"
China_Population<-rbind(male,female)%>%mutate(abs_pop=abs(Poputation))
China_Population$agegroup<-factor(China_Population$agegroup,
levels=c("0-4","5-9","10-14","15-19","20-24","25-29","30-34","35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80+") ,order=T)
China_Population_dd<-filter(China_Population,Year==1995)
定制shinyapp的ui:
ui <-shinyUI(fluidPage(
theme=shinytheme("cerulean"),
titlePanel("Population Structure Data"),
sidebarLayout(
sidebarPanel(
selectInput("var1", "x-axis",c("agegroup"="agegroup","Poputation"="Poputation","sex"="sex"),selected="agegroup"),
selectInput("var2", "y-axis",c("agegroup"="agegroup","Poputation"="Poputation","sex"="sex"),selected="Poputation"),
selectInput("var3", "Gender",c("agegroup"="agegroup","Poputation"="Poputation","sex"="sex"),selected="sex"),
selectInput("theme", "Choose a ShinyTheme:",choices ("cerulean","cosmo","cyborg","darkly","flatly","journal","lumen","paper",
"readable","sandstone","simplex","slate","spacelab","superhero","united","yeti")),
sliderInput("var4","Year",min=1950,max=2015,value=5,step=5)
),
mainPanel(h3('Dynamic pyramid of population structure in China'),plotOutput("distPlot"))
)
))
定制shiny的輸出服務端:
server<-shinyServer(function(input,output){
output$distPlot <- renderPlot({
mydata=filter(China_Population,Year==input$var4)
argu1<-switch(input$var1,agegroup=mydata$agegroup,Poputation=mydata$Poputation,sex=mydata$sex)
argu2<-switch(input$var2,agegroup=mydata$agegroup,Poputation=mydata$Poputation,sex=mydata$sex)
argu3<-switch(input$var3,agegroup=mydata$agegroup,Poputation=mydata$Poputation,sex=mydata$sex)
ggplot(data=mydata,aes(x=argu1,y=argu2,fill=argu3))+
coord_fixed()+
coord_flip() +
geom_bar(stat="identity",width=1) +
scale_y_continuous(breaks = seq(-70000,70000,length=9),
labels = paste0(as.character(c(abs(seq(-70,70,length=9)))), "m"),
limits = c(-75000,75000)) +
theme_economist(base_size=14)+
scale_fill_manual(values=c('#D40225','#374F8F')) +
labs(title=paste0("Population structure of China:",input$var4),
caption="Data Source:United Nations Department of Economic and Docial Affairs\nPopulation Division\nWorld Population Prospects,the 2015 Revision"
,y="Population",x="Age") +
guides(fill=guide_legend(reverse=TRUE))+
theme(
text=element_text(family="myfont"),
legend.position =c(0.8,0.9),
legend.title = element_blank(),
plot.title = element_text(size=20),
plot.caption = element_text(size=12,hjust=0)
)
})
})
運行app:
shinyApp(ui=ui,server=server)
看完上述內容,你們掌握怎樣用R-Shiny打造在線App的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。