'googledrive::drive_mv gives error "Parent specified via 'path' is invalid: x Does not exist"

This is a weird one and I am hoping someone can figure it out. I have written a function that uses googlesheets4 and googledrive. One thing I'm trying to do is move a googledrive document (spreadsheet) from the base folder to a specified folder. I had this working perfectly yesterday so I don't know what happened as it just didn't when I came in this morning. The weird thing is that if I step through the function, it works fine. It's just when I run the function all at once that I get the error.

I am using a folder ID instead of a name and using drive_find to get the correct folder ID. I am also using a sheet ID instead of a name. The folder already exists and like I said, it was working yesterday.

outFolder <- 'exact_outFolder_name_without_slashes'

createGoogleSheets <- function(
  outFolder
){

  folder_id <- googledrive::drive_find(n_max = 10, pattern = outFolder)$id

  data <- data.frame(Name = c("Sally", "Sue"), Data = c("data1", "data2"))

  sheet_id <- NA
  nameDate <- NA
  tempData <- data.frame()

  for (i in 1:nrow(data)){

    nameDate <- data[i, "Name"]

    tempData <- data[i, ]

    googlesheets4::gs4_create(name = nameDate, sheets = list(sheet1 = tempData)

    sheet_id <- googledrive::drive_find(type = "spreadsheet", n_max = 10, pattern = nameDate)$id

    googledrive::drive_mv(file = as_id(sheet_id), path = as_id(folder_id))

  } end 'for'

} end 'function'

I don't think this will be a reproducible example. The offending code is within the for loop that is within the function and it works fine when I run through it step by step. folder_id is defined within the function but outside of the for loop. sheet_id is within the for loop. When I move folder_id into the for loop, it still doesn't work although I don't know why it would change anything. These are just the things I have tried. I do have the proper authorization for google drive and googlesheets4 by using:

googledrive::drive_auth()
googlesheets4::gs4_auth(token = drive_token())

<error/rlang_error> Error in as_parent(): ! Parent specified via path is invalid: x Does not exist.

Backtrace:

  1. global createGoogleSheets(inputFile, outPath, addNames)
  2. googledrive::drive_mv(file = as_id(sheet_id), path = as_id(folder_id))
  3. googledrive:::as_parent(path) Run rlang::last_trace() to see the full context.

Backtrace: x

  1. -global createGoogleSheets(inputFile, outPath, addNames)
  2. -googledrive::drive_mv(file = as_id(sheet_id), path = as_id(folder_id))
  3. \-googledrive:::as_parent(path)
    
  4.   \-googledrive:::drive_abort(c(invalid_parent, x = "Does not exist."))
    
  5.     \-cli::cli_abort(message = message, ..., .envir = .envir)
    
  6.       \-rlang::abort(message, ..., call = call, use_cli_format = TRUE)
    

I have tried changing the folder_id to the exact path of my google drive W:/My Drive... and got the same error. I should mention I have also tried deleting the folder and re-creating it fresh.

Anybody have any ideas?

Thank you in advance for your help!



Solution 1:[1]

I can't comment because I don't have the reputation yet, but I believe you're missing a parenthesis in your for-loop.

You need that SECOND parenthesis below:


    for (i in 1:nrow(tempData) ) {
    ...
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick Camarda