2017-09-03 960 views
1

我正在努力編寫一個腳本,它會以某種方式爲最新的RStudio版本的數量刮掉https://www.rstudio.com/products/rstudio/download/,下載並安裝它。如何獲取最新的RStudio版本

由於我是R程序員,我開始使用rvest包編寫R腳本。我設法刮掉RStudio服務器的下載鏈接,但我仍然無法獲得RStudio本身。

這裏是獲取用於Ubuntu的64位RStudio服務器的下載鏈接的R代碼。

if(!require('stringr')) install.packages('stringr', Ncpus=8, repos='http://cran.us.r-project.org') 
if(!require('rvest')) install.packages('rvest', Ncpus=8, repos='http://cran.us.r-project.org') 

xpath<-'//code[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]' 
url<-'https://www.rstudio.com/products/rstudio/download-server/' 
thepage<-xml2::read_html(url) 
the_links_html <- rvest::html_nodes(thepage,xpath=xpath) 
the_links <- rvest::html_text(the_links_html) 
the_link <- the_links[stringr::str_detect(the_links, '-amd64\\\\.deb')] 
the_r_uri<-stringr::str_match(the_link, 'https://.*$') 
cat(the_r_uri) 

不幸的是,RStudio桌面下載頁面有完全不同的佈局,我同樣的方法不在這裏工作了。

有人可以幫助我嗎?我不敢相信,世界上所有的數據科學家都會手動升級他們的RStudio!


有一個更簡單的腳本版本,讀取RStudio-server的版本。 Bash的版本:

RSTUDIO_LATEST=$(wget --no-check-certificate -qO- https://s3.amazonaws.com/rstudio-server/current.ver) 

或R版本:

scan('https://s3.amazonaws.com/rstudio-server/current.ver', what = character(0)) 

但RStudio桌面的版本仍然逃避我。

+0

R Studio是否在運行時不檢查自己的更新? – Spacedman

+0

是的,它是一個可配置的選項 - 但這不是在這裏要求的。 – mdsumner

+0

@mdsumner是的,但RStudio是開源的,所以解決方案就在那裏。 – Spacedman

回答

1

如果查詢RStudio的check_for_update帶有版本字符串,你會回來更新版本和從哪裏得到它的網址:

https://www.rstudio.org/links/check_for_update?version=1.0.0

更新版本= 1.0.153 &更新URL = HTTPS%3A%2F%2Fwww.rstudio.com%2Fproducts% 2Firstudio%2Fdownload%2F & up日期的消息= RStudio%201.0.153%圖20是%20now%20available%20%28you%27re%20using%201.0。0%29 &更新緊急= 0

在這裏看到:

https://github.com/rstudio/rstudio/blob/54cd3abcfc58837b433464c793fe9b03a87f0bb4/src/cpp/session/modules/SessionUpdates.R

如果你真的想從下載頁面刮它,然後我會得到<a>中的href第一個<td>的類「下載」類的第一個<table>,然後解析出「RStudio-」和「.exe」之間的三個點分隔的數字。所有平臺上的RStudio發行版本都應該足夠用於Windows下載。

> url = "https://www.rstudio.com/products/rstudio/download/" 
> thepage<-xml2::read_html(url) 
> html_node(thepage, ".downloads td a") %>% html_attr("href") 
[1] "https://download1.rstudio.org/RStudio-1.0.153.exe" 
+0

這是我解決的一個問題:'stringr :: str_match(scan(「https://www.rstudio.org/links/check_for_update?version=1.0.0」,what = character(0)), '^ [^ =] + =([^ \\&] +)\\&*')[[2]]' –

0

有一個幾乎這裏的解決方案:

https://hub.docker.com/r/rocker/rstudio-daily/~/dockerfile/

在這個腳本,它擦傷最新的構建:

https://raw.githubusercontent.com/rocker-org/rstudio-daily/master/latest.R

你要修改劇本是更嚴格的接受什麼,即我想要這一個rstudio-server-1.1.355-amd64.deb而不是stretch變種。

(但你可以修改它的目標的那種你想反正身材,這是每日生成,RStudio服務器的Ubuntu。)

+0

謝謝。你提出的immidiate鏈接是RStudio(服務器)的日常構建,但這可以很容易地修復。對於穩定的RStudio(服務器),他們使用'RSTUDIO_LATEST = $(wget --no-check-certificate -qO- https://s3.amazonaws.com/rstudio-server/current.ver)' –

+0

真正的問題與你的提議是''rocker'只服務於RStudio-server,因爲它們使用docker容器,並且從來不打算封裝GUI應用程序。我已經有了一個網頁抓取腳本來讀取當前的RStudio-server版本。 –

0

如果有人有興趣,這裏是我最終的RServer-desktop-on-Ubuntu更新腳本。它安裝了RStudio-desktop 64位,然後,如果Fira Console字體可用,則從https://github.com/tonsky/FiraCode/wiki/RStudio-instructions開始爲RStudio應用修補程序,以便連字開始工作。

#!/bin/bash 
if dpkg -s rstudio >/dev/null 2>/dev/null; then 
    ver=$(apt show rstudio | grep Version) 
    pattern='^Version: ([0-9.]+)\s*$' 
    if [[ $ver =~ $pattern ]]; then 
     ourversion=${BASH_REMATCH[1]} 
     netversion=$(Rscript -e 'cat(stringr::str_match(scan("https://www.rstudio.org/links/check_for_update?version=1.0.0", what = character(0), quiet=TRUE), "^[^=]+=([^\\&]+)\\&.*")[[2]])') 
     if [[ $ourversion != $netversion ]]; then 
      RSTUDIO_URI=$(Rscript /tmp/get_rstudio_uri.R) 
     fi 
     tee /tmp/get_rstudio_uri.R <<EOF 
if(!require('rvest')) install.packages('rvest', repos='http://cran.us.r-project.org') 
xpath='.downloads:nth-child(2) tr:nth-child(5) a' 
url = "https://www.rstudio.com/products/rstudio/download/" 
thepage<-xml2::read_html(url) 
cat(html_node(thepage, xpath) %>% html_attr("href")) 
EOF 
     RSTUDIO_URI=$(Rscript /tmp/get_rstudio_uri.R) 

     wget -c --output-document /tmp/rstudio.deb $RSTUDIO_URI 
     sudo dpkg -i /tmp/rstudio.deb 
     rm /tmp/rstudio.deb 
     rm /tmp/get_rstudio_uri.R 

     if fc-list |grep -q FiraCode; then 
      if !grep -q "text-rendering:" /usr/lib/rstudio/www/index.htm; then 
       sudo sed -i '/<head>/a<style>*{text-rendering: optimizeLegibility;}<\/style>' /usr/lib/rstudio/www/index.htm 
      fi 
     fi 
    fi 
fi