如何使用命令重新加载当前的 R 脚本文件? [英] How to reload current R script file with a command?
问题描述
如何在当前 R 脚本文件被另一个程序更改后使用命令重新加载它?
How to reload current R script file with a command after it has been changed by another program?
假设有一个名为 test.R 的初始文件,内容如下:
Assume there is an initial file called test.R with following content:
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 生成一个目录.
and now I would like to reload the current file such that the content is updated within the file itself. Since a couple of years RStudio has a reloading feature, but I cannot wait until RStudio's update signal is triggered. I want to trigger it myself. This is relevant because I would like to write a R function which generates a table of content automatically for xaringan.
关于SO的链接问题.
推荐答案
我能做的就是
document_reload <- function() {
path <- rstudioapi::getActiveDocumentContext()$path
rstudioapi::documentClose()
shell(path)
}
document_reload()
这篇关于如何使用命令重新加载当前的 R 脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!