在 win7 上安装 rpy2 时遇到问题(R 2.12,Python 2.5)

发布于 2024-10-16 09:56:06 字数 14387 浏览 4 评论 0原文

我是 Python(以及一般编程)的新手,我来自金融背景,所以请耐心等待。我刚刚开始使用 Python(Enthought 的 Pylab/Scipy/Numpy)和 R 进行统计分析。我正在尝试将 rpy2 安装到 Python 中以集成 R,但出现错误:

尝试猜测 R 的 HOME,但没有找到 R PATH 中的命令。

我不确定这意味着什么。我的 R.exe 的路径是“C:\Program Files\R\R-2.12.1\bin”(如果有用的话)。任何帮助将不胜感激!

这是setup.py的代码

import os, os.path, sys, shutil, re, itertools
from distutils.command.build_ext import build_ext as _build_ext
from distutils.command.build import build as _build

from distutils.core import setup
from distutils.core import Extension


pack_name = 'rpy2'
pack_version = __import__('rpy').__version__


class build(_build):
    user_options = _build.user_options + \
        [
        #('r-autoconfig', None,
        # "guess all configuration paths from " +\
        #     "the R executable found in the PATH " +\
        #     "(this overrides r-home)"),
        ('r-home=', None, 
         "full path for the R home to compile against " +\
             "(see r-autoconfig for an automatic configuration)"),
        ('r-home-lib=', None,
         "full path for the R shared lib/ directory " +\
             "(<r-home>/lib otherwise)"),
        ('r-home-modules=', None,
         "full path for the R shared modules/ directory " +\
             "(<r-home>/modules otherwise)") 
        ]
    boolean_options = _build.boolean_options #+ \
        #['r-autoconfig', ]


    def initialize_options(self):
        _build.initialize_options(self)
        self.r_autoconfig = None
        self.r_home = None
        self.r_home_lib = None
        self.r_home_modules = None

class build_ext(_build_ext):
    """
    -DRPY_VERBOSE
    -DRPY_DEBUG_PRESERV
    -DRPY_DEBUG_PROMISE    : evaluation of promises
    -DRPY_DEBUG_OBJECTINIT : initialization of PySexpObject
    -DRPY_DEBUG_CONSOLE    : console I/O
    -DRPY_DEBUG_COBJECT    : SexpObject passed as a CObject
    -DRPY_DEBUG_GRDEV
    """
    user_options = _build_ext.user_options + \
        [
        #('r-autoconfig', None,
        #  "guess all configuration paths from " +\
        #      "the R executable found in the PATH " +\
        #      "(this overrides r-home)"),
        ('r-home=', None, 
         "full path for the R home to compile against " +\
             "(see r-autoconfig for an automatic configuration)"),
        ('r-home-lib=', None,
         "full path for the R shared lib/ directory" +\
             "(<r-home>/lib otherwise)"),
        ('r-home-modules=', None,
         "full path for the R shared modules/ directory" +\
             "(<r-home>/modules otherwise)")]

    boolean_options = _build_ext.boolean_options #+ \
        #['r-autoconfig', ]

    def initialize_options(self):
        _build_ext.initialize_options(self)
        self.r_autoconfig = None
        self.r_home = None
        self.r_home_lib = None
        self.r_home_modules = None

    def finalize_options(self):
        self.set_undefined_options('build',
                                   #('r_autoconfig', 'r_autoconfig'),
                                   ('r_home', 'r_home'))
        _build_ext.finalize_options(self) 
        if self.r_home is None:
            self.r_home = os.popen("R RHOME").readlines()
            if len(self.r_home) == 0:
                raise SystemExit("Error: Tried to guess R's HOME but no R command in the PATH.")

    #Twist if 'R RHOME' spits out a warning
            if self.r_home[0].startswith("WARNING"):
                self.r_home = self.r_home[1]
            else:
                self.r_home = self.r_home[0]
            #self.r_home = [self.r_home, ]

        if self.r_home is None:
            raise SystemExit("Error: --r-home not specified.")
        else:
            self.r_home = self.r_home.split(os.pathsep)

        rversions = []
        for r_home in self.r_home:
            r_home = r_home.strip()
        rversion = get_rversion(r_home)
        if cmp_version(rversion[:2], [2, 8]) == -1:
            raise SystemExit("Error: R >= 2.8 required.")
        rversions.append(rversion)

        config = RConfig()
        for about in ('--ldflags', '--cppflags', 
                      'LAPACK_LIBS', 'BLAS_LIBS'):
            config += get_rconfig(r_home, about)

        print(config.__repr__())

        self.include_dirs.extend(config._include_dirs)
        self.libraries.extend(config._libraries)
        self.library_dirs.extend(config._library_dirs)

        if self.r_home_modules is None:
            self.library_dirs.extend([os.path.join(r_home, 'modules'), ])
        else:
            self.library_dirs.extends([self.r_home_modules, ])

        #for e in self.extensions:
        #    self.extra_link_args.extra_link_args(config.extra_link_args)
        #    e.extra_compile_args.extend(extra_compile_args)

    def run(self):
        _build_ext.run(self)



