Rで自作関数:get_rstudio_version

関数名: get_rstudio_version

rstudioapi::versionInfo() を利用せずに、Rstudio のバージョンを取得する関数です。

Rコード

rstudio.exe の実行ファイルパスの取得

実行中の Windows プロセスから取得する

Windows環境で RStudio が起動している場合、PowerShell を R から呼び出して rstudio.exe の実行パスを直接抽出できます。

PowerShellGet-Process コマンドは、起動中のすべての RStudio 関連プロセス(メインプロセスやバックグラウンドのレンダリングプロセスなど)を個別に取得しますので、重複を排除して1つだけ抽出するために、Select-Object -Unique を追加しています。

# PowerShellを利用して実行中のrstudio.exeのフルパスを取得
cmd <- 'powershell -Command "(Get-Process -Name rstudio -ErrorAction SilentlyContinue).Path | Select-Object -Unique"'
rstudio_path <- system(cmd, intern = TRUE)

print(rstudio_path)
[1] "C:\\Program Files\\RStudio\\rstudio.exe"
レジストリから検索する

Windowsに RStudio が通常インストールされている場合、レジストリ情報からインストール先を特定できます。

# レジストリからRStudioのアンインストール情報を検索
reg_query <- try(
  readRegistry("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\RStudio", hive = "HLM"),
  silent = TRUE
)

if (!inherits(reg_query, "try-error")) {
  print(reg_query$InstallLocation)
}
[1] "C:\\Program Files\\RStudio"

RStudio のバージョン取得

本関数では、 PowerShell を利用して、実行中の rstudio.exe の実効パスを取得します。

get_rstudio_version <- function() {
  cmd <- 'powershell -Command "(Get-Process -Name rstudio -ErrorAction SilentlyContinue).Path | Select-Object -Unique"'

  rstudio <- system(cmd, intern = TRUE)

  if (!file.exists(rstudio)) {
    return(NA_character_)
  }

  cmd <- sprintf(
    'powershell -NoProfile -Command "(Get-Item -LiteralPath \\"%s\\").VersionInfo.ProductVersion"',
    rstudio
  )

  trimws(system(cmd, intern = TRUE))
}

get_rstudio_version()
[1] "2026.08.0+187"

以上です。