R语言_Shiny包-_学习笔记.docx
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等):PgrarnFiles>R>R-4.3.3>library>shiny>examples>08htmlXZ名称修改日期类型WWW2024/3/717:57文件夹0app.R2023/11/2216: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)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")#tabbedpaneLsrunExample("07_widgets")#heLptextandsubmitbuttonsrunExample("08_html")#ShinyappbutLtfromHTMLrunExample("09_upload")#fiLeupLoadwizardrunExample("10_download")#fiLedownloadwizardrunExample("ll_timer")#anautomatedtimer布局fluidPage函数来展示一个自动调整组件尺寸大小来适应浏览器,所有组件放在fluidPage函数中,得到整个app的布局。除了fluidPage()之外,Shiny还提供了一些其他的页面函数,这些函数可以在更专业的情况下派上用场:fixedPage()和fillPage()0Iibrary(Shiny)ui<-fluidPage(titlePanel("titlepanel"),SidebarLayout(position="right",sidebarPanel("sidebarpanel")jmainPanel("mainpanel")侧边栏(SidebarPaneI部分)默认出现在app的左边,通过调整Sidebar'Layout函数的一个参数position="right”将SidebarPanel调到右边。fluidPage()titlePanel()SidebarLayoutOsidebarPanel()mainPanel()tabsetPanel()为任意数量的tabPanels()创建一个容器,该容器又可以包含任何其他HTML组件。ui<-fluidPage(tabsetPanel(tabPanel("Importdata"jfile工叩Ut("file","Data",buttonLabel="Upload.textlnput("delim","Delimiter(leaveblanktoguess)","")4numericlnput("skip","Rowstoskip",0,min=0),numericinput("rows","Rowstopreview",10,min=1),tabPanel("Setparameters")jtabPanel("Visualiseresults")ImportdataSmPerwneterSVualeMEUlt3DateUpload.NofMeeeiectedDelimttef(leaveblanktoguess)Rowstoskjp0mRowstopreviow1。m如果你想知道用户选择了哪个选项卡,你可以向tabsetPanel提供id参数,它成为一个输入。Iibrary(Shiny)ui<-fluidPage(sidebarLayout(sidebarPanel(textoutput("panel"),mainPanel(tabsetPanel(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","Heading,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",tabPanel("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="#0b3d91'fg="white"jbase_font="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,简单的比较或相等操作也非常容易做到。ui<-fluidPage(selectlnput("dataset","Dataset",c("diamonds","rock","pressure","cars"),ConditionalPanel(condition="output.nrows",checkbo×Input(',headonly"j"Onlyusefirst1000rows")server<-function(input,output,session)datasetinput<-reactive(switch(input$dataset,"rock"=rock,"pressure"=pressure,"cars"=cars)output$nrows<-reactive(nrow(datasetlnput()outputoptions(outputj"nrows"SuspendWhenHidden=FALSE)shinyApp(uijserver)HTML可以将您自己的HTML添加到Ui中。一种方法是用HTML()函数来包含HTML,用另外一种是使用Shiny提供的HTML助手。重要的标签元件有常规函数(如hl()和p(),所有其他标签都可以通过tags使用。names(tags)查看标签。ui<-fluidPage(HTML(r"(<hl>Thisisaheading<hl><pclass=,my-class">Thisissometext!<p><ul><li>Firstbullet<li><li>Secondbullet<li><ul>)")#同上ui<-fluidPage(hl(,Thisisaheading"),p("Thisissometext",class="my-class")jtags$ul(tags$li("Firstbullet"),tags$li("Secondbullet")#注意inLine=TRUE;的使用textutput()默认是生成一个完整的段落。tags$p("Youmade",tags$b("$",textoutput("amount"jinline=TRUE),"inthelast"jtextoutput("days",inline=TRUE),"days")img函数通过特殊处理才能找到图片,图片文件必须在WWW文件及在下,WWW文件和app.R脚本同路径(在同一文件下)img(src="my_image.png"jheight=72,width=72)Iibrary(Shiny)ui<-fluidPage(titlePanel(