2

Thank you in advance for your help!

I made a function in R that returns a dataframe, say DF_Name which have 4 columns, say harder,better,faster,stronger, and I just want to store this single dataframe in the Global Environment.

Here´s an extremely simplified version of the function:

daft_funct<-function()
{
  DF_Name <- read.csv("D:/SomeFolder/DF_Data.csv",stringsAsFactors=FALSE)
  return(DF_Name) # I want to fix this line here
}

And these are my unsuccessfully attemps to store it on .GlobalEnv:

  1. Without any knowledge:

    return(DF_Name) # Just shows on screen
    
  2. Attempt to see the whole dataframe, shows but doesn´t store:

    View(DF_Name) # Shows on RStudio panel
    
  3. Looking for help in StackOverflow and found this question. However it stores every single column of the data frame as separated values:

    list2env(DF_name,.GlobalEnv)
    # Check if worked
    >ls()
    "better"    "faster"    "harder"    "stronger"
    
  4. Looking again for help in StackOverflow and found this other question. Which stores every single column of DF_Name again, but this time as separated data frames:

    list2env(lapply(DF_Name, as.data.frame),.GlobalEnv)
    # Check if worked
    >ls()
    "better"    "faster"    "harder"    "stronger"
    

Trying to merge the dataframes from 4th attempt into a single one inside the function leads to the main problem again.

P.S.: I have intentionally omitted daft_funct from ls()output.

Community
  • 1
  • 1
Dante
  • 154
  • 1
  • 8
  • 4
    The proper way to do it is not to put it in the global environment, but to have the user assign it. The user should just do `foo <- daft_funct()` and then they will have the data frame as an object named `foo` in the global environment. – Gregor Thomas Aug 04 '16 at 17:26
  • As Gregor mentioned assigning to the global environment from within a function is not good practice but if you must then this will assign data frame `DF_name` to a data frame named `X` in the global environment. `assign("X", DF_name, .GlobalEnv)` . Note the dot in `.GlobalEnv` . – G. Grothendieck Aug 04 '16 at 17:30
  • If you must bypass this, it is possible using `assign` or `<<-`, but this is frowned upon - generally R users expect functions will *not* modify the global environment internally. Your function may overwrite an existing object. If the user attempts to use your function like any normal R function (`foo <- daft_funct()`) they may be surprised and confused to have `foo` be useless. If the user doesn't know to look for an object with the name `DF_NAME` in their global environment, they will just think your function is broken. – Gregor Thomas Aug 04 '16 at 17:30
  • Possible duplicate: [Assign multiple objects to `.GlobalEnv` from within a function](http://stackoverflow.com/q/9726705/903061). The solution for multiple objects can clearly be simplified to a single object. – Gregor Thomas Aug 04 '16 at 17:38
  • How didn´t I figure it could be that simple. Thank you @Gregor, there´s nothing wrong on `da_funk<-daft_funct()`. I'll be on shame forever. – Dante Aug 04 '16 at 17:39

0 Answers0