0

I have an R source file that I cannot modify buy I know it contains an instruction that will fail, for example

x <- read.csv("C:/folder/that/does/NOT/exist/file.csv")

Is there any possible way to "externally" modify that instruction or its behaviour with, for example

x <- read.csv("C:/folder/that/exists/file.csv")

before/when sourcing it?

  • The best way is to avoid fixed path names (with drive letters on Windows or leading slash on *nixes) at all and instead use only relative path names. – tpetzoldt Oct 18 '21 at 11:40
  • 1
    It is a bad anti-pattern to write absolute paths in scripts ment to run on other data as well. You can use `patch` and `diff` to create a modified copy of the script. Please use R packages e.g `optparse` for scripts to run them on other datasets as well. – danlooo Oct 18 '21 at 11:49
  • 1
    If you cannot modify maybe take a copy then modify? In any case filenames could be passed as an argument - https://stackoverflow.com/q/14167178/680068 when we call the script. Or see [here package](https://here.r-lib.org/). – zx8754 Oct 18 '21 at 11:50
  • Possible duplicate https://stackoverflow.com/q/36834767/680068 and https://stackoverflow.com/q/14167178/680068 – zx8754 Oct 18 '21 at 11:52
  • 2
    You could read the sourcefile as text, grep for the problematic instruction, rewrite it and then source that version of the file instead.... Seems a bit hackish to me but there might be (hypothetical) situations where this is a viable path... – dario Oct 18 '21 at 12:28

0 Answers0