简体   繁体   中英

file.choose() analogue for objects in R

Is in R an analogue to file.choose() function,working with objects inside R (elements of vectors, objects in environments and etc)? I need just dialog window like in file.choose() function, where i can choose elements of vector, for example

For Example I have dataframe with 3 columns.

length(unique(df$column2))
[1] 3

Then i write

df<- filter(df, column2 %in% MyMagicFunction() )

Then i see window, where i choose right elements =)

I guess you are working in pure R console for that (ie. not RStudio) You can use file.choose for that purpose after having populated some fake files, see:

myfunction <- function(df){
  split_path <- function(path) {
    rev(setdiff(strsplit(path,"/|\\\\")[[1]], ""))
  } 


  tmpdir <- file.path("c:/temp",substitute(df))
  dir.create(tmpdir,showWarnings =FALSE)
  for (ivar in names(df)){
    cat("", file=file.path(tmpdir,ivar))
  }
  selvar <- choose.files(default = paste(tmpdir,"*",sep="/"), caption = "Variable",
                         multi = FALSE)
  varname <- split_path(selvar)[1]
  unlink(file.path(tmpdir,"*"))

  print(varname) # to be replaced by your function exploiting df and varname such as mean(df[,varname])

}

then:

>     doit <- myfunction(iris)
[1] "Sepal.Length"

as said in comment, you have to define your own function call within myfunction .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM