Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TCL, R Studio, and GUI's: Creating a temporary table for a Graph

45 views
Skip to first unread message

rio...@gmail.com

unread,
Jul 16, 2015, 9:33:15 AM7/16/15
to
As the title states: How do I create a temporary table using TCL in R studio?

My Current code:
============================================================================
intervalplot.press<-function(){

tt <- tktoplevel()
intervalplot_title<-tclVar("")
intervalplot_x<-tclVar("")
intervalplot_y<-tclVar("")
intervalplot_confidenceinterval=tclVar("")

tkwm.geometry(tt, "350x380")
tkwm.title(tt, "Interval Plot")

fr0<-tkframe(tt)
tkpack ( fr0, expand = F , fill="x")
tkgrid(tklabel(fr0,text="Select Variables from the list below: \n"),row=0,column=0)

fr<-tkframe(tt)
tkpack ( fr , expand = F , fill = "y" )
tkgrid(tklabel(fr,text="Numerical Variables"),row=0,column=0)
tkgrid(tklabel(fr,text="By categories (Max 2)"),row=0,column=2)

tl1 <- tklistbox(fr,height=10,width=15,selectmode="single", background="white",exportselection=F)
yscr1 <- tkscrollbar(fr, orient="vertical",repeatinterval=3, command=function(...) tkyview(tl1,...))
tkgrid(tklabel(fr,text=""),tl1,yscr1,sticky="nes",row=1,column=0)

tl2 <- tklistbox(fr,height=10,width=15,selectmode="multiple", background="white",exportselection=F)
yscr2 <- tkscrollbar(fr, orient="vertical",repeatinterval=3, command=function(...) tkyview(tl2,...))
tkgrid(tklabel(fr,text=""),tl2,yscr2,sticky="nes",row=1,column=2)

fr1<-tkframe(tt)
tkpack ( fr1 , expand = F , fill = "x" )
tkgrid(tklabel(fr1,text=""))

tkgrid(tklabel(fr1,text="Title:"),row=4,column=0)
tkgrid(tkentry(fr1,textvariable = intervalplot_title) , row=4 , column = 1, padx = 5 )

tkgrid(tklabel(fr1,text="X-axis label:"),row=5,column=0)
tkgrid(tkentry(fr1,textvariable = intervalplot_x) , row =5 , column = 1, padx = 5 )

tkgrid(tklabel(fr1,text="Y-axis label:"),row=6,column=0)
tkgrid(tkentry(fr1,textvariable = intervalplot_y) , row =6 , column = 1, padx = 5 )

tkgrid(tklabel(fr1,text="Confidence Interval (Decimal)"),row=7,column=0) # EDIT
tkgrid(tkentry(fr1,textvariable = intervalplot_confidenceinterval) , row =7 , column = 1, padx = 5 ) # EDIT


dataframe <- tclVar("myData")
df <- get(tclvalue(dataframe))
vars.name <- c(names(df))
len <- length(df)
vars.name.fac<-c("x")
vars.name.num<-c("x")
for (i in 1:len) {
if (class(myData[,i]) == "factor") {
tkinsert(tl2,"end",vars.name[i])
vars.name.fac<-c(vars.name.fac,vars.name[i])
}
else {
tkinsert(tl1,"end",vars.name[i])
vars.name.num<-c(vars.name.num,vars.name[i])
}
}
vars.name.fac<-vars.name.fac[-1]
vars.name.num<-vars.name.num[-1]
######For ToDo
order1<<-permn(c(1:2))
order2<<-permn(c(1:3))
ind1<-1
ind2<-1

####display summary result in the output window
ToDo<-function(){
tx.choice1<<-vars.name.num[as.integer(tkcurselection(tl1))+1]
tx.choice2<<-vars.name.fac[as.integer(tkcurselection(tl2))+1]
numb.select.num<<-length(tx.choice1)
numb.select.fac<<-length(tx.choice2)

windows()

# EDIT double check if this is dropped in future graphs, or if this would work
pd <- position_dodge(1)

#create temporary table of analysis
summary.myData=summarySE(myData, measurevar=tx.choice1, groupvars=c(tx.choice2[order1[[ind1]][1]],tx.choice2[order1[[ind1]][2]]),conf.interval=tclvalue(intervalplot_confidenceinterval),na.rm=TRUE,.drop=FALSE)

if (numb.select.num!=0){
if (numb.select.fac==0){
stop(tkmessageBox(message="Please select at least one categorical variable!", icon="error"))
}
else if (numb.select.fac==1) {
if(tclvalue(intervalplot_title)=="" & tclvalue(intervalplot_x)=="" & tclvalue(intervalplot_y)==""){
box0<-ggplot(data=summary.myData,aes(x=tx.choice2, y=tx.choice1))+ #check if you need "[order1[[ind1]][1]]" for "x=tx.choice2"
geom_errorbar(aes(ymin=tx.choice1-ci, ymax=tx.choice1+ci), colour="black", width=.5, position=pd)+
geom_point(position=pd, size=3)+
labs(title =tclvalue(intervalplot_title))
} else {
box0<-ggplot(data=summary.myData,aes(x=tx.choice2, y=tx.choice1))+ #check if you need "[order1[[ind1]][1]]" for "x=tx.choice2"
geom_errorbar(aes(ymin=tx.choice1-ci, ymax=tx.choice1+ci), colour="black", width=.5, position=pd)+
geom_point(position=pd, size=3)+
labs(title =tclvalue(intervalplot_title),x=tclvalue(intervalplot_x),y=tclvalue(intervalplot_y))
}
tkinsert(txt,"end",paste("\nintervalplot of", tx.choice1,"By",tx.choice2[1],"\n\n"))
}
else if (numb.select.fac==2) {
##################
xx2<-c(0,0)
for (i in 1:2){
x<-nchar(levels(myData[,tx.choice2[i]]))
xx2[i]<-levels(myData[,tx.choice2[i]])[seq(along=x)[x==max(x)][1]]
}
xx22<-paste(xx2[1],xx2[2], sep=".")
xdist<-strwidth(xx22,"inch")+0.15
par(mai=c(xdist,1.1,.82,.42))
############

if (!is.character(all.equal(ind1/2,as.integer(ind1/2)))=="FALSE"){
ind1<-1
} else{
ind1<-2
}
order1[[ind1]][1]
if(tclvalue(intervalplot_title)=="" & tclvalue(intervalplot_x)=="" & tclvalue(intervalplot_y)==""){
box0<-stripchart(formula(paste(tx.choice1,"~",tx.choice2[order1[[ind1]][1]],"*",tx.choice2[order1[[ind1]][2]])),
data=myData,main=paste("Interval Plot of", tx.choice1,"Against",tx.choice2[order1[[ind1]][1]],"*",tx.choice2[order1[[ind1]][2]],offset=1/2),
method="jitter",jitter=0.1,col="black",
las=1,cex.axis=.8,xlab="",pch=c(18))
} else {
box0<-stripchart(formula(paste(tx.choice1,"~",tx.choice2[order1[[ind1]][1]],"*",tx.choice2[order1[[ind1]][2]])),
data=myData,main=tclvalue(intervalplot_title),
method="jitter",jitter=0.1,col="black",
las=1,cex.axis=.8,xlab=tclvalue(intervalplot_x),ylab=tclvalue(intervalplot_y),pch=c(18))
}
tkinsert(txt,"end",paste("\nintervalplot of", tx.choice1,"Against",tx.choice2[order1[[ind1]][1]],
"*",tx.choice2[order1[[ind1]][2]],"\n\n"))
ind1<<-ind1+1
par(mar=c(5.1, 4.1, 4.1, 2.1))

}
else {
stop(tkmessageBox(message="No more than 2 categorical variables be selected!", icon="error"))
}
} else {
stop(tkmessageBox(message="Please select one numeric variable!", icon="error"))
}}

onhlp <- function() #still needs to be revised
{
tkmessageBox(title="Information",
message="intervalplot - Single Y \n
-=Generate a single intervalplot=-\n
1. Select one variable from the list of Numerical values on the left side;
2. If desired , change the tittle, X and Y labels;
3. Check the 'Stacked?' if you want coincident points stacked. This method only makes sense for very granular data;
4. Press the OK button;\n
-=Generate a single intervalplot by categories=- \n
1. Choose one variable from the list of Numerical values on the left side;
2. Choose maximum of 3 variables from the by categories list on the right side;
3. If desired, change the title, X and Y labels;
5. Check the 'Stacked?' if you want coincident points stacked. This method only makes sense for very granular data;
4. Press the OK button;
5. If you choose more than one category value and keep pressing the OK button, not only the color will be changed but also the category groups order;\n
Note: To learn more about a intervalplot, go to: https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/stripchart.html",
icon="info",
type="ok")
}

####add some bottom on the menu
fr1<-tkframe(tt)
tkpack(fr1,tkbutton(tt,text="Help",command=onhlp),side='left',padx=23)
tkpack(fr1,tkbutton(tt,text="Ok",command=ToDo),side='left',padx=23)
tkpack(fr1,tkbutton(tt,text="Cancel",command=function()tkdestroy(tt)),side='left',padx=23)

}

============================================================================
To be specific: (1) create a new table using

summary.mydata=summarySE(mydata, measurevar=tx.choice1, groupvars=c(tx.choice2[order1[[ind1]][1]],tx.choice2[order1[[ind1]][2]]),conf.interval=tclvalue(intervalplot_confidenceinterval),na.rm=TRUE,.drop=FALSE)

(2) have the ggplot() commands use(reference?) the new summary.mydata table.

(3) destroy(or perhaps another word?) this instance of the summary.mydata table so that it won't get mixed up with another table down the road.

Thank you.

PS: An example of the test data:
0 new messages