source("codename.R") RRS feed

  • Question

  • Hi,

    I saved an R code as codename.R file and then tried to run the command source("codename.R") on R console. It cannot find the file. When I run ls() it also cannot see this file. But when I run getwd() it opens the directory where this R file is saved and in the directory I can see this R file. Can someone please suggest why ls() is not able to see this file which is saved in the R workspace? The other question is how can I locate the workspace and the working directory? I suspect this issue is because of confusion between workspace and working directory.



    Friday, October 27, 2017 6:49 AM

All replies

  • Your default R working directory is the directory returned by the R function getwd(). That is also the directory referenced by default by other R commands, such as source().

    I think you are confusing the R function ls() with the OS command ls().

    The R help file indicates that ls() does not list files on disk, but rather it lists objects that exist in the current R environment.

    From help(ls):

    "ls() and objects() return a vector of character strings giving the names of the objects in the specified environment.

    When invoked with no arguments at the top level prompt, ls() shows what data and functions a user has defined."

    Stephen Weller

    Microsoft R Product Team


    Monday, October 30, 2017 4:08 PM
  • You can list files on disk from R using the function list.files().
    Monday, October 30, 2017 4:09 PM