Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Excel/R

Example of running an R script from Excel using VBA

  • Loggen Sie sich ein, um Kommentare anzuzeigen.

Excel/R

  1. 1. Excel / R - Example of running an R script from Excel -Open an Excel file, create following table and insert two buttons using figures from Shapes tab:Write an R script, named it example.R and save it on C drive:library(gWidgets)options(guiToolkit="tcltk")Args <- commandArgs(TRUE)n1 <- Args[1]n2 <- Args[2]pdf("C:plots.pdf")par(mfrow=c(2,1))plot(rnorm(n1), type="l", main=paste("Scatterplot of rnorm(", n1,")",sep=""))hist(rnorm(n2), col="red", main=paste("Histogram of rnorm(", n2, ")",sep=""))dev.off()gmessage("Plots are created.")Assign macro (called run_r_script) to the button Create plots running an R script:Sub run_r_script()Dim cmdLine As StringcmdLine = "C:Program FilesRR-2.15.2binRscript C:example.R"cmdLine = cmdLine & " " & Range("B2").Value & " " & Range("B3").ValueShell cmdLineEnd Sub*adjust path to R.exe and R script (example.R) as needed
  2. 2. Assign macro (called open_pdf_file) to the button Open plots:Sub open_pdf_file()Dim strPDF_File_Name As StringstrPDF_File_Name = "C:plots.pdf"If Dir(strPDF_File_Name) <> "" ThenActiveWorkbook.FollowHyperlink strPDF_File_NameElse: MsgBox ("First you have to create plots")End IfEnd SubSave Excel file enabling macros. Insert some numbers into cells B2 and B3 (for example 111 and 222)sending it as arguments to R script:Press button Create plots running an R script and wait for getting a message that plots are created.Then press button Open plots to open created pdf file with plots:

×