How to find R_HOME with VBA Environ

I can find the path to R by using Sys.getenv() like so:

Sys.getenv("R_HOME")
[1] "C:/PROGRA~1/R/R-41~1.1"

However, I am trying to find the R_HOME with the Environ function from Excel VBA with a subroutine something like:

Sub findR()
Dim a As String
a = Environ$("R_HOME")
MsgBox a
End Sub

The message box returns empty. R_HOME is in the environment but for some reason Environ can’t find it. See a screenshot attached showing R_HOME in the environment. Could someone point me in the right direction to find R_HOME with Environ VBA?

Recognized by R Language Collective

This VBA function, GetREnviron, is designed to retrieve environment variables from an R installation, such as R_HOME, by running a small R script through the Windows command line.

Parameter EnvName: The function takes one parameter, EnvName, which is the name of the environment variable you want to retrieve (e.g., “R_HOME”).

R Script Construction: The function dynamically creates a small R script (Rscript -e “cat(Sys.getenv(‘EnvName’))”) to retrieve the value of the specified environment variable using R’s Sys.getenv function.

Shell Execution: It uses the WScript.Shell object to execute the R script and capture the output (StdOut.ReadAll). This output is the value of the environment variable.

Trimming Output: Any extra whitespace or newline characters are trimmed from the output.

Error Handling: If something goes wrong (e.g., R is not installed, or the variable doesn’t exist), the function returns a generic error message, “Error: Unable to retrieve EnvName”.

This approach allows for dynamically retrieving any R environment variable by passing its name as an argument to the function.

Function GetREnviron(Name As String, Optional TrimResult As Boolean = True) As String
    Dim RScript As String
    Dim ShellOutput As String
    On Error GoTo ErrHandler
        
    Rem R script to print the specified environment variable
    RScript = "Rscript -e ""cat(Sys.getenv('" & Name & "'))"""
        
    Rem Create a Shell object
    With VBA.CreateObject("WScript.Shell")
        Rem Run the R script and capture the output
        ShellOutput = .Exec(RScript).StdOut.ReadAll
    End With
        
    Rem Trim any extra whitespace from the output
    GetREnviron = IIf(TrimResult, Trim(ShellOutput), ShellOutput)
        
    Exit Function
    
ErrHandler:
    GetREnviron = "Error: Unable to retrieve " & Name
End Function

I’ve updated the code to store the environmental variable. This will avoid having to use WScript.Shell more than once per variable per machine.

Function GetREnviron(Name As String, Optional UpdateSetting As Boolean = False, Optional TrimResult As Boolean = True) As String
    Dim RScript As String
    Dim ShellOutput As String
    Dim SavedSettings As Variant
    Dim StoredValue As String
    Dim AppName As String
    Dim Section As String
    On Error GoTo ErrHandler
    
    AppName = "REnvironSettings"  ' Application name for SaveSetting
    Section = "EnvironmentVars"    ' Section to store environment variables
    
    ' Retrieve all saved settings
    SavedSettings = GetAllSettings(AppName, Section)
    
    ' Check if the variable is already saved and UpdateSetting is False
    If Not UpdateSetting And Not IsEmpty(SavedSettings) Then
        On Error Resume Next
        StoredValue = GetSetting(AppName, Section, Name, "")
        On Error GoTo ErrHandler
        If Len(StoredValue) > 0 Then
            GetREnviron = StoredValue
            Exit Function
        End If
    End If

    ' If not saved or UpdateSetting is True, run the R script to get the environment variable
    RScript = "Rscript -e ""cat(Sys.getenv('" & Name & "'))"""
        
    ' Create a Shell object and capture the output
    With VBA.CreateObject("WScript.Shell")
        ShellOutput = .Exec(RScript).StdOut.ReadAll
    End With
        
    ' Trim any extra whitespace from the output
    ShellOutput = IIf(TrimResult, Trim(ShellOutput), ShellOutput)
    
    ' Save the retrieved environment variable value
    SaveSetting AppName, Section, Name, ShellOutput
    
    ' Return the result
    GetREnviron = ShellOutput
    
    Exit Function
    
ErrHandler:
    GetREnviron = "Error: Unable to retrieve " & Name
End Function

Addendum

It turns out we can get R_HOME by reading the Windows Registry.

Rem Function to get the value from the registry
Function GetRegistryValue(keyPath As String, valueName As String) As String
    Dim Reg As Object
    Dim Key As String
    Dim Value As String

    Rem Create WScript.Shell to interact with the registry
    Set Reg = CreateObject("WScript.Shell")

    Rem Try to read the registry value
    On Error Resume Next
    Value = Reg.RegRead("HKEY_LOCAL_MACHINE" & keyPath & "" & valueName)
    On Error GoTo 0

    Rem Return the result (the registry value)
    GetRegistryValue = Value
End Function

Rem Function to get the R installation directory path
Function GetR_HOME() As String
    Rem Call GetRegistryValue to retrieve the R "InstallPath" from the registry
    GetR_HOME = GetRegistryValue("SOFTWARER-coreR", "InstallPath")
End Function

24

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật