Search code examples
rexcelvba

VBA Excel and R


I am looking for a way to connect and communicate with R from excel. The solution I found is Rexcel which seems to be almost 10 years old and does not work with Excel 365. Is there a new way available to interact with R from Excel?


Solution

  • You can use the R package RDCOMClient to control / modify an Excel file. For example, you can open an Excel file and modify it as you wish from R, write a data.frame from R to the Excel file, etc. Here is a simple example :

    library(RDCOMClient)
    xlApp <- COMCreate("Excel.Application")
    xlApp[["DisplayAlerts"]] <- FALSE
    xlApp[["Visible"]] <- TRUE
    
    path_To_Excel_File <- "C:/my_excel_File.xlsx"
    xlWbk <- xlApp$Workbooks()$Open(path_To_Excel_File, ReadOnly = TRUE)
    xlWbk$Sheets(1)$Range("A1")$Value()