0

I know that file.create() can generate an empty file, but my request is to generate an R script filled with specific text, such as

#' This is specific text 

#' So is this

foo <- function(x){
  bar(x)
}

How can I save this text in a way that it populates a file I generate via file.create() or through some other function?

Joe
  • 2,735
  • 1
  • 15
  • 34

1 Answers1

1

There is nothing special about R script files. They are simply text files and you can use writeLines to write them to a file:

script <- "
foo <- function(x){
  bar(x)
}
"
writeLines(script, "script.R")

Here is a simple case to test:

script <- "
print('hello world!')
"
writeLines(script, "script.R")
source("script.R")
#> [1] "hello world!"
JBGruber
  • 10,045
  • 1
  • 19
  • 41