Shiny Packages Resources

栏目: R语言 · 发布时间: 6年前

内容简介:使用shinythemes包,可以给shiny app设置一个Bootstrap theme,使其看起来更加美观所有的theme均来自于如果确定选择某个theme后,则在

shinythemes

使用shinythemes包,可以给shiny app设置一个Bootstrap theme,使其看起来更加美观

所有的theme均来自于 http://bootswatch.com/ ,现在这个包里所包含的theme有大约16种,我们可以在 ui.R 里写入 shinythemes::themeSelector() 来调用一个theme选择器,你可以在选择器的下拉框中依次查看每个theme的效果,示例代码展示网站: https://gallery.shinyapps.io/117-shinythemes/

如果确定选择某个theme后,则在 ui.R 中用下述代码调用

theme = shinytheme("cerulean")

有从上述Bootstrap网站中下载的主题(未包含在shinytheme包),可以将 bootstrap.css 放在 www 文件夹下,然后再用theme参数调用 theme = bootstrap.css

shinyalert

shinyalert包主要是用于生成一个messages Modal,跟shiny自带的Modal dialogs很类似,包的作者( Dean Attali ,一位shiny大牛,还写了shinyJS包)在其基础加上了一点JS库功能,使其不仅仅用于展示对话框messages,而且还能接受信息并且对其处理

使用shinyalert,需要在 ui.R 中加上 useShinyalert() (跟shinyJS调用方式一样)

然后就能在 server.R 中用 shinyalert() 函数来实现你的messages Modal,如下加个警告通知:

shinyalert("Oops!", "Something went wrong.", type = "error")

如果想增加OK和Cancel按钮来确定返回的值(对应就是TRUE和FALSE),则需要在shinyalert函数中增加 confirmButtonTextcancelButtonText ,如果点击框外部也当Cancel返回,如下:

shinyalert(
    title = "Hello",
    text = "This is a modal",
    closeOnClickOutside = TRUE,
    type = "success",
    showConfirmButton = TRUE,
    showCancelButton = TRUE,
    confirmButtonText = "OK",
    cancelButtonText = "Cancel"
)

如果将type参数改为 type = "input" ,则在Modal会有一个可输入框,然后用 input$shinyalert 来接受输入,如:

shinyalert(
    "Enter your name", type = "input",
)
output$txt <- renderText(paste0("Your name is: ", input$shinyalert))

此外可以还通过 callbackR 函数接受回调信息

shinyalert(
    "Enter your name", type = "input",
    callbackR = function(x) { if(x != FALSE) message("Hello ", x) }
)

利用这个回调信息,可以再次结合 shinyalert 给出提示Modal(但是不知道为啥有BUG了)

shinyalert(
    title = "What is your name?", type = "input",
    callbackR = function(value) { shinyalert(paste("Welcome", value)) }
)

至于shiny内置的Modal,是搭配使用 showModal()modalDialog() 函数,其比shinyalert优势在于可以在modal上放置各种形式的插件input或者文档rmarkdown等,如下所示:

observeEvent(input$show, {
  showModal(modalDialog(
    title = "Important message",
    "This is an important message!",
    fileInput(inputId = "list", label = "File Input:", accept = c(".txt")),
    includeMarkdown("www/about.md"),
    easyClose = TRUE
  ))
})

shinyBS

shinyBS包可以将一些Bootstrap Components添加到shiny程序上,使得shiny app更具互动性

shinyBS是将alerts和Modals分成两个块:

  • Alerts主要由 bsAlert()createAlert()closeAlert() 组成,首先是在 ui.R 代码中用bsAlert()确定alerts的位置,然后在 server.R 代码中用createAlert()创建alerts内容,最后用closeAlert()关闭,示例如: https://ebailey78.github.io/shinyBS/docs/Alerts.html#bsAlert
  • Modals中最常用的则是 bsModal() 函数,在不少shiny app中见过,使用比较简单,展示形式跟shiny自带的Modal也是差不多,在 ui.R 中写入如下代码:

    bsModal(id = "modalExample", title = "Data Table", trigger = "tabBut", size = "large", dataTableOutput("distTable"))

    如果想对这个modal进行控制(open or close),则可以在 server.R 中使用toggleModal函数,如下:

    toggleModal(session, modalId = "modalExample", toggle = "open")

Collapses主要用于收缩或者展开panels(展开或者关闭),首先在 ui.R 中用 bsCollapse() 创建一个组,然后在组下用 bsCollapsePanel() 函数控制各个子panels,最后在 server.R 中用 updateCollapse() 函数控制panels的变化,示例如: https://ebailey78.github.io/shinyBS/docs/Collapses.html

Buttons主要用于对shiny自带的buttons进行改进(样式、大小、是否block等),在 ui.Rserver.R 下分别是 bsButton()updateButton()

# ui.R
bsButton("actOne", label = "Block Action Button", block = TRUE)
# server.R
updateButton(session, "actOne", disabled = !input$togOne)

Tooltips和Popovers简单的说就是是UI界面上增加上一些提示信息,以免提示信息文字对UI界面整体造成过于的杂乱感,很好记,前者就是鼠标悬浮在某个插件上就跳出提示信息,后者则是鼠标点击某个区域后跳出提示信息,示例如: https://ebailey78.github.io/shinyBS/docs/Tooltips_and_Popovers.html

colourpicker

colourpicker包主要用于创建一个颜色选择插件

其主要函数是 colourInput() ,根据其不同参数,可以用于多种形式的颜色选择,我比较喜欢的一种是:

colourInput(inputId = "col", label = "corlor (e.g., blue)", value = "blue", palette = "limited", returnName = TRUE)

其还有一个 plotHelper() 函数有非常有趣,其可以让你实时查看,当你选择不同颜色时,在图片上呈现的结果是怎么样的,作者还做了个GIF文件可供学习: GIF

DT

DT包主要是给JS的DataTables库提供了一个R接口,可以将R的表格以更加美观的形式展示(包括html)以及提供一定的交互操作,可参照官网文档以及笔记:Learning DT包

shinydashboard

shinydashboard包主要是提供了一个框架,方便让shiny来实现dashboards,可操作性比较强,有不少独立于shiny之外的布局函数,但是最终界面确实蛮好用的,可看下官方的例子 http://rstudio.github.io/shinydashboard/examples.html

官方使用文档 http://rstudio.github.io/shinydashboard/get_started.html

shinyLP

shinyLP包主要提供了一些函数用于设计一个shiny版的home page(或者说 Landing page)

可以用shinyLP::runExample()查看下作者给的几个模块,并给出了每个模块所使用的代码,所以可以根据个人需求,从代码中挑选合适的函数,可以用用

我一般用用 jumbotron() 函数和 thumbnail_label() 来设置布局

shinyjs

把shinyJS包写在最后,是因为这是一个非常棒的shiny包,如果你不太懂JS的话,这个包能让你在不会JS的前提下使用一些JS的功能,只能说非常好用,值得好好尝试下,内容比较多,大概要好好整一整。。。

本文出自于 http://www.bioinfo-scrounger.com 转载请注明出处


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Django

Pro Django

Marty Alchin / Apress / 2008-11-24 / USD 49.99

Django is the leading Python web application development framework. Learn how to leverage the Django web framework to its full potential in this advanced tutorial and reference. Endorsed by Django, Pr......一起来看看 《Pro Django》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具