0

I need to save a number of tables, in the same way, using the function.

I have tried the below.

FYI: group_name is a character object of length 1 (should just print "group_one", for example).

# Save table
save_table <- function(x) {
  table_name <- as.character(x)
  fwrite(
    x, 
    file = 
      paste0(
        "tables/", 
        group_name, 
        "/",
        table_name,
        ".csv"
      )
  )
}

I get the following error message:

 Error in fwrite(x, file = paste0("tables/", group_name, "/",  : 
  is.character(file) && length(file) == 1L && !is.na(file) is not TRUE
M--
  • 25,431
  • 8
  • 61
  • 93
Tom
  • 279
  • 1
  • 12
  • What exactly are you passing in to this function? It's easier to help you if you 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 Mar 24 '20 at 15:24

1 Answers1

0

Found a solution by replacing the 'as.character' with 'deparse(substitute(x))'.

save_table <- function(x) {
  table_name <- deparse(substitute(x))
  fwrite(
    x, 
    file = 
      paste0(
        "tables/", 
        group_name, 
        "/",
        table_name,
        ".csv"
      )
  )
}
Tom
  • 279
  • 1
  • 12