【问题标题】:How to reload current R script file with a command?如何使用命令重新加载当前的 R 脚本文件?
【发布时间】:2019-12-04 18:10:37
【问题描述】:

当前的R脚本文件被其他程序修改后如何用命令重新加载?

编辑:

假设有一个名为 test.R 的初始文件,其内容如下:

1 # a random number

y <- readLines("test.R") # read the script

x <- as.numeric(y[1]) # convert the number to numeric
x <- x + 1 # increase the number by 1
y[1] <- x # assign the value to y
print(y[1]) 
writeLines(y, "test.R") # replace the old file by the new file

现在我想重新加载当前文件,以便在文件本身中更新内容。几年来,RStudio 具有重新加载功能,但我不能等到触发 RStudio 的更新信号。我想自己触发。 这是相关的,因为我想编写一个 R 函数,它会自动为xaringan 生成目录。

SO 上的相关问题。

【问题讨论】:

  • 不确定你想要什么。 .rs.restartR() 会重启 Rstudio 会话吗?当您在例如 notepad++ 中编辑脚本时,它会在 Rstudio 中自动更新?
  • 嘿,谢谢。不,我不谈论重新启动会话。是的,我知道 Rstudio 的重新加载功能,但是当您处于 xaringan 的无限月亮阅读器模式时不会触发此功能

标签: r rstudio


【解决方案1】:

我能做的就是

document_reload <- function() {
  path <- rstudioapi::getActiveDocumentContext()$path
  rstudioapi::documentClose()
  shell(path)
}

document_reload()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2015-02-04
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多