R语言_Shiny包-_学习笔记.docx

上传人:p** 文档编号:933782 上传时间:2024-04-21 格式:DOCX 页数:13 大小:59.19KB
下载 相关 举报
R语言_Shiny包-_学习笔记.docx_第1页
第1页 / 共13页
R语言_Shiny包-_学习笔记.docx_第2页
第2页 / 共13页
R语言_Shiny包-_学习笔记.docx_第3页
第3页 / 共13页
R语言_Shiny包-_学习笔记.docx_第4页
第4页 / 共13页
R语言_Shiny包-_学习笔记.docx_第5页
第5页 / 共13页
R语言_Shiny包-_学习笔记.docx_第6页
第6页 / 共13页
R语言_Shiny包-_学习笔记.docx_第7页
第7页 / 共13页
R语言_Shiny包-_学习笔记.docx_第8页
第8页 / 共13页
R语言_Shiny包-_学习笔记.docx_第9页
第9页 / 共13页
R语言_Shiny包-_学习笔记.docx_第10页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《R语言_Shiny包-_学习笔记.docx》由会员分享,可在线阅读,更多相关《R语言_Shiny包-_学习笔记.docx(13页珍藏版)》请在第壹文秘上搜索。

1、R语言_Shiny包学习笔记ZW2024-03-17ShinyApp的基本构成ShinyaPP有两部分(ui.R和SerVer.R可以放在一个R脚本文件中app.R): 一个用户交互脚本(ui):Ui负责控制布局和展示,在源脚本中固定的名称为ui.R 一个服务器脚本(server):SerVer.R脚本包含建立app计算机需要的基本说明文件夹内容如下(创建的WWW文件夹用来存放JS、CSS、图片、html等):PgrarnFilesRR-4.3.3libraryshinyexamples08htmlXZ名称修改日期类型WWW2024/3/717:57文件夹0app.R2023/11/2216:

2、43R文件DDESCRIPTION2023/11/2216:43文件oReadme.md9CreateShinyappI2023/11/2216:43MD文件ShinyAppId=htlTemplatef.,*widex.htlw,SerVerIibrary(Shiny)ui-fluidPage(textoutput(greeting)server-function(input,output,session)output$greeting-renderText(Hellohuman!)shinyApp(ui=Ui,server=server)运行方式#app.R的路径Iibrary(Shiny

3、)runApp(my_app)Shinyapps示例Youcanalsoembedplots,forexample:system.file(examples,package=shiny)runExample(0:LJIello)#ahistogramrunExample(02_text)#tabLesanddataframesrunExample(03_reactivity)#areactiveexpressionrunExample(04_mpg)#gLobaLVariabLesrunExample(05_sliders)#sLiderbarsrunExample(06_tabsets)#t

4、abbedpaneLsrunExample(07_widgets)#heLptextandsubmitbuttonsrunExample(08_html)#ShinyappbutLtfromHTMLrunExample(09_upload)#fiLeupLoadwizardrunExample(10_download)#fiLedownloadwizardrunExample(ll_timer)#anautomatedtimer布局fluidPage函数来展示一个自动调整组件尺寸大小来适应浏览器,所有组件放在fluidPage函数中,得到整个app的布局。除了fluidPage()之外,Shi

