千家信息网

怎样用R-Shiny打造在线App

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,怎样用R-Shiny打造在线App,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。今天做一个小小的案例,算是shiny动态可视化的小开端…
千家信息网最后更新 2026年11月02日怎样用R-Shiny打造在线App

怎样用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的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

数据 在线 人口 内容 动态 方法 更多 案例 结构 问题 不错 束手无策 为此 原因 对此 小开 性别 意义 技能 数据源 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 中国互联网科技工作委员会毛军 互联网科技公司税收优惠 互联网科技论文 银优互联网科技有限公司 火传媒上海互联网科技 互联网金融和金融科技 重庆科技学院互联网 成都仁贤领冠互联网科技有限公司 华脉科技工业互联网产品 阿里巴巴算科技企业还是互联网 互联网金融科技公司干啥的 湖南诚度互联网科技有限公司 福建科技产业互联网发展 链环科技传统企业怎么转型互联网 燊美集互联网科技有限公司 恒生互联网科技业指数调整周期 区块链结合互联网科技 上海京穆互联网科技有限公司 全球科技与互联网公司市值变化 互联网 科技节活动 微视频大赛 互联网和科技技术 互联网科技的 南京欣网互联网科技 苏州大垚互联网科技 互联网科技公司需要的职位 宁波青枫互联网科技有限公司 互联网金融中的现代科技 启辰互联网科技 互联网科技职业技能培训学校 求路客互联网科技电话
0