【问题标题】:Automated Installation of R-Studio Using Shell Script使用 Shell 脚本自动安装 R-Studio
【发布时间】:2015-06-22 10:09:51
【问题描述】:

有没有办法在 Linux 系统上自动安装 R-Studio?它应该自动检测操作系统并安装具有所需依赖项的 R 和 R-Studio。 谢谢..

【问题讨论】:

    标签: linux r bash shell rstudio


    【解决方案1】:

    我准备了以下 shell 脚本以使安装完全自动化。

    #!/bin/bash
    # ******************************************
    # Program: R-Studio Installation Script
    # Developer: Pratik Patil
    # Date: 16-04-2015
    # Last Updated: 16-04-2015
    # ********************************************
    
    if [ "`lsb_release -is`" == "Ubuntu" ] || [ "`lsb_release -is`" == "Debian" ]
    then
        sudo apt-get -y install r-base gdebi-core libapparmor1;
        sudo wget http://download2.rstudio.org/rstudio-server-0.98.1103-amd64.deb;
        sudo gdebi rstudio-server-0.98.1103-amd64.deb;
    
    elif [ "`lsb_release -is`" == "CentOS" ] || [ "`lsb_release -is`" == "RedHat" ]
    then
        sudo yum -y install R openssl098e;
        sudo wget http://download2.rstudio.org/rstudio-server-0.98.1103-x86_64.rpm;
        sudo yum -y install --nogpgcheck rstudio-server-0.98.1103-x86_64.rpm;
    else
        echo "Unsupported Operating System";
    fi
    
    sudo rm -f rstudio-server-*;
    sudo rstudio-server verify-installation;
    

    如何在 Ubuntu 或 Debian 系统中启动 R-Studio? 在终端输入以下命令:

    rstudio;
    

    如何在 CentOS 或 RedHat 系统中启动 R-Studio? 在浏览器中打开以下网址:

    http://localhost:8787
    

    之后将出现登录提示,然后使用当前系统用户凭据登录。

    【讨论】:

      【解决方案2】:

      我使用它在 Ubuntu(32 位或 64 位)上安装 R 的当前版本和 RStudio 的预览版本,以及我经常使用的 pkg:

      #!/bin/bash
      
      # this script can be run from the terminal with the next line (minus the #)
      # bash install_things.sh
      
      # you may  need to enter your password at a few points, so keep an eye on it while it runs.
      
      # in case we need to step through:
      # set -x
      # trap read debug
      
      echo "install a few dependancies for our workflow"
      sudo apt-get update  -y
      # sudo apt-get upgrade  -y
      sudo apt-get install libgstreamer0.10-0 -y
      sudo apt-get install libgstreamer-plugins-base0.10-dev -y
      sudo apt-get install libcurl4-openssl-dev -y
      sudo apt-get install libssl-dev -y
      sudo apt-get install libopenblas-base -y
      sudo apt-get install libxml2-dev -y
      sudo apt-get install make -y
      sudo apt-get install gcc -y
      sudo apt-get install git -y
      sudo apt-get install pandoc -y
      sudo apt-get install libjpeg62 -y
      sudo apt-get install unzip -y
      sudo apt-get install curl -y
      sudo apt-get install littler -y
      sudo apt-get install openjdk-7-* -y
      sudo apt-get install gedit -y
      sudo apt-get install jags -y
      sudo apt-get install imagemagick -y
      sudo apt-get install docker-engine -y
      
      
      echo "edit the sources file to prepare to install R"
      # see http://cran.r-project.org/bin/linux/ubuntu/README
      sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu xenial/" >> /etc/apt/sources.list' # I'm using Lubuntu
      
      echo "get keys to install R"
      sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9 # # I'm using Lubuntu
      
      echo "install R and some helpers"
      sudo apt-get update
      sudo apt-get install r-base  -y
      sudo apt-get install r-base-dev -y
      sudo apt-get install r-cran-xml  -y
      sudo apt-get install r-cran-rjava -y
      sudo R CMD javareconf # for rJava
      
      echo "install RStudio from the web"
      # use daily build to get rmarkdown & latest goodies
      # http://*.com/a/15046782/1036500
      # check if 32 or 64 bit and install appropriate version... 
      # http://*.com/a/106416/1036500
      
      MACHINE_TYPE=`uname -m`
      if [ ${MACHINE_TYPE} == 'x86_64' ]; then
        # 64-bit stuff here
      
      URL=$(wget -q -O -  http://www.rstudio.org/download/daily/desktop/ubuntu64 | grep -o -m 1 "https[^\']*" )
      
      FILE=`mktemp`; sudo wget "$URL" -qO $FILE && sudo dpkg -i $FILE; rm $FILE
      
      else
        # 32-bit stuff here
      
      URL=$(wget -q -O -  http://www.rstudio.org/download/daily/desktop/ubuntu32 | grep -o -m 1 "https[^\']*" )
      
      FILE=`mktemp`; sudo wget "$URL" -qO $FILE && sudo dpkg -i $FILE; rm $FILE
      fi
      
      
      echo "start R and install commonly used packages"
      # http://*.com/q/4090169/1036500
      # Make an R script file to use in a moment...
      LOADSTUFF="options(repos=structure(c(CRAN='http://cran.rstudio.com/')))
      update.packages(checkBuilt = TRUE, ask = FALSE)
      packages <- c('codetools', 'Rcpp', 'devtools', 'knitr', 'ggplot2',  'plyr', 'dplyr', 'XML', 'RCurl', 'readxl', 'tidyr') 
      # just some of my most often used ones
      install.packages(packages)
      # and some from github
      devtools::install_git(c('https://github.com/rstudio/rmarkdown.git',  'https://github.com/hadley/reshape'))" # close the R script
      
      # put that R code into an R script file
      FILENAME1="loadstuff.r"
      sudo echo "$LOADSTUFF" > /tmp/$FILENAME1
      
      # Make a shell file that contains instructions in bash for running that R script file
      # from the command line. There may be a simpler way, but nothing I tried worked.
      NEWBASH='#!/usr/bin/env 
      sudo Rscript /tmp/loadstuff.r'
      FILENAME2="loadstuff.sh"
      
      # put that bash code into a shell script file
      sudo echo "$NEWBASH" > /tmp/$FILENAME2
      
      # run the bash file to exectute the R code and install the packages
      sh /tmp/loadstuff.sh
      # clean up by deleting the temp file
      rm /tmp/loadstuff.sh 
      
      # done.
      echo "all done"
      echo "type 'sudo rstudio' in the terminal to start RStudio"
      

      这来自我的虚拟机设置脚本:https://gist.github.com/benmarwick/11204658

      【讨论】:

      • 不错。但作为一般提示,sudo apt-get install 可以接受多个参数。所以我会折叠上面just how we do it in the Rocker containers 上面的许多重复行。
      • 谢谢,是的,你的方法更整洁了。