I am a beginner in R programming. I have coded a basic R script that allows the user to choose a .csv file with 2 columns (X and Y), plot these on a graph to show linear regression and print the related rsquared value of the XY relationship.
Can someone explain the basics of adding this code to gWidgets so I can have a GUI with two buttons? - button1 will be a file.choose() to select the .csv file (i've seen this code elsewhere), button2 will run linear regression and output the plot to a display window.
I've been through all the gWidgets tutorials and code snippets but it is still not clear to me how to carry out a simple function as I have described above. I think I thought it would be possible to just cut and paste my existing R code into a gWidgets button, but I now see this is not the case.
Any help will be greatly appreciated.
Kind Regards
#CALCULATES LINEAR REGRESSION SCATTER PLOT FOR TWO COLUMNS - X and Y
# ALSO PRINTS r2 VALUE ON PLOT
#READ IN THE .CSV FILE - FILE SHOULD CONTAIN TWO COLUMNS WITH NO HEADER - X and Y
#CREATE DATAFRAME
test1 <- read.csv("C:/Data/test.csv", header=FALSE)
#ASSIGN THE DATA IN EACH COLUMN TO A VARIABLE NAME
X1 <- test1$V1
Y1 <- test1$V2
#PLOT DATA WITH APPROPRIATE LABELS
plot(X1, Y1, xlab="X", ylab="Y")
#CALCULATE THE BEST FIT LINE AND PRINT R2 VALUE ON PLOT
abline(fit <- lm(X1 ~ Y1, data=test1), col='red')
legend("topright", bty="n", legend=paste("r2 is", format(summary(fit)$adj.r.squared, digits=4)))
I guess you are looking for something simple like:
library(gWidgets)
options(guiToolkit="tcltk") ## or RGtk2 ...
w <- gwindow("Your GUI", visible=FALSE)
g <- ggroup(cont=w, horizontal=FALSE)
upload <- gfilebrowse("Select a csv file", cont=g)
btn <- gbutton("Do it", cont=g)
enabled(btn) <- FALSE
filename <- NULL
do_it <- function(...) {
message("Read csv file in filename")
message("make graphic")
message("make summary")
}
addHandlerChanged(upload, handler=function(h, ...) {
val <- svalue(upload)
if(!is.na(val)) {
filename <<- val
enabled(btn) <- TRUE
}
})
addHandlerChanged(btn, handler=do_it)
visible(w) <- TRUE