def get_rversion(r_home):
    r_exec = os.path.join(r_home, 'bin', 'R')
    # Twist if Win32
    if sys.platform == "win32":
        rp = os.popen3('"'+r_exec+'" --version')[2]
    else:
        rp = os.popen('"'+r_exec+'" --version')
    rversion = rp.readline()
    #Twist if 'R RHOME' spits out a warning
    if rversion.startswith("WARNING"):
        rversion = rp.readline()
    m = re.match('^R version ([^ ]+) .+$', rversion)
    rversion = m.groups()[0]
    rversion = rversion.split('.')
    rversion[0] = int(rversion[0])
    rversion[1] = int(rversion[1])
    return rversion

def cmp_version(x, y):
    if (x[0] < y[0]):
        return -1
    if (x[0] > y[0]):
        return 1
    if (x[0] == y[0]):
        if len(x) == 1 or len(y) == 1:
            return 0
        return cmp_version(x[1:], y[1:])

class RConfig(object):
    _include_dirs = None
    _libraries = None
    _library_dirs = None 
    _extra_link_args = None
    _frameworks = None
    _framework_dirs = None
    def __init__(self,
                 include_dirs = tuple(), libraries = tuple(),
                 library_dirs = tuple(), extra_link_args = tuple(),
                 frameworks = tuple(),
                 framework_dirs = tuple()):
        for k in ('include_dirs', 'libraries', 
                  'library_dirs', 'extra_link_args'):
            v = locals()[k]
            if not isinstance(v, tuple):
                if isinstance(v, str):
                    v = [v, ]
            v = tuple(set(v))
            self.__dict__['_'+k] = v
        # frameworks are specific to OSX
        for k in ('framework_dirs', 'frameworks'):
            v = locals()[k]
            if not isinstance(v, tuple):
                if isinstance(v, str):
                    v = [v, ]
            v = tuple(set(v))
            self.__dict__['_'+k] = v
            self.__dict__['_'+'extra_link_args'] = tuple(set(v + self.__dict__['_'+'extra_link_args']))


    def __repr__(self):
        s = 'Configuration for R as a library:' + os.linesep
        s += os.linesep.join(
            ['  ' + x + ': ' + self.__dict__['_'+x].__repr__() \
                 for x in ('include_dirs', 'libraries',
                           'library_dirs', 'extra_link_args')])
        s += os.linesep + ' # OSX-specific (included in extra_link_args)' + os.linesep 
        s += os.linesep.join(
            ['  ' + x + ': ' + self.__dict__['_'+x].__repr__() \
                 for x in ('framework_dirs', 'frameworks')]
            )

        return s

    def __add__(self, config):
        assert isinstance(config, RConfig)
        res = RConfig(include_dirs = self._include_dirs + \
                          config._include_dirs,
                      libraries = self._libraries + config._libraries,
                      library_dirs = self._library_dirs + \
                          config._library_dirs,
                      extra_link_args = self._extra_link_args + \
                          config._extra_link_args)
        return res
    @staticmethod
    def from_string(string, allow_empty = False):
        possible_patterns = ('^-L(?P<library_dirs>[^ ]+)$',
                             '^-l(?P<libraries>[^ ]+)$',
                             '^-I(?P<include_dirs>[^ ]+)$',
                             '^(?P<framework_dirs>-F[^ ]+?)$',
                             '^(?P<frameworks>-framework [^ ]+)$')
        pp = [re.compile(x) for x in possible_patterns]
        # sanity check of what is returned into rconfig
        rconfig_m = None        
        span = (0, 0)
        rc = RConfig()
        for substring in re.split('(?<!-framework) ', string):
            ok = False
            for pattern in pp:
                rconfig_m = pattern.match(substring)
                if rconfig_m is not None:
                    rc += RConfig(**rconfig_m.groupdict())
                    span = rconfig_m.span()
                    ok = True
                    break
                elif rconfig_m is None:
                    if allow_empty and (rconfig == ''):
                        print(cmd + '\nreturned an empty string.\n')
                        rc += RConfig()
                        ok = True
                        break
                    else:
                        # if the configuration points to an existing library, 
                        # use it
                        if os.path.exists(string):
                            rc += RConfig(library = substring)
                            ok = True
                            break
            if not ok:
                raise ValueError('Invalid substring\n' + substring 
                                 + '\nin string\n' + string)
        return rc

def get_rconfig(r_home, about, allow_empty = False):
    r_exec = os.path.join(r_home, 'bin', 'R')
    cmd = '"'+r_exec+'" CMD config '+about
    rp = os.popen(cmd)
    rconfig = rp.readline()
    #Twist if 'R RHOME' spits out a warning
    if rconfig.startswith("WARNING"):
        rconfig = rp.readline()
    rconfig = rconfig.strip()
    rc = RConfig.from_string(rconfig)
    return rc


