Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:35:43+00:00 2026-06-14T15:35:43+00:00

I have a script (call it Main.R ) that has the following code to

  • 0

I have a script (call it Main.R) that has the following code to find itself when I run it:

frame_files <- lapply(sys.frames(), function(x) x$ofile)
frame_files <- Filter(Negate(is.null), frame_files) 
main.dir <- dirname(dirname(frame_files[[length(frame_files)]]))

This is used to get the directory above its own directory, main.dir, which is used to call other scripts relative to this path.

I’m interested in running this script from a command line, for example

R CMD BATCH Main.R

or

Rscript Main.R

Unfortunately, the commands above do not work when I call the script from the command line.

Is there any code I could put in Main.R or a call option to R or Rscript that I can use instead?

More specifically, the solution would need to work in Windows.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:35:44+00:00Added an answer on June 14, 2026 at 3:35 pm

    Below is a solution that will give you the correct file directory path when the script is run either with source or with Rscript.

    # this is wrapped in a tryCatch. The first expression works when source executes, the
    # second expression works when R CMD does it.
    full.fpath <- tryCatch(normalizePath(parent.frame(2)$ofile),  # works when using source
                   error=function(e) # works when using R CMD
                         normalizePath(unlist(strsplit(commandArgs()[grep('^--file=', commandArgs())], '='))[2]))
    dirname(full.fpath)
    

    The key to this is the function normalizePath. Given a relative or abbreviated path name, normalizePath will return a valid path or raise an error. When running the script from Rscript, if you give normalizePath the base filename of the current script, it’ll return the fullpath, regardless of what your current directory is. It even gets the path right when you supply a relative path to R CMD and there’s a script with the same name in the current directory!

    In the code above, I extract the filename from one of the strings returned by commandArgs. If you take a look at the output of commandArgs, you’ll see that the filename is the 4th argument. The argument is recorded as ‘–file=yourscript.R’, so in the final line above, I split the string on ‘=’ and pull out the file name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script which I can run perfectly if I call it manually
I have a matlab script, lets call it master.m, that loads a file called
I have a php script that gets called via an ajax call. Values are
I have a php script, let's call it main_script.php and if you run it
I have this piece of code that has a problem. so can you help
I have a script, lets call it linkchecker, that loops through about 10.000 URLs,
I have the following html code: (that gets loaded via ajax into a div)
I have a perl script (call it worker) installed on each node/machine (4 total)
I have script which allows to display Bing search results. I can call for
I have this script and need to be able to call the $play variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.