我試圖用R打開RStudio中使用的.Rproj文件。我已成功使用下面的代碼(從Ananda here盜取)。但是,打開文件後,從R調用的打開RStudio的連接並未關閉。 打開.Rproj文件後,如何切斷此「連接」?(PS尚未在Linux或Mac上測試過)。系統打開RStudio關閉連接
## Create dummy .Rproj
x <- c("Version: 1.0", "", "RestoreWorkspace: Default", "SaveWorkspace: Default",
"AlwaysSaveHistory: Default", "", "EnableCodeIndexing: Yes",
"UseSpacesForTab: No", "NumSpacesForTab: 4", "Encoding: UTF-8",
"", "RnwWeave: knitr", "LaTeX: pdfLaTeX")
loc <- file.path(getwd(), "Bar.rproj")
cat(paste(x, collapse = "\n"), file = loc)
## wheresRStudio function to find RStudio location
wheresRstudio <-
function() {
myPaths <- c("rstudio", "~/.cabal/bin/rstudio",
"~/Library/Haskell/bin/rstudio", "C:\\PROGRA~1\\RStudio\\bin\\rstudio.exe",
"C:\\RStudio\\bin\\rstudio.exe")
panloc <- Sys.which(myPaths)
temp <- panloc[panloc != ""]
if (identical(names(temp), character(0))) {
ans <- readline("RStudio not installed in one of the typical locations.\n
Do you know where RStudio is installed? (y/n) ")
if (ans == "y") {
temp <- readline("Enter the (unquoted) path to RStudio: ")
} else {
if (ans == "n") {
stop("RStudio not installed or not found.")
}
}
}
temp
}
## function to open .Rproj files
open_project <- function(Rproj.loc) {
action <- paste(wheresRstudio(), Rproj.loc)
message("Preparing to open project!")
system(action)
}
## Test it (it works but does no close)
open_project(loc)
Downvoter保健佳品,爲什麼這樣我就可以改善這個問題? –
你嘗試過'closeAllConnections()'嗎? –
@Roman通常情況下,這可能會起作用,但是這個用法是用在一個包中的,我記得Yihui說過使用'closeAllConnections'是不好的,因爲你不知道其他包中的其他連接可能會關閉。 –