def getRinterface_ext():
    #r_libs = [os.path.join(RHOME, 'lib'), os.path.join(RHOME, 'modules')]
    r_libs = []
    extra_link_args = []

    #FIXME: crude way (will break in many cases)
    #check how to get how to have a configure step
    define_macros = []

    if sys.platform == 'win32':
        define_macros.append(('Win32', 1))
    else:
        define_macros.append(('R_INTERFACE_PTRS', 1))
        define_macros.append(('HAVE_POSIX_SIGJMP', 1))

    define_macros.append(('CSTACK_DEFNS', 1))
    define_macros.append(('RIF_HAS_RSIGHAND', 1))

    include_dirs = []

    rinterface_ext = Extension(
            name = pack_name + '.rinterface.rinterface',
            sources = [ \
            #os.path.join('rpy', 'rinterface', 'embeddedr.c'), 
            #os.path.join('rpy', 'rinterface', 'r_utils.c'),
            #os.path.join('rpy', 'rinterface', 'buffer.c'),
            #os.path.join('rpy', 'rinterface', 'sequence.c'),
            #os.path.join('rpy', 'rinterface', 'sexp.c'),
            os.path.join('rpy', 'rinterface', 'rinterface.c')
                       ],
            depends = [os.path.join('rpy', 'rinterface', 'embeddedr.h'), 
                       os.path.join('rpy', 'rinterface', 'r_utils.h'),
                       os.path.join('rpy', 'rinterface', 'buffer.h'),
                       os.path.join('rpy', 'rinterface', 'sequence.h'),
                       os.path.join('rpy', 'rinterface', 'sexp.h'),
                       os.path.join('rpy', 'rinterface', 'rpy_rinterface.h')
                       ],
            include_dirs = [os.path.join('rpy', 'rinterface'),] + include_dirs,
            libraries = ['R', ],
            library_dirs = r_libs,
            define_macros = define_macros,
            runtime_library_dirs = r_libs,
            #extra_compile_args=['-O0', '-g'],
            #extra_link_args = extra_link_args
            )

    rpy_device_ext = Extension(
        pack_name + '.rinterface.rpy_device',
            [
            os.path.join('rpy', 'rinterface', 'rpy_device.c'),
             ],
            include_dirs = include_dirs + 
                            [os.path.join('rpy', 'rinterface'), ],
            libraries = ['R', ],
            library_dirs = r_libs,
            define_macros = define_macros,
            runtime_library_dirs = r_libs,
            #extra_compile_args=['-O0', '-g'],
            extra_link_args = extra_link_args
        )

    return [rinterface_ext, rpy_device_ext]


rinterface_exts = []
ri_ext = getRinterface_ext()
rinterface_exts.append(ri_ext)

pack_dir = {pack_name: 'rpy'}

import distutils.command.install
for scheme in distutils.command.install.INSTALL_SCHEMES.values():
    scheme['data'] = scheme['purelib']

setup(
    #install_requires=['distribute'],
    cmdclass = {'build': build,
                'build_ext': build_ext},
    name = pack_name,
    version = pack_version,
    description = "Python interface to the R language",
    url = "http://rpy.sourceforge.net",
    license = "AGPLv3.0 (except rpy2.rinterface: LGPL)",
    author = "Laurent Gautier",
    author_email = "[email protected]",
    ext_modules = rinterface_exts[0],
    package_dir = pack_dir,
    packages = [pack_name,
                pack_name + '.rlike',
                pack_name + '.rlike.tests',
                pack_name + '.rinterface',
                pack_name + '.rinterface.tests',
                pack_name + '.robjects',
                pack_name + '.robjects.tests',
                pack_name + '.robjects.lib',
                ],
    classifiers = ['Programming Language :: Python',
                   'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
                   'License :: OSI Approved :: GNU Affero General Public License v3',
                   'Intended Audience :: Developers',
                   'Intended Audience :: Science/Research',
                   'Development Status :: 5 - Production/Stable'
                   ],
    data_files = [(os.path.join('rpy2', 'images'), 
                   [os.path.join('doc', 'source', 'rpy2_logo.png')])]

    #[pack_name + '.rinterface_' + x for x in rinterface_rversions] + \
        #[pack_name + '.rinterface_' + x + '.tests' for x in rinterface_rversions]
    )

I'm brand new to Python (and programming in general) I come from a finance background, so please bear with me. I just started using Python (Enthought's Pylab/Scipy/Numpy) and R for statistical analysis. I'm trying to install rpy2 into Python to integrate R, but I get the error:

Tried to guess R's HOME but no R
command in the PATH.

