1

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:

  1. read all the csv files in a folder
  2. apply a low-pass filter (for example)
  3. 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!

Community
  • 1
  • 1
NeReiS
  • 77
  • 1
  • 8
  • It seems you want the user to select a directory, and then from there the rest is programatically determined. A simple GUI might look like this: – jverzani Oct 09 '19 at 18:52

1 Answers1

0

It seems you want the user to select a directory, and then from there the rest is programmatically determined. A simple GUI might look like this:


w <- gwindow()
g <- ggroup(horizontal=FALSE, cont=w)
fb <- gfilebrowse(quote=FALSE,type="selectdir", cont=g)
output <- gtext(cont=g, expand=TRUE)

addHandlerChanged(fb, handler=function(h, ...) {
 d = svalue(h$obj)
 file_names = dir(d, recursive=TRUE)
 print(file_names)
 print(".... do more with file_names ...")
 svalue(output) <- paste(file_names, sep="\n", collapse=" ")
})
jverzani
  • 5,600
  • 2
  • 21
  • 17