'Initialize the environment for installing R packages.  The class is called during the initial
download stage when installing packages, so the value of initial_download will generally be True.
However, the parameter value allows this class to also be used in the second stage of the installation,
although it may never be necessary.  If initial_download is True, the recipe steps will be filtered
and returned and the installation directory (i.e., dir) will be defined and returned.  If we\'re not
in the initial download stage, these actions will not occur, and None values will be returned for them.'

def execute_step(self, tool_dependency, package_name, actions, action_dict, filtered_actions, env_file_builder, install_environment, work_dir, current_dir=None, initial_download=False):

    dir = None
    if initial_download:
        filtered_actions = actions[1:]
    env_shell_file_paths = action_dict.get('env_shell_file_paths', None)
    if (env_shell_file_paths is None):
        error_message = 'Missing    R    environment.    Please    check    your    specified    R    installation    exists.'
        log.error(error_message)
        status = self.app.install_model.ToolDependency.installation_status.ERROR
        tool_dependency = tool_dependency_util.set_tool_dependency_attributes(self.app, tool_dependency, status=status, error_message=error_message)
        return (tool_dependency, [], None)
    else:
        install_environment.add_env_shell_file_paths(env_shell_file_paths)
    log.debug(('Handling    setup_r_environment    for    tool    dependency    %s    with    install_environment.env_shell_file_paths:\n%s' % (str(tool_dependency.name), str(install_environment.env_shell_file_paths))))
    tarball_names = []
    for r_package_dict in action_dict['r_packages']:
        url = r_package_dict['package']
        filename = url.split('/')[(-1)]
        tarball_names.append(filename)
        checksums = r_package_dict.get('checksums', {})
        self.url_download(work_dir, filename, url, extract=False, checksums=checksums)
    dir = os.path.curdir
    current_dir = os.path.abspath(os.path.join(work_dir, dir))
    with lcd(current_dir):
        with settings(warn_only=True):
            for tarball_name in tarball_names:
                cmd = ('PATH=$PATH:$R_HOME/bin;    export    PATH;    R_LIBS=$INSTALL_DIR:$R_LIBS;    export    R_LIBS;\n                                                                                                Rscript    -e    "tryCatch(    {    install.packages(c(\'%s\'),    lib    =    \'$INSTALL_DIR\',    repos    =    NULL,    dependencies    =    FALSE)    },    error    =    function(e)    {    print(e);    quit(status    =    1)    },    warning    =    function(w)    {    if    (    grepl(\'had    non-zero    exit    status|is    not    writable|installation    of    one    of    more    packages    failed\',    as.character(w))    )    {    print(w);    quit(status    =    1)    }    }    )"' % str(tarball_name))
                cmd = install_environment.build_command(basic_util.evaluate_template(cmd, install_environment))
                return_code = install_environment.handle_command(tool_dependency=tool_dependency, cmd=cmd, return_output=False, job_name=package_name)
                if return_code:
                    if initial_download:
                        return (tool_dependency, filtered_actions, dir)
                    return (tool_dependency, None, None)
            env_file_builder.handle_action_shell_file_paths(action_dict)
            env_file_builder.append_line(name='R_LIBS', action='prepend_to', value=install_environment.install_dir)
            return_code = env_file_builder.return_code
            if return_code:
                if initial_download:
                    return (tool_dependency, filtered_actions, dir)
                return (tool_dependency, None, None)
    if initial_download:
        return (tool_dependency, filtered_actions, dir)
    return (tool_dependency, None, None)
