溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎樣用R-Shiny打造在線App

發(fā)布時間:2021-12-09 11:22:44 來源:億速云 閱讀:262 作者:柒染 欄目:大數(shù)據(jù)

怎樣用R-Shiny打造在線App,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

今天做一個小小的案例,算是shiny動態(tài)可視化的小開端……

這個案例是之前發(fā)過的中國人口結(jié)構(gòu)動態(tài)金字塔圖,這個圖還是蠻不錯,數(shù)據(jù)取自UN的官網(wǎng),非常有現(xiàn)實意義的人口性別結(jié)構(gòu)數(shù)據(jù)。

library(ggplot2)

library(animation)

library(dplyr)

library(tidyr)

library(xlsx)

library(ggthemes)

library(shiny)

library(shinythemes)

做簡單的數(shù)據(jù)清洗工作,為shiny提供可用的數(shù)據(jù)源:

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的輸出服務(wù)端:

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

看完上述內(nèi)容,你們掌握怎樣用R-Shiny打造在線App的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

app
AI