I'm not sure what this means. The path to my R.exe is "C:\Program Files\R\R-2.12.1\bin" if that's useful. Any help would be much appreciated!

Here's setup.py's code

import os, os.path, sys, shutil, re, itertools
from distutils.command.build_ext import build_ext as _build_ext
from distutils.command.build import build as _build

from distutils.core import setup
from distutils.core import Extension


pack_name = 'rpy2'
pack_version = __import__('rpy').__version__


class build(_build):
    user_options = _build.user_options + \
        [
        #('r-autoconfig', None,
        # "guess all configuration paths from " +\
        #     "the R executable found in the PATH " +\
        #     "(this overrides r-home)"),
        ('r-home=', None, 
         "full path for the R home to compile against " +\
             "(see r-autoconfig for an automatic configuration)"),
        ('r-home-lib=', None,
         "full path for the R shared lib/ directory " +\
             "(<r-home>/lib otherwise)"),
        ('r-home-modules=', None,
         "full path for the R shared modules/ directory " +\
             "(<r-home>/modules otherwise)") 
        ]
    boolean_options = _build.boolean_options #+ \
        #['r-autoconfig', ]


    def initialize_options(self):
        _build.initialize_options(self)
        self.r_autoconfig = None
        self.r_home = None
        self.r_home_lib = None
        self.r_home_modules = None

class build_ext(_build_ext):
    """
    -DRPY_VERBOSE
    -DRPY_DEBUG_PRESERV
    -DRPY_DEBUG_PROMISE    : evaluation of promises
    -DRPY_DEBUG_OBJECTINIT : initialization of PySexpObject
    -DRPY_DEBUG_CONSOLE    : console I/O
    -DRPY_DEBUG_COBJECT    : SexpObject passed as a CObject
    -DRPY_DEBUG_GRDEV
    """
    user_options = _build_ext.user_options + \
        [
        #('r-autoconfig', None,
        #  "guess all configuration paths from " +\
        #      "the R executable found in the PATH " +\
        #      "(this overrides r-home)"),
        ('r-home=', None, 
         "full path for the R home to compile against " +\
             "(see r-autoconfig for an automatic configuration)"),
        ('r-home-lib=', None,
         "full path for the R shared lib/ directory" +\
             "(<r-home>/lib otherwise)"),
        ('r-home-modules=', None,
         "full path for the R shared modules/ directory" +\
             "(<r-home>/modules otherwise)")]

    boolean_options = _build_ext.boolean_options #+ \
        #['r-autoconfig', ]

    def initialize_options(self):
        _build_ext.initialize_options(self)
        self.r_autoconfig = None
        self.r_home = None
        self.r_home_lib = None
        self.r_home_modules = None

    def finalize_options(self):
        self.set_undefined_options('build',
                                   #('r_autoconfig', 'r_autoconfig'),
                                   ('r_home', 'r_home'))
        _build_ext.finalize_options(self) 
        if self.r_home is None:
            self.r_home = os.popen("R RHOME").readlines()
            if len(self.r_home) == 0:
                raise SystemExit("Error: Tried to guess R's HOME but no R command in the PATH.")

    #Twist if 'R RHOME' spits out a warning
            if self.r_home[0].startswith("WARNING"):
                self.r_home = self.r_home[1]
            else:
                self.r_home = self.r_home[0]
            #self.r_home = [self.r_home, ]

        if self.r_home is None:
            raise SystemExit("Error: --r-home not specified.")
        else:
            self.r_home = self.r_home.split(os.pathsep)

        rversions = []
        for r_home in self.r_home:
            r_home = r_home.strip()
        rversion = get_rversion(r_home)
        if cmp_version(rversion[:2], [2, 8]) == -1:
            raise SystemExit("Error: R >= 2.8 required.")
        rversions.append(rversion)

        config = RConfig()
        for about in ('--ldflags', '--cppflags', 
                      'LAPACK_LIBS', 'BLAS_LIBS'):
            config += get_rconfig(r_home, about)

        print(config.__repr__())

        self.include_dirs.extend(config._include_dirs)
        self.libraries.extend(config._libraries)
        self.library_dirs.extend(config._library_dirs)

        if self.r_home_modules is None:
            self.library_dirs.extend([os.path.join(r_home, 'modules'), ])
        else:
            self.library_dirs.extends([self.r_home_modules, ])

        #for e in self.extensions:
        #    self.extra_link_args.extra_link_args(config.extra_link_args)
        #    e.extra_compile_args.extend(extra_compile_args)

    def run(self):
        _build_ext.run(self)



def get_rversion(r_home):
    r_exec = os.path.join(r_home, 'bin', 'R')
    # Twist if Win32
    if sys.platform == "win32":
        rp = os.popen3('"'+r_exec+'" --version')[2]
    else:
        rp = os.popen('"'+r_exec+'" --version')
    rversion = rp.readline()
    #Twist if 'R RHOME' spits out a warning
    if rversion.startswith("WARNING"):
        rversion = rp.readline()
    m = re.match('^R version ([^ ]+) .+
, rversion)
    rversion = m.groups()[0]
    rversion = rversion.split('.')
    rversion[0] = int(rversion[0])
    rversion[1] = int(rversion[1])
    return rversion

