【问题标题】:Run multiple instances of RStudio in a web browser在 Web 浏览器中运行多个 RStudio 实例
【发布时间】:2014-05-21 05:22:43
【问题描述】:

我在远程 aws 服务器 (ubuntu) 上安装了 RStudio 服务器,并希望同时运行多个项目(其中一个项目需要大量时间才能完成)。在 Windows 上,有一个简单的 GUI 解决方案,例如“在新窗口中打开项目”。 rstudio 服务器有类似的东西吗?

简单的问题,但没有找到解决方案,除了这个相关的question for Macs,它提供了

使用项目运行多个 rstudio 会话

但是怎么做呢?

【问题讨论】:

    标签: r ubuntu amazon-web-services rstudio-server


    【解决方案1】:

    虽然运行批处理脚本当然是一个不错的选择,但它并不是唯一的解决方案。有时您可能仍希望在不同会话中进行交互使用,而不是必须将所有内容都作为批处理脚本。

    没有什么能阻止您在 Ubuntu 服务器上的不同端口上运行多个 RStudio 服务器实例。 (我发现通过 docker 启动 RStudio,as outlined here 很容易做到这一点。因为即使关闭浏览器窗口,实例也会继续运行,因此您可以轻松启动多个实例并在它们之间切换。您只需登录当你切换时再次。

    不幸的是,RStudio-server 仍然阻止您在浏览器中同时打开多个实例(请参阅help forum)。这不是一个大问题,因为您只需要再次登录,但您可以通过使用不同的浏览器来解决它。

    编辑:多个实例都可以,只要它们不在同一个浏览器、同一个浏览器用户和同一个 IP 地址上。例如在 127.0.0.1 和另一个 0.0.0.0 上的会话会很好。更重要的是,即使实例没有“打开”,它们也会继续运行,所以这真的不是问题。唯一需要注意的是,您必须重新登录才能访问该实例。

    至于项目,您会看到您可以使用右上角的“项目”按钮在项目之间切换,但是虽然这会保留您的其他会话,但我认为它实际上并不支持同时执行代码。您需要运行多个 R 环境实例才能真正做到这一点。

    2020 年更新 好的,现在是 2020 年,有很多方法可以做到这一点。

    要在新的 R 环境中运行脚本或函数,请查看:

    • callr package

    • RStudio jobs panel

    • RStudio terminal panel 中的一个或多个终端会话运行新的 R 会话或脚本

    • 注销并以不同用户身份登录到 RStudio-server(需要在容器中设置多个用户,对于单个用户来说显然不是一个好的工作流程,但只是注意到许多不同的 用户可以访问同一个 RStudio 服务器实例没问题。

    当然,在不同的端口上启动多个 docker 会话仍然是一个不错的选择。请注意,上面列出的许多方法仍然不允许您重新启动主 R 会话,这会阻止您重新加载已安装的包、在项目之间切换等,这显然不理想。我认为,如果在 RStudio(服务器)会话中的项目之间切换将允许先前活动项目中的作业继续在后台运行,但不知道开源版本是否存在这种情况,那将是非常棒的。

    【讨论】:

    • 人们也可以利用浏览器的“配置文件”而不是不同的浏览器。
    • 在同一浏览器上使用不同的浏览器“配置文件”完全不同的浏览器不起作用。一旦您尝试在第二个浏览器/选项卡上登录,第一个选项卡上就会出现一个弹出窗口,说明 “此浏览器已与 R 会话断开连接,因为连接了另一个浏览器(一次只能连接一个浏览器) RStudio 会话)。您可以使用下面的按钮重新连接。" Rstudio 服务器版本 - 0.99.467。
    • 这里是一个示例命令 - docker run -d -p 9090:8787 -v /home/user/analytics:/home/dockeruser/analytics -v /mnt/libs:/home/dockeruser/ libs --name mydocker -e USER=dockeruser -e PASSWORD=dockerpassw -e ROOT=TRUE rocker/hadleyverse
    • @cboettig 我的理解正确吗,使用 docker 拥有多个 R 环境实例是同时运行多个实例的唯一方法?
    • @JamesHirschorn 有很多方法可以拥有多个 R 环境实例。要从单个 RStudio 会话执行此操作,只需在任意数量的“终端”会话选项卡中运行“R”。或查看callr。另请查看 RStudio“工作”面板以运行脚本。
    【解决方案2】:

    通常您不需要多个 Rstudio 实例 - 在这种情况下,只需将您的代码保存在 .R 文件中并使用 ubuntu 命令提示符(可能使用屏幕)启动它

    Rscript script.R
    

    这将启动一个单独的 R 会话,该会话将在不冻结您的 Rstudio 的情况下完成工作。你也可以传递参数,例如

    # script.R - 
    args <- commandArgs(trailingOnly = TRUE)
    
    if (length(args) == 0) {
      start = '2015-08-01'
    } else {
      start = args[1]  
    }
    

    控制台 -

     Rscript script.R 2015-11-01
    

    【讨论】:

    • 是的,但是如果您想同时运行部分脚本怎么办?
    【解决方案3】:

    我认为您需要 R Studio Server Pro 才能使用多个用户/会话登录。

    您可以查看下面的比较表以供参考。

    https://www.rstudio.com/products/rstudio-server-pro/

    【讨论】:

      【解决方案4】:

      安装另一个 rstudio 服务器实例并不理想。

      Linux 服务器管理员,不要害怕。您只需要 root 访问权限或善良的管理员。

      创建要使用的组:groupadd Rwarrior

      创建一个与您的主 Rstudio 登录具有相同主目录的附加用户:

      useradd -d /home/user1 user2

      将主要用户和新用户添加到 Rwarrior 组:

      gpasswd -a user2 Rwarrior

      gpasswd -a user1 Rwarrior

      注意您的主主目录​​的权限:

      cd /home

      chown -R user1:Rwarrior /home/user1

      chmod -R 770 /home/user1

      chmod g+s /home/user1

      为新用户设置密码: 密码用户2

      以隐身/隐私浏览模式打开一个新的浏览器窗口,并使用您创建的新用户登录 Rstudio。享受。

      【讨论】:

      • 这曾经运行良好,但与最新版本的 Rstudio 不兼容!
      【解决方案5】:

      我通过在 Singularity 实例中隔离它们来运行多个 RStudio 服务器。使用命令singularity pull shub://nickjer/singularity-rstudio下载奇点镜像

      我使用两个脚本:

      run-rserver.sh:

      • 找到一个空闲的端口
      #!/bin/env bash
      set -ue
      
      thisdir="$(dirname "${BASH_SOURCE[0]}")"
      
      # Return 0 if the port $1 is free, else return 1
      is_port_free(){
        port="$1"
        set +e
        netstat -an | 
          grep --color=none "^tcp.*LISTEN\s*$" | \
          awk '{gsub("^.*:","",$4);print $4}' | \
          grep -q "^$port\$"
        r="$?"
        set -e
        if [ "$r" = 0 ]; then return 1; else return 0; fi
      }
      
      # Find a free port
      find_free_port(){
          local lower_port="$1"
          local upper_port="$2"
          for ((port=lower_port; port <= upper_port; port++)); do
            if is_port_free "$port"; then r=free; else r=used; fi
            if [ "$r" = "used" -a "$port" = "$upper_port" ]; then
              echo "Ports $lower_port to $upper_port are all in use" >&2
              exit 1
            fi
            if [ "$r" = "free" ]; then break; fi
          done
          echo $port
      }
      
      port=$(find_free_port 8080 8200)
      
      echo "Access RStudio Server on http://localhost:$port" >&2
      
      
      "$thisdir/cexec" \
          rserver \
          --www-address 127.0.0.1 \
          --www-port $port
      

      cexec:

      • 为每个实例创建一个专用的配置目录
      • 为每个实例创建一个专用的临时目录
      • 使用singularity instance 机制来避免分叉的R 会话被PID 1 采用并在rserver 关闭后保留。相反,它们成为 Singularity 实例的子节点,并在该实例关闭时被杀死。
      • 将当前目录映射到容器内的目录/data 并将其设置为主文件夹(如果您不关心每台机器上的可重现路径,则此步骤可能不是必需的)
      #!/usr/bin/env bash
      # Execute a command in the container
      set -ue
      
      if [ "${1-}" = "--help" ]; then 
      echo <<EOF
      Usage: cexec command [args...]
      
      Execute `command` in the container. This script starts the Singularity
      container and executes the given command therein. The project root is mapped 
      to the folder `/data` inside the container. Moreover, a temporary directory
      is provided at `/tmp` that is removed after the end of the script.
      
      EOF
      exit 0
      fi
      
      thisdir="$(dirname "${BASH_SOURCE[0]}")"
      container="rserver_200403.sif"
      
      # Create a temporary directory
      tmpdir="$(mktemp -d -t cexec-XXXXXXXX)"
      # We delete this directory afterwards, so its important that $tmpdir
      # really has the path to an empty, temporary dir, and nothing else!
      # (for example empty string or home dir)
      if [[ ! "$tmpdir" || ! -d "$tmpdir" ]]; then
        echo "Error: Could not create temp dir $tmpdir"
        exit 1
      fi
      # check if temp dir is empty (this might be superfluous, see
      # https://codereview.stackexchange.com/questions/238439)
      tmpcontent="$(ls -A "$tmpdir")"
      if [ ! -z "$tmpcontent" ]; then
        echo "Error: Temp dir '$tmpdir' is not empty"
        exit 1
      fi
      
      # Start Singularity instance
      instancename="$(basename "$tmpdir")"
      
      # Maybe also superfluous (like above)
      rundir="$(readlink -f "$thisdir/.run/$instancename")"
      if [ -e "$rundir" ]; then
        echo "Error: Runtime directory '$rundir' exists already!" >&2
        exit 1
      fi
      mkdir -p "$rundir"
      
      singularity instance start \
        --contain \
        -W "$tmpdir" \
        -H "$thisdir:/data" \
        -B "$rundir:/data/.rstudio" \
        -B "$thisdir/.rstudio/monitored/user-settings:/data/.rstudio/monitored/user-settings" \
        "$container" \
        "$instancename"
      
      # Delete the temporary directory after the end of the script
      trap "singularity instance stop '$instancename'; rm -rf '$tmpdir'; rm -rf '$rundir'" EXIT
      singularity exec \
        --pwd "/data" \
        "instance://$instancename" \
        "$@"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-12
        相关资源
        最近更新 更多