如何使用ShinyApps部署Shiny应用?

来源:互联网 发布:怎样下载解压软件 编辑:程序博客网 时间:2024/05/13 18:03

原文地址:http://www.xinglongjian.com/index.php/2014/06/06/shinyapps/

接触R有几个月了,开始接触时,是在命令行下通过命令运行,能够完成强大的统计和绘图功能,后来就想R写的程序和绘制的图能否通过网页的形式运行呢,这样就可以放到网上与同行交流了,在一次数盟聚会时,听到R大牛讲到了shiny,心里非常激动,这正是我想要的东东。结果一口气把R-Web Application Development with R Using shiny.pdf英文版看完了,对如何使用shiny开发R的web应用有了了解,并且我对web技术也有所了解,因此想深入学习一下shiny,编写完后,如何部署呢?这又是一个问题,后来在RStudio的官网上发现了ShinyApps,这是一个能够部署shiny应用程序的云平台,当时ShinyApps还没正式运行,现在可以测试运行,部署一下试试看,下面就说明如何使用ShinyApps部署自己的shiny应用程序。

主要参考:Getting Started Guide

1、必要条件:

  • 一个R开发环境
  • 最新版本的devtools包
  • 从GitHub安装shinyapps包
  • 一个能够运行的shiny应用
  • Windows:用于构建包的RTool
  • Mac OSX:用于构建包的XCode命令行工具

2、安装devtools包

      ShinyApps需要使用devtools包的最新改进特性,需要devtools包1.4或更高版本。

  1. > install.packages("devtools")

3、安装shinyapps包

     shinyapps包用于部署应用到ShinyApps服务中。该包目前只能从GitHub中获得。

     使用devtools安装: 

  1. > library(devtools)
  2. > devtools::install_github("rstudio/shinyapps")

     安装完后将其加载到R中     

  1. > library(shinyapps)

​4、创建一个工程

    在RStudio中创建一个名为“demo”的工程,并确保shiny应用依赖包ggplot2和shiny包已安装。

    在工程中需要创建2个shiny源文件:ui.R和server.R

    Server.R

   

  1. library(shiny)
  2. library(ggplot2)
  3. shinyServer(function(input, output) {
  4.   dataset <- reactive(function() {
  5.     diamonds[sample(nrow(diamonds), input$sampleSize),]
  6.   })
  7.   output$plot <- reactivePlot(function() {
  8.     p <- ggplot(dataset(), aes_string(x=input$x, y=input$y)) + geom_point()
  9.     if (input$color != 'None')
  10.       p <- p + aes_string(color=input$color)
  11.     facets <- paste(input$facet_row, '~', input$facet_col)
  12.     if (facets != '. ~ .')
  13.       p <- p + facet_grid(facets)
  14.     if (input$jitter)
  15.       p <- p + geom_jitter()
  16.     if (input$smooth)
  17.       p <- p + geom_smooth()
  18.     print(p)
  19.   }, height=700)
  20. })

  ui.R

 

  1. library(shiny)
  2. library(ggplot2)
  3. dataset <- diamonds
  4. shinyUI(pageWithSidebar(
  5.   headerPanel("Diamonds Explorer"),
  6.   sidebarPanel(
  7.     sliderInput('sampleSize', 'Sample Size', min=1, max=nrow(dataset),
  8.                 value=min(1000, nrow(dataset)), step=500, round=0),
  9.     selectInput('x', 'X', names(dataset)),
  10.     selectInput('y', 'Y', names(dataset), names(dataset)[[2]]),
  11.     selectInput('color', 'Color', c('None', names(dataset))),
  12.     checkboxInput('jitter', 'Jitter'),
  13.     checkboxInput('smooth', 'Smooth'),
  14.     selectInput('facet_row', 'Facet Row', c(None='.', names(dataset))),
  15.     selectInput('facet_col', 'Facet Column', c(None='.', names(dataset)))
  16.   ),
  17.   mainPanel(
  18.     plotOutput('plot')
  19.   )
  20. ))

   然后获取secret,点击show

   

实例类型可以通过shinyapps包的configureApp函数来更改:

  1. shinyapps::configureApp("APPNAME", size="medium")

这样将使用medium实例类型从新部署你的应用程序。

10、应用程序权限

   使用ShinyApps你可以配置权限以限制对你的应用程序的访问。当权限启用后,用户需要输入用户名和密码才能访问你的应用程序。只有正确登陆的用户才能查看和使用你的应用程序。当第一个授权用户添加时,权限自动启用。

   你需要准备你的系统构建本地包,确保你的系统已经安装了一下工具:

  • Windows:RTools
  • Mac OSX:XCode Command Line Tools
  • Linux:GCC

   为了支持权限,shinyapps的版本必须>=0.3,可以使用devtools安装:

  1. devtools::install_github('rstudio/shinyapps'

   需要使用scrypt包对密码进行加密,安装:

  1. devtools::install_github('rstudio/rscrypt')

   然后重启R会话。

   

  添加权限用户:

   Shinyapps包中有很多函数来管理应用程序的权限用户,首先需要加载shinyapps包到R中。

   使用shinyapps::addAuthorizedUser函数添加用户,然后提示输入用户的密码,密码使用scrypt加密。一旦存储,密码就不能查看,但可以重置。

  1. addAuthorizedUser("andy")

  添加完用户后,需要使用deployApp()函数从新部署应用程序。这样,该用户可以通过用户名和密码访问应用。如果忘记密码,可以使用shinyapps::addAuthorizedUser函数重置。

移除权限用户

  使用shinyapps::removeAuthorizedUser函数移除用户。  

  1. removeAuthorizedUser("andy")

  移除完后,也需要重新部署应用程序。


0 0
原创粉丝点击