How can I load an object into a variable name that I specify from an R data file?

前端 未结 7 2162
你的背包
你的背包 2020-11-28 19:50

When you save a variable in an R data file using save, it is saved under whatever name it had in the session that saved it. When I later go to load it from anot

7条回答
  •  死守一世寂寞
    2020-11-28 20:51

    I'm extending the answer from @ricardo to allow selection of specific variable if the .Rdata file contains multiple variables (as my credits are low to edit an answer). It adds some lines to read user input after listing the variables contained in the .Rdata file.

    loadRData <- function(fileName) {
      #loads an RData file, and returns it
      load(fileName)
      print(ls())
      n <- readline(prompt="Which variable to load? \n")
      get(ls()[as.integer(n)])
    }
    
    select_var <- loadRData('Multiple_variables.Rdata')
    
    
    

提交回复
热议问题