簡體   English   中英

有沒有辦法在 RStudio 控制台中顯示腳本文件中執行的行號?

[英]Is there a way to display in the RStudio console the executed line numbers from the script file?

我在 Chrome 中使用 RStudio Server Pro(又名 Workbench),並希望在控制台中顯示腳本文件中執行代碼的行號。 如果有這樣的設置,我至今沒能找到。 感謝您的任何建議!

例如,在 R 腳本中,我在第 70 行有以下代碼:

70 總結(lm_robust(y ~ x, data=DF))

在執行該行時,我進入控制台:

'> 摘要(lm_robust(y ~ x, data=DF))

但我想得到類似我看到執行的行的地方:

'> 70摘要(lm_robust(y ~ x, data=DF))

也許控制台顯示從腳本執行的命令的邏輯不適合這種情況。 但我會發現查看腳本中執行了哪一行很有幫助。

據我所知,沒有辦法完全按照您的要求進行。

在常規 RStudio 中,可以編寫一個插件,在腳本的每一行末尾添加行號注釋。 然后你會看到

> summary(lm_robust(y ~ x, data=DF)) # line 70

當您從腳本執行該行時。 我不知道這在 Workbench 中是否可行,但我猜是這樣。

編輯添加:

以下是添加(或更新)行號注釋或刪除它們的幾個函數。 To make an add-in, you need to put them in an R package and follow the instructions here: https://rstudio.github.io/rstudio-extensions/rstudio_addins.html .

addLineNos <- function() { 
  if (!rstudioapi::isAvailable()) 
    stop("Rstudio API is not available") 
  ctxt <- rstudioapi::getSourceEditorContext() 
  contents <- ctxt$contents 
  # Get a range holding every line 
  lines <- seq_along(contents) 
  rng <- Map(c, Map(c, lines, 1), Map(c, lines, nchar(contents) + 1)) 
  # Remove old line numbers 
  contents <- gsub("# line [[:digit:]]+$", "", contents) 
  # Add new ones 
  contents <- paste(contents, "# line", seq_along(contents)) 
  # Put in place 
  rstudioapi::modifyRange(rng, contents, id = ctxt$id) 
} 
 
removeLineNos <- function() { 
  if (!rstudioapi::isAvailable()) 
    stop("Rstudio API is not available") 
  ctxt <- rstudioapi::getSourceEditorContext() 
  contents <- ctxt$contents 
  # Get a range holding every line 
  lines <- seq_along(contents) 
  rng <- Map(c, Map(c, lines, 1), Map(c, lines, nchar(contents) + 1)) 
  # Remove old line numbers 
  contents <- gsub("# line [[:digit:]]+$", "", contents) 
  # Put in place 
  rstudioapi::modifyRange(rng, contents, id = ctxt$id) 
} 
 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM