[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  sys.source: Parse and Evaluate Expressions from a File

##  Description

Parses expressions in the given file, and then successively evaluates them in
the specified environment.

##  Usage

    
    
    sys.source(file, envir = baseenv(), chdir = FALSE,
               keep.source = getOption("keep.source.pkgs"),
               keep.parse.data = getOption("keep.parse.data.pkgs"),
               toplevel.env = as.environment(envir))

##  Arguments

file

a character string naming the file to be read from

envir

an  R  object specifying the environment in which the expressions are to be
evaluated. May also be a list or an integer. The default value ` NULL `
corresponds to evaluation in the base environment. This is probably not what
you want; you should typically supply an explicit ` envir ` argument.

chdir

logical; if ` TRUE ` , the  R  working directory is changed to the directory
containing ` file ` for evaluating.

keep.source

logical. If ` TRUE ` , functions keep their source including comments, see ` [
options ](/link/options?package=base&version=3.6.2) (keep.source = *) ` for
more details.

keep.parse.data

logical. If ` TRUE ` and ` keep.source ` is also ` TRUE ` , functions keep
parse data with their source, see ` [ options
](/link/options?package=base&version=3.6.2) (keep.parse.data = *) ` for more
details.

toplevel.env

an  R  environment to be used as top level while evaluating the expressions.
This argument is useful for frameworks running package tests; the default
should be used in other cases

##  Details

For large files, ` keep.source = FALSE ` may save quite a bit of memory.
Disabling only parse data via ` keep.parse.data = FALSE ` can already save a
lot.  In order for the code being evaluated to use the correct environment
(for example, in global assignments), source code in packages should call ` [
topenv ](/link/topenv?package=base&version=3.6.2) () ` , which will return the
namespace, if any, the environment set up by ` sys.source ` , or the global
environment if a saved image is being used.

##  See Also

` [ source ](/link/source?package=base&version=3.6.2) ` , and ` [ library
](/link/library?package=base&version=3.6.2) ` which uses ` sys.source ` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fsys.source&utm_source=r-docs&utm_medium=docs&utm_term=sys.source&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    ## a simple way to put some objects in an environment
    ## high on the search path
    tmp <- tempfile()
    writeLines("aaa <- pi", tmp)
    env <- attach(NULL, name = "myenv")
    sys.source(tmp, env)
    unlink(tmp)
    search()
    aaa
    detach("myenv")
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fsys.source&utm_source=r-docs&utm_medium=docs&utm_term=sys.source&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)