Chapter 5 Shiny
5.1 Tencent log
#-------------------------------------------------------------------------------
# Step1 Install R
#-------------------------------------------------------------------------------
sudo apt-get install r-base-core
#-------------------------------------------------------------------------------
# Step2 Install packages
#-------------------------------------------------------------------------------
## Use R to install packages
install.packages("BiocManager")
BiocManager::install("shiny")
## Install shiny-sever
sudo apt-get install gdebi-core
wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.14.948-amd64.deb
sudo gdebi shiny-server-1.5.14.948-amd64.deb
#-------------------------------------------------------------------------------
# Step3 Set shiny sever
#-------------------------------------------------------------------------------
# check the condition of shiny sever
sudo systemctl status shiny-server.service
# After the end, the shiny user will appear, set the shiny user password
## Firstly, switch to root mode
sudo -i
## Then, set the password
sudo passwd shiny
## Third, add groups
sudo groupadd shiny-apps
sudo usermod -aG shiny-apps shiny
sudo usermod -aG shiny-apps ubuntu
## Fourth, change the owner of the specified file to the specified user or group
sudo chown -R shiny:shiny-apps /srv/shiny-server
sudo chmod g+w /srv/shiny-server
sudo chmod g+s /srv/shiny-server
## Fifth, go to shiny user
su - shiny
#-------------------------------------------------------------------------------
# Step4 Configure .Rprofile
#-------------------------------------------------------------------------------
vi .Rrofile
#-------------------------------------------------------------------------------
# Step5 Configure .Renviron
#-------------------------------------------------------------------------------
vi .Renviron
R_LIBS=/home/shiny/R_Library
#-------------------------------------------------------------------------------
# Step6 Install R packages
#-------------------------------------------------------------------------------
install.packages("BiocManager")
BiocManager::install("remotes")
BiocManager::install("shiny")
BiocManager::install("ggplot2")
BiocManager::install("dplyr")
library(shiny)
library(ggplot2)
library(dplyr)
remotes::install_github("Github-Yilei/ggcor")
remotes::install_github("hfang-bristol/XGR")
remotes::install_github("tuantuangui/MNet")
#-------------------------------------------------------------------------------
# Step7 Shiny sets port 3838
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step8 cp the shiny project folders to /srv/shiny-server/
#-------------------------------------------------------------------------------
cp -r MNet/ /srv/shiny-server/
sudo chown R shiny:shiny MNet/
#-------------------------------------------------------------------------------
# Step9 Restart shiny server
#-------------------------------------------------------------------------------
sudo systemctl restart shiny-server.service
## If an error occurs, check the log file
/var/log/shiny-server/
##If successful it displays the following contents
http:/124.221.255.21:3838/MNet
www.mnet.vip5.2 Shiny app log
您可以将开发好的Shiny APP部署在自己的服务器上,或是将其部署在公共的平台(即shinyapps.io)上。这里仅分享后者。对于如何将Shiny APP部署到自己的服务器中,将在未来进行补充。 部署至shinyapps.io平台操作步骤:
#-------------------------------------------------------------------------------
# Step1 在shinyapps.io官网上注册账号。您会被要求填写用户名
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step2 重新打开shinyapps.io官网,单击右上角Dashboard按钮(图1)。进入Dashboard界面(图2)后,单击左侧边栏Acccount > Tokens。您将在右侧主面板中看到Token和Secret信息。单击“Show”按钮,弹出对话框。
#-----------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step3 不要关闭该网页。现假设您的APP名称是"myapp"。在本地的文件浏览器中新建一个文件夹,名为"myapp"。确保该文件夹中有“app.R”文件,并且"app.R"文件的末尾要有shinyApp函数(或能返回与shinyApp函数相同类型的对象的函数)。
#-----------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step4 用RStudio打开文件"app.R"。在命令行窗口中输入runApp()。这一步称为本地测试。若您的Shiny APP成功运行,方可进行下一步。
#-----------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step5 运行以下代码。若报错提示您未安装某些包,则采用install.package安装相应的包即可。代码运行完成后,会自动弹出一个网页,网页内容即是您的Shiny APP,表明部署完成。
#-----------------------------------------------------------------------------
if(!require(rsconnect)) install.packages("rsconnect")
rsconnect::setAccountInfo(name='Your Account',
token='Your Token',
secret='Your Secret')
#name, token, secret信息来源于Step2网页中的对话框
deployApp()
如果出bug:
library(BiocManager)
r=BiocManager::repositories()
#getOption("repos")[["CRAN"]] = "http://cran.r-project.org"
r[6] <- "http://cran.r-project.org"
options(repos =r)