HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
Excel/R
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 String
cmdLine = "C:Program FilesRR-2.15.2binRscript C:example.R"
cmdLine = cmdLine & " " & Range("B2").Value & " " & Range("B3").Value
Shell cmdLine
End Sub
*adjust path to R.exe and R script (example.R) as needed
2. Assign macro (called open_pdf_file) to the button Open plots:
Sub open_pdf_file()
Dim strPDF_File_Name As String
strPDF_File_Name = "C:plots.pdf"
If Dir(strPDF_File_Name) <> "" Then
ActiveWorkbook.FollowHyperlink strPDF_File_Name
Else: MsgBox ("First you have to create plots")
End If
End Sub
Save 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: