How do I open a script file in RStudio using an R command?

前端 未结 4 1371
慢半拍i
慢半拍i 2021-01-30 16:21

I have a simple script file called test.R. It\'s saved in the working directory. When I go to File > Open > test.R, it opens the file in the edit

相关标签:
4条回答
  • 2021-01-30 16:48

    You are looking for file.edit

    file.edit('test.R')
    

    should open the file in an editor (if you are in RStudio, this should default to RStudio)

    0 讨论(0)
  • 2021-01-30 16:51

    It appears that if you are using RStudio then another option is to use rstudioapi::navigateToFile("test.R")

    0 讨论(0)
  • 2021-01-30 17:08

    Another alternative is the shell.exec function.

    shell.exec("text.R") # This will open the file or URL using the path associated with it

    Also, I think for your use case. This code snippet might be a little bit useful.

    file <- "example.csv"
    sub_dir <- "subdirectory"
    dir.create(sub_dir)
    writeLines("myfile",file.path(sub_dir, file))
    # Works
    shell.exec(file.path(sub_dir, file, fsep = "\\"))
    shell.exec(file.path(sub_dir, file))
    
    0 讨论(0)
  • 2021-01-30 17:13

    I had the same issue that utils::file_edit() within my package opens outside R-studio. I changed it to usethis::edit_file(). With this function the file opens inside R-studio.

    0 讨论(0)
提交回复
热议问题