def cmp_version(x, y):
    if (x[0] < y[0]):
        return -1
    if (x[0] > y[0]):
        return 1
    if (x[0] == y[0]):
        if len(x) == 1 or len(y) == 1:
            return 0
        return cmp_version(x[1:], y[1:])

class RConfig(object):
    _include_dirs = None
    _libraries = None
    _library_dirs = None 
    _extra_link_args = None
    _frameworks = None
    _framework_dirs = None
    def __init__(self,
                 include_dirs = tuple(), libraries = tuple(),
                 library_dirs = tuple(), extra_link_args = tuple(),
                 frameworks = tuple(),
                 framework_dirs = tuple()):
        for k in ('include_dirs', 'libraries', 
                  'library_dirs', 'extra_link_args'):
            v = locals()[k]
            if not isinstance(v, tuple):
                if isinstance(v, str):
                    v = [v, ]
            v = tuple(set(v))
            self.__dict__['_'+k] = v
        # frameworks are specific to OSX
        for k in ('framework_dirs', 'frameworks'):
            v = locals()[k]
            if not isinstance(v, tuple):
                if isinstance(v, str):
                    v = [v, ]
            v = tuple(set(v))
            self.__dict__['_'+k] = v
            self.__dict__['_'+'extra_link_args'] = tuple(set(v + self.__dict__['_'+'extra_link_args']))


    def __repr__(self):
        s = 'Configuration for R as a library:' + os.linesep
        s += os.linesep.join(
            ['  ' + x + ': ' + self.__dict__['_'+x].__repr__() \
                 for x in ('include_dirs', 'libraries',
                           'library_dirs', 'extra_link_args')])
        s += os.linesep + ' # OSX-specific (included in extra_link_args)' + os.linesep 
        s += os.linesep.join(
            ['  ' + x + ': ' + self.__dict__['_'+x].__repr__() \
                 for x in ('framework_dirs', 'frameworks')]
            )

        return s

    def __add__(self, config):
        assert isinstance(config, RConfig)
        res = RConfig(include_dirs = self._include_dirs + \
                          config._include_dirs,
                      libraries = self._libraries + config._libraries,
                      library_dirs = self._library_dirs + \
                          config._library_dirs,
                      extra_link_args = self._extra_link_args + \
                          config._extra_link_args)
        return res
    @staticmethod
    def from_string(string, allow_empty = False):
        possible_patterns = ('^-L(?P<library_dirs>[^ ]+)
,
                             '^-l(?P<libraries>[^ ]+)
,
                             '^-I(?P<include_dirs>[^ ]+)
,
                             '^(?P<framework_dirs>-F[^ ]+?)
,
                             '^(?P<frameworks>-framework [^ ]+)
)
        pp = [re.compile(x) for x in possible_patterns]
        # sanity check of what is returned into rconfig
        rconfig_m = None        
        span = (0, 0)
        rc = RConfig()
        for substring in re.split('(?<!-framework) ', string):
            ok = False
            for pattern in pp:
                rconfig_m = pattern.match(substring)
                if rconfig_m is not None:
                    rc += RConfig(**rconfig_m.groupdict())
                    span = rconfig_m.span()
                    ok = True
                    break
                elif rconfig_m is None:
                    if allow_empty and (rconfig == ''):
                        print(cmd + '\nreturned an empty string.\n')
                        rc += RConfig()
                        ok = True
                        break
                    else:
                        # if the configuration points to an existing library, 
                        # use it
                        if os.path.exists(string):
                            rc += RConfig(library = substring)
                            ok = True
                            break
            if not ok:
                raise ValueError('Invalid substring\n' + substring 
                                 + '\nin string\n' + string)
        return rc

def get_rconfig(r_home, about, allow_empty = False):
    r_exec = os.path.join(r_home, 'bin', 'R')
    cmd = '"'+r_exec+'" CMD config '+about
    rp = os.popen(cmd)
    rconfig = rp.readline()
    #Twist if 'R RHOME' spits out a warning
    if rconfig.startswith("WARNING"):
        rconfig = rp.readline()
    rconfig = rconfig.strip()
    rc = RConfig.from_string(rconfig)
    return rc


