Skip to content

Instantly share code, notes, and snippets.

@yzhong52
Last active September 11, 2020 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yzhong52/1b75ad704bec5864c8eeb939f43047d7 to your computer and use it in GitHub Desktop.
Save yzhong52/1b75ad704bec5864c8eeb939f43047d7 to your computer and use it in GitHub Desktop.
在Ubuntu操作系统下运行shiny服务器

在Ubuntu操作系统下运行shiny服务器

Basic Command

ls - show the current diretory pwd - show the current folder cd - go to folder nano - edit file cat - show the content of a file mv - move a folder to another location

基本系统工具升级与安装

系统升级

apt-get是Ubuntu操作系统下面的软件管理工具,需要升级才可以下载安装最新版本的工具包。

sudo apt-get update

安装R和Shiny相关

sudo apt-get install r-base
sudo apt-get install r-base-dev
sudo apt-get install gdebi-core
sudo apt-get install git

安装Shiny服务器

# 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

这里的v1.5.14是目前shiny-server最新版本。

修改R配置文件

Open this file ~/.Rprofile and type in the following:

options(download.file.method = "libcurl")
local({
  r <- getOption("repos")
  r["CRAN"] <- "https://cran.rstudio.com/"
  options(repos=r)
})

control + X: to exit editing file shift + Y: to confirm saving the file cat ~/.Rprofile: to double check

安装Shiny工具包

方法一

sudo su - -c "R -e \"install.packages('shiny')\""

方法二

$ sudo R
> install.packages('shiny')

这一步会将shiny安装到这个目录下/opt/shiny-server/

运行服务器

当shiny服务器安装好之后,输入下面命令来运行服务器。

sudo systemctl start shiny-server

如果发生故障,可以尝试重启:

sudo systemctl restart shiny-server

打开3838端口

管理 > 本实例安全组 > 安全组列表 > 手动添加 > 目的3838 > 对象 0.0.0.0/0.

这里要打开3838端口是因为这个是Shiny服务器实用的默认端口。选择IP地址(对象)0.0.0.0的意思是允许任何IP地址访问。有些时候有些网站希望只能对个别IP地址访问,也可以填写具体的IP地址。

在这个时候,只需要输入云计算机的公网IP地址,加上3838端口,就可以访问默认的shiny网页了。比如说,

http://47.252.19.149:3838

(备注:这个是我之前租的北美的服务器,目前已经到期,链接不可用。)

运行网站

最后,只需要将代码放入到该目录即可cd /srv/shiny-server

比如说,将代码放在/srv/shiny-server/ESIShiny目录下,我们将可以通过公网IP,加上端口,加上子目录,访问网页。

http://47.252.19.149:3838/ESIShiny

(备注:这个是我之前租的北美的服务器,目前已经到期,链接不可用。)

安装额外工具包

当然,如果网页用到其它的工具包,也需要都安装上。比如说,ESIShiny需要这些:

install.packages('ggplot2')
install.packages('wordcloud')
install.packages('stringr')
install.packages('plyr')
install.packages('tidytext')
install.packages('memoise')
install.packages('reshape2')
install.packages('DT')

代码拷贝到云计算机

那么,怎么把代码拷贝到云计算机上呢?有多种方法可以将代码拷贝到服务器上。帅帅常常用的是一个叫scp的命令,可以参考:

https://unix.stackexchange.com/questions/106480/how-to-copy-files-from-one-machine-to-another-using-ssh

另外,现在常用git代码管理系统。目前最流行的是GitHub,推荐:

https://github.com/

To install git

brew install git

To clone a repository

URL is copied from github.

git clone https://github.com/yzhong52/ESIShiny2.git

To save the code to the cloud git server

$ git add .
$ git commit -m "Add all code"
# git push

To download the code from the github sever to our aliyun server

git clone https://github.com/yzhong52/ESIShiny2.git

参考文献

https://rstudio.com/products/shiny/download-server/ubuntu/ https://docs.rstudio.com/shiny-server/#ubuntu-14.04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment