shiny作为Rstudio中一个非常重要的包,对于构建交互式的网页非常方便。一般由shiny构筑的网页,由两个文件构成:ui.R和server.R,下面分别对两者的组件进行介绍。
在运行程序的时候,需要将ui.R和server.R放入同一个文件夹之中
ui.R一般的框架library(shiny)
shinyUI(fluidPage(
# 标题
titlePanel("title name"),
# 边栏
sidebarPanel(
#选择块
selectInput("dataset","choose a dataset",choices=c("A","B"))
)
#主栏
mainPanel()
))
selectInputselectInput提供下拉菜单的选择,输入格式:
selectInput("data","choose",choices=c("A","B"))
data是用于储存变量的名称(可以在server.R中以input$data调用),choose是输入说明,choices给出下拉变量的选择
numericInput顾名思义,numericInput是用来输入数值类型数据,其输入格式:
numericInput("num","type a number",10)
其中num是用于储存变量的名称(可以再server.R中以input$num调用),type a number是说明信息,10是输入的数据
checkboxInput勾选组件有时候,我们可以选择某些图形是否出现,可以通过勾选是否要这个内容来实现,所以这个输入的变量只会出现TRUE或者FALSE,也就是说这个变量是bool型变量
checkboxInput("out","Output",FALSE)
其中out是用来储存bool型变量,Output是对变量进行的说明,FALSE是取值
checkboxGroupInput多勾选组件有时候我们需要对内容进行多选,这样就需要多项选择
checkboxGroupInput("cities","choose Cities",cities)
这样子就可以实现多项选择了
sliderInput定制化滑动条通过滑动滑动条来选择数据,是一件非常酷炫的事情,下面给出一个例子
sliderInput("integer","Integer:",
min=0,max=1000,value=500,step=1)
其中integer用来储存变量,Interger:是说明,min代表着滑动条的最小值,max代表着滑动条的最大值,value代表着滑动条初始值,step代表着滑动条的间隔
若需要输入区间范围,可以增加两个变量,例如
sliderInput("range","Range:",
min=0,max=1000,value=c(200,500))
其中range用来储存变量,只不过此时的变量变成了一个vector
radioButtons选项卡我们常常会给出一些选项来,还是直接举例子:
radioButtons("dist","Distribution type:",
list("Norm"="norm",
"Uniform"="unif",
"Log-normal"="lnorm",
"Exponential"="exp"
)
),
br(),
其中dist依旧是用于储存变量,Distribution依旧是对变量的说明,而list中,左端代表着展现出来选项,等号右边是选项对应的值,当然在server.R中调用也需要用reactive来动态调用
helpText插件说明对于某些插件的功能,我们可能需要对其进行说明,最简单的方式就是在插件下方使用helpText
submitButton提交按钮对于结果展现,我们并不希望其实时进行更新,而是通过我们的提交来实现结果的刷新,直接在底下增加submitButton,使用如下面的:
submitButton("Update View")
tabsetPanel有时候,将所有的内容放入一个界面之中,会显得非常杂乱无章,所以为了将内容分开,需要在使用tabsetPanel
tabsetPanel(
tabPanel("plot",plotOutput("plot")),
tabPanel("summary",verbatimTextOutput("summary")),
tabPanel("Table",tableOutput("table"))
)
由于ui中的输出需要与server中的程序对应,所以这里需要将两个程序中的组件一起写出来
server.R中需要调用renderPrint:
output$summary<-renderPrint({
#内容
summary()
})
ui.R中需要调用verbatimTextOutput:
verbatimTextOutput("summary")
server.R中需要调用renderTable:
output$view<-renderTable({
#内容 data.frame
})
ui.R中需要调用tableOutput:
tableOutput("view")
server.R则调用renderText来输出
ui.R对于字符串的输出,可以用h1、h2和h3等来调节字体的大小,例如:
h3(textOutput("caption"))
server.R则调用renderPlot来输出
ui.R对于图片输出,使用plotOutput即可
plotOutput("graph")
server.R一般框架library(shiny)
shinyServer(function(input,output){
#内容
})
reactive在ui.R中传递过来的参数,只是一个字符串,如何获取将这个字符串所代表的数据集,我们需要用到reactive函数:
datasetInput<-reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars
)
})
其中input$dataset代表着从ui.R中传递过来的参数,但是它仅仅只是一个字符串,所以需要用switch函数将数据转化对应的数据集,之后调用的时候,使用datasetInput()即可
代码在某个地方无条件地停止执行:
browser()
在特定的条件下停止执行代码:
# Stop execution when the user selects "am"
browser(expr = identical(input$variable, "am"))
设置R的error选项,使得当错误发生的时候,自动进入调试浏览器:
# Immediately enter the browser when an error occurs
options(error = browser)
使用recover函数为错误处理器
# Call the recover function when an error occurs
options(error = recover)
一般情况下,shiny上传的每个文件最大不超过5MB,可以通过修改shiny.maxRequestSize,在server.R中增加以下命令: options(shiny.maxRequestSize=30*1024^2)即可将文件大小限制提高到30MB
在ui.R中需要用fileInput来调用文件
fileInput('file1','choose CSV File',
accept=c('text/csv','.csv'))
tags$hr()
其中file1用来储存文件,choose CSV File是对输入的说明,accept是对于文件类型的选择
在server.R中需要通过以下代码进行调用:
inFile<-input$file1
if(is.null(inFile))
return(NULL)
read.csv(inFile$datapath)
可以看出其调用使用的文件的路径,并不是真的把文件储存在变量之中
有时候会有上传文件的需求,我们查看的数据需要下载
在ui.R中需要用到downloadButton函数来下载需要的数据
downloadButton('downloadData','Download')
对上述的表达式进行说明,其中downloadData代表着储存数据的变量,Download是下载按钮上的说明
在server.R中需要将数据写入,需要用到downloadHandler
output$downloadData <- downloadHandler(
filename=function(){
paste(input$dataset,'.csv',sep='')
}
content = function(file){
write.csv(datasetInput(),file)
}
)
主要需要定义,文件的的名字filename,文件的内容content
conditionalPanel条件刷新当动态输入的变量来自于input变量
checkboxInput("smooth","Smooth"),
conditionalPanel(
condition = "input.smooth == true"
selectInput("smoothMethod","Method",
list("lm","glm","gam","loess","rlm"))
)
根据你是否选择smooth来判断是否出现下面的内容,一旦勾选了smooth,下面就会出现让你选择smooth的方法
如果动态输入的变量来自于output变量
在ui.R中,如下面的内容:
# Partial example
selectInput("dataset", "Dataset", c("diamonds", "rock", "pressure", "cars")),
conditionalPanel(
condition = "output.nrows",
checkboxInput("headonly", "Only use first 1000 rows"))
在server.R,如下内容:
# Partial example
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
output$nrows <- reactive({
nrow(datasetInput())
})
在server.R中需要将输出的变量设置为动态的,也就是说用reactive函数对其进行封装
renderUIrenderUI使用与renderTable,renderPrint的使用方法差不多,在server.R中使用renderUI,而在ui.R中使用uiOutput,对server.R计算结果进行展示,下面编写代码,进行说明:
在ui.R中编辑如下代码:
library(shiny)
shinyUI(fluidPage(
headerPanel("Test"),
sidebarPanel(
# Partial example
numericInput("lat", "Latitude",10),
numericInput("long", "Longitude",10),
uiOutput("cityControls"))
))
在server.R中编辑如下代码:
library(shiny)
shinyServer(function(input,output){
# Partial example
output$cityControls <- renderUI({
cities <- c(input$lat:input$long)
checkboxGroupInput("cities", "Choose Cities", cities)
})
})
上面代码对于结果呈现已经说得很清楚了,调试一下代码就可以了
需要使用组件Shiny.unbindAll()和Shiny.bindAll()
global.R和局部的理解如果没有设置全局变量的话,当用户每一次对ui.R和server.R访问时,都会重新加载变量,重新生成网页,对于全局变量,最好的方式是在global.R中设置,这样子并不需要用户每一访问,都重新加载,既占用内存,又消耗了时间
source('yourcode.R',local=TRUE)
使用shiny server的好处
shiny APP,每一个都有其自己的URLWebSocket)详细的shiny-server教程参见shiny-server
系统: centos 7.0
安装R命令: sudo yum install R
安装shiny package:``
安装shiny server:通过wget来下载安装文件:
wget https://download3.rstudio.org/centos5.9/x86_64/shiny-server-1.4.2.786-rh5-x86_64.rpm
然后用一下命令直接安装:
sudo yum install --nogpgcheck shiny-server-1.4.2.786-rh5-x86_64.rpm
shiny server启动shiny server:sudo systemctl start shiny-server
结束shiny server:sudo systemctl stop shiny-server
观察shiny server的状态:sudo systemtcl status shiny-server
设置shiny server自动运行\不自动运行:sudo systemctl enable\disable shiny-server
命令打开vi /etc/shiny-server/shiny-server.conf
server默认为HTTP server,监听的接口(port/IP)通过下面的定义:
server{
listen 80;
}
通过定义虚拟接口,可以隐藏监听接口
server{
listen 80;
server_name server1.com;
}
shiny用户创建密码:passwd shiny/etc/sudoers文件,向其中添加下面的指令shiny ALL=(ALL) ALL,即可修改用户权限,从root用户进入shiny用户,可以用下面的指令su shiny在运行了其中默认的配置
sudo /opt/shiny-server/bin/deploy-example user-dirs
运行下面的命令
mkdir ~/ShinyApps
拷贝文件夹指令:scp/cp -r
只需要将文件复制到目录下~/ShinyApps,然后通过下面的地址来访问shiny形成的网页
http://<server-address>:3838/<your_username>/yourfile
shiny包可能会无法安装上去,所以在命令窗口输入R,然后输入下面指令:install.packages('shiny', repos='http://cran.rstudio.com/')
library(shiny)
shiny访问的的时候,需要将服务器的端口打开,不然无法进行访问,如果你在配置文件/etc/shiny-server/shiny-server.conf文件中写的监听端口为3838,服务器端口开放命令:
iptables -I INPUT -p tcp --dport 3838 -j ACCEPT
iptables -I INPUT -p gre -j ACCEPT
plotly包安装问题,可能会碰到系统的curl包无法安装,而curl无法安装的原因在于系统的libcurl没有安装,需要输入一下命令:
yum -y install libcurl
yum install curl curl-level