def getRinterface_ext():
    #r_libs = [os.path.join(RHOME, 'lib'), os.path.join(RHOME, 'modules')]
    r_libs = []
    extra_link_args = []

    #FIXME: crude way (will break in many cases)
    #check how to get how to have a configure step
    define_macros = []

    if sys.platform == 'win32':
        define_macros.append(('Win32', 1))
    else:
        define_macros.append(('R_INTERFACE_PTRS', 1))
        define_macros.append(('HAVE_POSIX_SIGJMP', 1))

    define_macros.append(('CSTACK_DEFNS', 1))
    define_macros.append(('RIF_HAS_RSIGHAND', 1))

    include_dirs = []

    rinterface_ext = Extension(
            name = pack_name + '.rinterface.rinterface',
            sources = [ \
            #os.path.join('rpy', 'rinterface', 'embeddedr.c'), 
            #os.path.join('rpy', 'rinterface', 'r_utils.c'),
            #os.path.join('rpy', 'rinterface', 'buffer.c'),
            #os.path.join('rpy', 'rinterface', 'sequence.c'),
            #os.path.join('rpy', 'rinterface', 'sexp.c'),
            os.path.join('rpy', 'rinterface', 'rinterface.c')
                       ],
            depends = [os.path.join('rpy', 'rinterface', 'embeddedr.h'), 
                       os.path.join('rpy', 'rinterface', 'r_utils.h'),
                       os.path.join('rpy', 'rinterface', 'buffer.h'),
                       os.path.join('rpy', 'rinterface', 'sequence.h'),
                       os.path.join('rpy', 'rinterface', 'sexp.h'),
                       os.path.join('rpy', 'rinterface', 'rpy_rinterface.h')
                       ],
            include_dirs = [os.path.join('rpy', 'rinterface'),] + include_dirs,
            libraries = ['R', ],
            library_dirs = r_libs,
            define_macros = define_macros,
            runtime_library_dirs = r_libs,
            #extra_compile_args=['-O0', '-g'],
            #extra_link_args = extra_link_args
            )

    rpy_device_ext = Extension(
        pack_name + '.rinterface.rpy_device',
            [
            os.path.join('rpy', 'rinterface', 'rpy_device.c'),
             ],
            include_dirs = include_dirs + 
                            [os.path.join('rpy', 'rinterface'), ],
            libraries = ['R', ],
            library_dirs = r_libs,
            define_macros = define_macros,
            runtime_library_dirs = r_libs,
            #extra_compile_args=['-O0', '-g'],
            extra_link_args = extra_link_args
        )

    return [rinterface_ext, rpy_device_ext]


rinterface_exts = []
ri_ext = getRinterface_ext()
rinterface_exts.append(ri_ext)

pack_dir = {pack_name: 'rpy'}

import distutils.command.install
for scheme in distutils.command.install.INSTALL_SCHEMES.values():
    scheme['data'] = scheme['purelib']

setup(
    #install_requires=['distribute'],
    cmdclass = {'build': build,
                'build_ext': build_ext},
    name = pack_name,
    version = pack_version,
    description = "Python interface to the R language",
    url = "http://rpy.sourceforge.net",
    license = "AGPLv3.0 (except rpy2.rinterface: LGPL)",
    author = "Laurent Gautier",
    author_email = "[email protected]",
    ext_modules = rinterface_exts[0],
    package_dir = pack_dir,
    packages = [pack_name,
                pack_name + '.rlike',
                pack_name + '.rlike.tests',
                pack_name + '.rinterface',
                pack_name + '.rinterface.tests',
                pack_name + '.robjects',
                pack_name + '.robjects.tests',
                pack_name + '.robjects.lib',
                ],
    classifiers = ['Programming Language :: Python',
                   'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
                   'License :: OSI Approved :: GNU Affero General Public License v3',
                   'Intended Audience :: Developers',
                   'Intended Audience :: Science/Research',
                   'Development Status :: 5 - Production/Stable'
                   ],
    data_files = [(os.path.join('rpy2', 'images'), 
                   [os.path.join('doc', 'source', 'rpy2_logo.png')])]

    #[pack_name + '.rinterface_' + x for x in rinterface_rversions] + \
        #[pack_name + '.rinterface_' + x + '.tests' for x in rinterface_rversions]
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

坏尐絯 2024-10-23 09:56:06

我知道我来晚了一点,但我遇到了同样的问题,并通过 conda 安装使其在 Windows 7 上运行

conda install --channel https://conda.binstar.org/joshadel rpy2

I know I'm a little late to the party, but I had the same problem and got it working on Windows 7 by doing the install via conda

conda install --channel https://conda.binstar.org/joshadel rpy2
感受沵的脚步 2024-10-23 09:56:06

我在尝试将 rpy2 与 R 2.12 和 Python 2.6 一起使用时遇到了类似的问题(按照 rpy2 文档的建议)。

看来 Windows 二进制文件来自 http://cran .r-project.org/bin/windows/base/R-2.12.1-win.exe 将所需的 R.dll 安装在 rpy2 不需要的目录中。

我已将 R\R-2.12.1\bin\i386 中的所有文件复制到 bin 目录,设置环境变量 R_HOME 指向 R\R-2.12.1,然后就可以了。

I have encountered a similar problem trying to use rpy2 with R 2.12 and Python 2.6 (as recommended by the rpy2 documentation).

It seems that windows binaries from http://cran.r-project.org/bin/windows/base/R-2.12.1-win.exe install the required R.dll in a directory not expected by rpy2.

I have copied all the files from R\R-2.12.1\bin\i386 to the bin directory, set an environnement variable R_HOME pointing to R\R-2.12.1 and it then worked.

酒中人 2024-10-23 09:56:06

我遇到了一个有些不同但相关的安装问题,最后在 http://www.mail-archive.com/[电子邮件受保护]/msg02817.html

之后通过 rpy2-2.0.8.win32-py2.6.msi 安装了 rpy2 2.0.8(Windows 7(64 位)、python 2.6、R 2.14(32 位和 64 位,通过 RAndFriendsSetup2140V3.2-1-1.exe)),并尝试在 python 控制台中导入 rpy2,我得到了异常:

无法找到 R.dll

将以下行添加到 rinterface/__init__.py 使其工作:

if os.path.exists(os.path.join(R_HOME, 'lib')):             ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin')
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules')
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'lib')
else:                                   ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin', 'i386')     ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules', 'i386') ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'library')     ## ADDED ##

