I am trying to create a GUI (for the first time, so this a very nooby question) to do some a series of simple tasks:
- read all the csv files in a folder
- apply a low-pass filter (for example)
- export the results in a csv file containing the original data and the filtered
I did the coding for all the process in R, but I am having trouble translating it into a GUI. I am trying gWindget2 and following the "Programming Graphical User Interfaces in R" was able to read the files, but I don't really understand how to use the interface input to do the analysis and then export.
This is the R code for the analysis:
library(dplyr)
library(tools)
library(tidyr)
library(signal)
datafiles <- list.files("FilesData", pattern="*.csv", full.names=TRUE)
df = tibble(File = datafiles) %>%
mutate(Data = lapply(File, read.csv), Name=file_path_sans_ext(basename(File))) %>%
unnest(Data) %>%
select(-File)
df$date <- as.POSIXct(df$date)
order <- 1
sensitivity <- 0.2
bf <- butter(order,sensitivity)
for(var in unique(df$Name)){
dfvar <- subset(df, df$Name==var)
dfvar$LPmin <- filtfilt(bf,(dfvar$min))
dfvar$LPmax <- filtfilt(bf,(dfvar$max))
if(exists("df2")){
df2 <- rbind.data.frame(df2, dfvar)
}
if(!exists("df2")){
df2 <- data.frame(dfvar)
}
rm(var, dfvar)
}
for(var in unique(df2$Name)){
dfvar <- subset(df2, df2$Name==var)
write.csv(dfvar, paste("./Result/",var,"Filtered.csv",sep=""), row.names = FALSE)
rm(dfvar, var)
}
GUI Code (what I have tried so far...)
require(gWidgets2)
options(guiToolkit="RGtk2")
window <- gwindow("File search",visible=FALSE)
paned <- gpanedgroup(cont=window)
group <- ggroup(cont=paned,horizontal=FALSE)
glabel("Search for (filename):",cont=group,anchor=c(1,0))
txt_pattern <- gedit("",initial.msg="*.csv",cont=group)
glabel("Search in:",cont=group,anchor=c(1 , 0 ))
start_dir <- gfilebrowse(text="Select a directory ...",quote=FALSE,type="selectdir",cont=group)
search_button <- gbutton("Search",cont=group,handler=function(h, ...){
pattern <- glob2rx(svalue(h$action$txt) )
file_names <- dir(svalue(h$action$dir),
pattern,recursive=TRUE)
if(length(file_names))
svalue(h$action$results)<- file_names
else
galert("No matching files found",parent=w)},
action=list(txt=txt_pattern,dir=start_dir,results=search_results)
)
addSpring(group)
frame <- gframe("Output:",cont=paned,horizontal=FALSE)
search_results <- gtext("",cont=frame,expand=TRUE)
size(search_results)<- c(350 , 200)
addHandlerChanged(search_button,handler=function(h, ...){
pattern <- glob2rx(svalue(txt_pattern))
file_names <- dir(svalue(start_dir),pattern,recursive=TRUE)
if(length(file_names))
svalue(search_results)<- file_names
else
galert("No matching files found",parent=window)
})
visible(window)<- TRUE
With the code above I am able to load the files from the folder (searching for "*.csv" files) in the Output panel, but I don't know how to use that input to do the analysis with the R code above.
I am sorry if the question is to simple, but I will appreciate any help!