5、ny还提供了一些其他的页面函数,这些函数可以在更专业的情况下派上用场:fixedPage()和fillPage()0Iibrary(Shiny)ui-fluidPage(titlePanel(titlepanel),SidebarLayout(position=right,sidebarPanel(sidebarpanel)jmainPanel(mainpanel)侧边栏(SidebarPaneI部分)默认出现在app的左边,通过调整SidebarLayout函数的一个参数position=right”将SidebarPanel调到右边。fluidPage()titlePanel()Sideb

6、arLayoutOsidebarPanel()mainPanel()tabsetPanel()为任意数量的tabPanels()创建一个容器,该容器又可以包含任何其他HTML组件。ui-fluidPage(tabsetPanel(tabPanel(Importdatajfile工叩Ut(file,Data,buttonLabel=Upload.textlnput(delim,Delimiter(leaveblanktoguess),)4numericlnput(skip,Rowstoskip,0,min=0),numericinput(rows,Rowstopreview,10,min=1),

7、tabPanel(Setparameters)jtabPanel(Visualiseresults)ImportdataSmPerwneterSVualeMEUlt3DateUpload.NofMeeeiectedDelimttef(leaveblanktoguess)Rowstoskjp0mRowstopreviow1。m如果你想知道用户选择了哪个选项卡,你可以向tabsetPanel提供id参数,它成为一个输入。Iibrary(Shiny)ui-fluidPage(sidebarLayout(sidebarPanel(textoutput(panel),mainPanel(tabsetPa

8、nel(id=tabsettabPanel(,panel1,one),tabPanel(panel2,two),tabPanel(panel3,three)server-function(input,output,session)OUtPUt$Panel-renderText(paste(Currentpanel:,input$tabset)shinyApp(ui=ui,server=server)navlistPanel()与tabsetPanel()类似,但它不是水平运行选项卡标题,而是在侧边栏中垂直显示它们。ui-fluidPage(navlistPanel(id=tabset,Head

9、ing,tabPanel(panel1,Panelonecontents),Heading2,tabPanel(panel2,Paneltwocontents),tabPanel(panel3,Panelthreecontents)另一种方法是使用navbarPage():它仍然水平运行选项卡标题,但您可以使用navbarMenu()添加下拉菜单以获得额外的层次结构级别。ui-navbarPage(Pagetitle,tabPanel(,panel1,one),tabPanel(panel2,two),tabPanel(,panel3,three),navbarMenu(subpanels,t

10、abPanel(panel4a,j,four-a,)jtabPanel(panel4b,four-b),tabPanel(panel4c,four-c)主题安装bslib包或者shinythemes包使用页面主题。thematic包可以为ggplot2lattice和baseplots提供主题,只需在服务器函数中调用thematic_shiny()o自己制作的主题,可以写好的主题放在WWw/的子文件夹下myappI-server.RI-ui.RI-wwI-mytheme.cssui-fluidPage(theme-bslib:bs_theme(bg=#0b3d91fg=whitejbase_f

11、ont=SourceSansPro)# theme-shinytheme(ceruLean,)# 自制主题theme-mytheme.css)server-function(input,output,session)thematic:thematic_shiny()OUtPUt$PlOt-renderPlot(ggplot(mtcarsaes(wt,mpg)+geom_point()+geom_smooth(),res=96)ConditionalPanel创建一个面板,该面板根据JavaScript表达式的值显示和隐藏其内容。即使你不懂任何JaVaSCript,简单的比较或相等操作也非常容易

12、做到。ui-fluidPage(selectlnput(dataset,Dataset,c(diamonds,rock,pressure,cars),ConditionalPanel(condition=output.nrows,checkboInput(,headonlyjOnlyusefirst1000rows)server-function(input,output,session)datasetinput-reactive(switch(input$dataset,rock=rock,pressure=pressure,cars=cars)output$nrows-reactive(n

13、row(datasetlnput()outputoptions(outputjnrowsSuspendWhenHidden=FALSE)shinyApp(uijserver)HTML可以将您自己的HTML添加到Ui中。一种方法是用HTML()函数来包含HTML,用另外一种是使用Shiny提供的HTML助手。重要的标签元件有常规函数(如hl()和p(),所有其他标签都可以通过tags使用。names(tags)查看标签。ui-fluidPage(HTML(r(ThisisaheadingThisissometext!FirstbulletSecondbullet)#同上ui-fluidPage(

14、hl(,Thisisaheading),p(Thisissometext,class=my-class)jtags$ul(tags$li(Firstbullet),tags$li(Secondbullet)#注意inLine=TRUE;的使用textutput()默认是生成一个完整的段落。tags$p(Youmade,tags$b($,textoutput(amountjinline=TRUE),inthelastjtextoutput(days,inline=TRUE),days)img函数通过特殊处理才能找到图片,图片文件必须在WWW文件及在下,WWW文件和app.R脚本同路径(在同一文件下)img(src=my_image.pngjheight=72,width=72)Iibrary(Shiny)ui-fluidPage(titlePanel(

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 数据结构与算法

copyright@ 2008-2023 1wenmi网站版权所有

经营许可证编号:宁ICP备2022001189号-1

本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。第壹文秘仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知第壹文秘网,我们立即给予删除!