# Load the R dll using the explicit path
# First try the bin dir:
Rlib = os.path.join(R_HOME, 'bin', 'R.dll')
# Try bin/i386 subdirectory seen in R 2.12.0                ## ADDED ##
if not os.path.exists(Rlib):                        ## ADDED ##
    Rlib = os.path.join(R_HOME, 'bin', 'i386', 'R.dll')         ## ADDED ##
# Then the lib dir:
if not os.path.exists(Rlib):
    Rlib = os.path.join(R_HOME, 'lib', 'R.dll')
# Otherwise fail out!
if not os.path.exists(Rlib):
    raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)

事实证明,R.dll 是已移动,但 __init__.py 未相应更新。因此,只需编辑 __init__.py 文件即可解决问题。

然后我尝试复制 Taj G 的情况,我做到了。
将“your_R_installation_dir\bin\i386”添加到Windows环境变量PATH后,旧错误消失了,但又出现了新错误:

ValueError: Invalid substring in string

似乎需要安装一些额外的部分,并且需要正确配置C/C++编译器。我在这里放弃了。在 Windows 上使用 easy_install 从源代码构建 rpy2 似乎非常棘手,而且目前还没有正式支持。

虽然与 2.2.4 相比,rpy2 2.0.8 并不是一个成熟的版本,但它是 sourceforge 上带有标准 Windows 安装程序的最新版本。就目前而言,这是一个简单的选择。

I've encountered a somewhat different but related installation problem and finally found a solution at http://www.mail-archive.com/[email protected]/msg02817.html

After I installed rpy2 2.0.8 via rpy2-2.0.8.win32-py2.6.msi (windows 7 (64bit), python 2.6, R 2.14 (both 32bit and 64bit, via RAndFriendsSetup2140V3.2-1-1.exe)), and tried to import rpy2 within python console, I got the exception:

Unable to locate R.dll

Adding the following lines to rinterface/__init__.py made it work:

if os.path.exists(os.path.join(R_HOME, 'lib')):             ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin')
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules')
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'lib')
else:                                   ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin', 'i386')     ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules', 'i386') ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'library')     ## ADDED ##

# Load the R dll using the explicit path
# First try the bin dir:
Rlib = os.path.join(R_HOME, 'bin', 'R.dll')
# Try bin/i386 subdirectory seen in R 2.12.0                ## ADDED ##
if not os.path.exists(Rlib):                        ## ADDED ##
    Rlib = os.path.join(R_HOME, 'bin', 'i386', 'R.dll')         ## ADDED ##
# Then the lib dir:
if not os.path.exists(Rlib):
    Rlib = os.path.join(R_HOME, 'lib', 'R.dll')
# Otherwise fail out!
if not os.path.exists(Rlib):
    raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)

It turns out that the R.dll is moved but the __init__.py is not updated accordingly. So simply editing the __init__.py file will get things right.

Then I tried to replicate Taj G's situation, and I did it.
After adding "your_R_installation_dir\bin\i386" into the windows environment variable PATH, the old error disappeared but new one came:

ValueError: Invalid substring in string

It seems that some additional pieces need to be installed and a C/C++ compiler needs to be correctly configured. I gave up here. Using easy_install to build rpy2 from source seems really tricky on windows and not officially support at present.

Although rpy2 2.0.8 is not a full-fledged version compared with 2.2.4, it is the latest version with standard windows installer on sourceforge. For now, it is the easy choice.

挽心 2024-10-23 09:56:06

我也遇到了 RPy2 的问题,但在日复一日地尝试各种不同的解决方案之后,我从未真正让它发挥作用。我鼓励您尝试人们告诉您的所有好主意,我很想看看它们是否有效!

