Esempio n. 1
0
    def setup_graphics(self, args):
        """Setup graphics in preparation for evaluating R code.

        args : argparse bunch (should be whatever the R magic got)."""

        if getattr(args, 'units') is not None:
            if args.units != "px" and getattr(args, 'res') is None:
                args.res = 72

        plot_arg_names = ['width', 'height', 'pointsize', 'bg', 'type']
        if self.device == 'png':
            plot_arg_names += ['units', 'res']

        argdict = {}
        for name in plot_arg_names:
            val = getattr(args, name)
            if val is not None:
                argdict[name] = val

        tmpd = None
        if self.device in ['png', 'svg']:
            # Create a temporary directory for R graphics output
            # TODO: Do we want to capture file output for other device types
            # other than svg & png?
            tmpd = tempfile.mkdtemp()
            tmpd_fix_slashes = tmpd.replace('\\', '/')

            if self.device == 'png':
                # Note: that %% is to pass into R for interpolation there
                grdevices.png("%s/Rplots%%03d.png" % tmpd_fix_slashes,
                              **argdict)
            elif self.device == 'svg':
                self.cairo.CairoSVG("%s/Rplot.svg" % tmpd_fix_slashes,
                                    **argdict)

        elif self.device == 'X11':
            # Open a new X11 device, except if the current one is already an
            # X11 device.
            ro.r("""
            if (substr(names(dev.cur()), 1, 3) != "X11") {
                X11()
            }""")

        else:
            # TODO: This isn't actually an R interpreter error...
            raise RInterpreterError(
                'device must be one of ("png", "X11", "svg")')

        return tmpd
Esempio n. 2
0
    def setup_graphics(args):
        """Setup graphics in preparation for evaluating R code.
        args : argparse bunch (should be whatever the R magic got)."""

        args = deepcopy(args)
        if args.get('units') is not None:
            if args["units"] != "px" and getattr(args, "res") is None:
                args.res = 72

        plot_arg_names = ['width', 'height', 'pointsize', 'bg']
        if device == 'png':
            plot_arg_names += ['units', 'res']

        argdict = {}
        for name in plot_arg_names:
            val = args.get(name)
            if val is not None:
                argdict[name] = val

        graph_dir = None
        if device in ['png', 'svg']:
            # Create a temporary directory for R graphics output
            # TODO: Do we want to capture file output for other device types
            # other than svg & png?
            graph_dir = tempfile.mkdtemp()
            graph_dir_fix_slashes = graph_dir.replace('\\', '/')

            if device == 'png':
                # Note: that %% is to pass into R for interpolation there
                grdevices.png("%s/Rplots%%03d.png" % graph_dir_fix_slashes,
                              **argdict)
            elif device == 'svg':
                argdict.pop("width")  # BUG in Cairo or rpy2?
                argdict.pop("height")  # BUG in Cairo or rpy2?
                cairo.CairoSVG("%s/Rplot.svg" % graph_dir_fix_slashes,
                               **argdict)
        else:
            # TODO: This isn't actually an R interpreter error...
            raise ValueError('device must be one of ("png", "svg")')
        return graph_dir