0

I saw a question similar to this previously, but I didn't fully understand the answer. Here is my original function:

fxn_import_data <- function(file_path, filen)
    {work <-setwd(file_path)
    yelp <- stream_in(file(filen)) #creating a data fram called yelp using stream_in function}
    #head(yelp, 10)
    yelp_flat<- flatten(yelp) #helps turn JSON files with multiple df's into tabular format
    #str(yelp_flat) #displays abbreviated content (not needed for this data set)
    scoop <- as_data_frame(yelp_flat)
}

The purpose of this function is to open the file source of my data and unpack it, which it does. I need this data to run my next function, which is shown below:

fxn_create_meta<- function(fxn_import_data, scoop) {return(scoop)
fxn_import_data <- function(file_path, filen)
  scoop <- as_data_frame(yelp_flat)
  scoop$homePlayers <- NULL #eliminates homePlayers column
  scoop$awayPlayers <- NULL
  pop <- scoop %>% separate(ball, c("ball_x", "ball_y", "ball_z"), sep = ",") 
  #separates one column into multiple 
  l <- pop %>% separate(ball_x, c("throw","ball_x"), sep = "c")
  gh <- l %>% separate(ball_z, c("ball_z", "tra"), sep = "\\)")
  Meta <- gh %>% separate(ball_x, c("kol", "ball_x"), sep = "\\(")
  Meta$tra <- NULL
  Meta$kol <- NULL
  Meta$throw <- NULL
  return(Meta)
}

However, when I try to run the second function, it tells me that the object "scoop" is undefined. How do I fix this?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Paul Ibrahim
  • 413
  • 1
  • 3
  • 14
  • Is this really what your function looks like? It appears that you just have some unmatched brace problems. – MrFlick Jul 02 '18 at 15:30
  • 1
    I am not sure what you mean by unmatched brace problems. This is how my function looks with the exception that I define "file_path" and "filen" on my actual program – Paul Ibrahim Jul 02 '18 at 15:33
  • 2
    The first line of your second code block has an opening brace then a `return(scoop)`. Nothing after that `return()` is going to run since that kicks you out of the function. So basically everything after that is basically commented out (not run). The `fxn_import_data` is seems to be missing a brace before the code block. When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 02 '18 at 15:37
  • 1
    I see. Essentially I have a function streaming in a file containing the data (which I cannot upload), and I am trying to use another function to filter out unwanted data and create a table with the wanted data. Could you help me or refer me to how I can run the initial function within the second? – Paul Ibrahim Jul 02 '18 at 16:10
  • You need to assign the value of the result of your function. You can call it whatever you want. `scoop <- fxn_import_data(...)`, `my_scoop <- fxn_import_data(...)`, `larry <- fxn_import_data(...)`. But the assignment and the name happens *outside* the function, not inside. Just like when you do `pop <- scoop %>% ...` you pick the name `pop` and assign the output of `separate` to the name you picked. – Gregor Thomas Jul 02 '18 at 16:21
  • What's the point of piping if the lines aren't piped together? I would expect either `Meta <- scoop %>% separate(ball, ...) %>% separate(ball_x, ...) %>% separate(ball_z, ...)` or no pipes. Using one pipe per line seems like the worst of both worlds - you still have to come up with temporary variable names and retype them, `pop <- scoop %>% ...; l <- pop %>% ...; gh <- pop %>% ...`. The main point of the pipe is that you don't have to keep using those temporary variable names. – Gregor Thomas Jul 02 '18 at 16:24

1 Answers1

2

You can write the two functions, with the second function running the first function in it:

fxn_import_data <- function(file_path, filen)
    {work <-setwd(file_path)
    yelp <- stream_in(file(filen))
    yelp_flat<- flatten(yelp)
    scoop <- as_data_frame(yelp_flat)
}


fxn_import_data <- function(file_path, filen){
  scoop <- fxn_import_data(file_path, filen)#RUN THE FIRST FUNCTION AND SAVE THE RESULT AS scoop
  scoop$homePlayers <- NULL
  scoop$awayPlayers <- NULL
  pop <- scoop %>% separate(ball, c("ball_x", "ball_y", "ball_z"), sep = ",")       l <- pop %>% separate(ball_x, c("throw","ball_x"), sep = "c")
  gh <- l %>% separate(ball_z, c("ball_z", "tra"), sep = "\\)")
  Meta <- gh %>% separate(ball_x, c("kol", "ball_x"), sep = "\\(")
  Meta$tra <- NULL
  Meta$kol <- NULL
  Meta$throw <- NULL
  return(Meta)
}

please read the comment

Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • 1
    Thank you so much! In this, you are defining "scoop" as the function, correct? Scoop is defined in the initial question, and that is the operation I wish to execute in the second function. Will this still do the same? – Paul Ibrahim Jul 02 '18 at 16:19
  • @Gregor I just copy pasted OP's code and rectified the mistake. I did not change the code so much, just to make OP understand what s/he needed to do – Onyambu Jul 02 '18 at 16:20
  • 1
    @PaulIbrahim scoop is not defined as a function but rather as an output from the first function. – Onyambu Jul 02 '18 at 16:21