如果您像我一样失败,您可以根据您的目的通过以下方式使用解决方法:

  1. 使用您想要使用的所有函数编写 R 代码,该代码还调用您想要使用的库。将所有这些函数和库调用放入一个文件 (temp.r)。例如,也许我的文件看起来像

    <前><代码>
    CurrentYear <- function(birth.year,age) {
    年份 <- 出生年份 + 年龄
    回报(年)
    }

  2. 使用 python 提示用户输入以进行函数调用。我使用 GUI 完成了此操作,您可能只需在终端中使用脚本即可完成此操作。

  3. 使用 python 通过 R 函数调用创建字符串。例如,我们可能有

     stuff = '\nCurrentYear("%(birth.year)d", "%(age)d")\n' %vars() 

    其中birth.yearage已由用户在python程序中输入。

  4. 使用 python 将 stuff 添加到 temp.r 的末尾:

    <前><代码>
    # 创建 temp.r 的副本,以免干扰其内容以供将来使用。
    tocall = copyfile("C:\我的文档\temp.r", "C:\我的文档\tocall.r")
    # 打开副本并意图附加它(因此是“a”)
    inp = open("C:\tocall.r", "a")
    # 将函数调用添加到 R 脚本
    inp.write(东西)
    inp.close()
    # 导航到正确的目录,使用“Rscript”
    # 在 shell 中运行代码
    dostuff = call('cd C:\My Documents &Rscript temp.r', shell = True)

  5. 利润

我不确定这种方法的缺点是什么,但它对我有用。如果您整个 RPy2 失败,希望这会有所帮助!

I too had a problem with RPy2, and I never actually got it to work - after days and days of trying all different sorts of solutions. I encourage you to try all of the great ideas people are telling you, and I'm interested to see if any of them work!

If you fail as I did, you may be able to use a workaround in the following way depending on your purposes:

  1. Write R code with all of the functions you would like to use which also calls the libraries you want to use. Put all of these functions and library calls into one file (temp.r). For example, maybe my file looks like

    
    CurrentYear <- function(birth.year,age) { 
       year <- birth.year + age
       return(year)
    }
    

  2. Use python to prompt for user input to do function calls. I did this with a GUI, you can probably just do it with script in the terminal.

  3. Use python to create a string with the R function calls. For example, we might have

     stuff = '\nCurrentYear("%(birth.year)d", "%(age)d")\n' %vars() 


    where birth.year and age have been input by the user in the python program.

  4. Add stuff to the end of temp.r using python:

    
    # Creates a copy of temp.r, so as not to disturb its contents for future use.
    tocall = copyfile("C:\My Documents\temp.r", "C:\My Documents\tocall.r")
    # Open the copy with the intent to append it (hence the "a")
    inp = open("C:\tocall.r", "a")
    # Adds the function call to the R script
    inp.write(stuff)
    inp.close()
    # Navigate to the correct directory, use "Rscript" to
    # run the code in the shell
    dostuff = call('cd C:\My Documents &Rscript temp.r', shell = True)
    

  5. Profit

I'm not sure what the disadvantages of this method are, but it works for me. Hope this helps if you fail with the whole RPy2 thing!

So要识趣 2024-10-23 09:56:06

我在 win32 "WINBLOW :)" XP 下在 python 中运行 rpy2 时遇到了完全相同的问题;但我终于成功了!

我是这样做的:

  1. 与 Guillaume 一样,我必须将所有文件从 C:\Program Files\R\R-2.15.0\bin\i386 复制到 R\R-2.15.0

  2. 我必须设置一个环境变量

转到我的电脑>属性>高级>环境变量>
变量:“PATH”,在“Value”末尾添加;C:\Program Files\R\R-2.15.0

  1. 重新运行python setup.py build install在命令提示符下 C:\Python27\rpy2>

希望这有帮助,请告诉我这是否适合您。

I had exactly the same problem running rpy2 in python under win32 "WINBLOW :)" XP ; but I got this to work , FINALLY !!.

Here's how I did it :

  1. Same as Guillaume, I had to copy all the files from C:\Program Files\R\R-2.15.0\bin\i386 to R\R-2.15.0

  2. I had to set an environment variable

Go to My Computer>properties>Advanced>Environment Variables>
Variable : "PATH" , adding ;C:\Program Files\R\R-2.15.0 at the end of the "Value"

  1. rerun python setup.py build install under command prompt C:\Python27\rpy2>

Hope This help, and please let me know if this work for you.

野侃 2024-10-23 09:56:06

从源代码中让 rpy2 最终可以与 Windows 7 一起使用。如果它对你不起作用,就像对我不起作用一样。请参阅在 Windows 7 上安装 rpy2

希望这有帮助!

Got rpy2 to work finally with Windows 7 from source code. If it doesn't work for you like it didn't for me. See rpy2 install on windows 7.

Hope this helps!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文