예제 #1
0
def process(args, table_lines):
    """
    @param args: command line or web input
    @param table_lines: input lines
    @return: the image data as a string
    """
    rtable = RUtil.RTable(table_lines)
    header_row = rtable.headers
    data_rows = rtable.data
    Carbone.validate_headers(header_row)
    # Read the relevant columns and their labels.
    plot_info = PlotInfo(args, header_row, data_rows)
    # Get info for the temporary data
    augmented_lines = plot_info.get_augmented_table_lines()
    # Create a temporary data table file for R.
    table_string = "\n".join(augmented_lines)
    temp_table_name = Util.create_tmp_file(table_string, suffix=".table")
    # Create a temporary pathname for the plot created by R.
    temp_plot_name = Util.get_tmp_filename()
    # Create a temporary R script file.
    script = plot_info.get_script(args, temp_plot_name, temp_table_name)
    temp_script_name = Util.create_tmp_file(script, suffix=".R")
    # Call R.
    retcode, r_out, r_err = RUtil.run(temp_script_name)
    if retcode:
        raise ValueError("R error:\n" + r_err)
    # Delete the temporary data table file.
    os.unlink(temp_table_name)
    # Delete the temporary script file.
    os.unlink(temp_script_name)
    # Read the image file.
    try:
        with open(temp_plot_name, "rb") as fin:
            image_data = fin.read()
    except IOError as e:
        raise HandlingError("the R call seems to not have created the plot")
    # Delete the temporary image file.
    os.unlink(temp_plot_name)
    # Return the image data as a string.
    return image_data
예제 #2
0
def process(args, table_lines):
    """
    @param args: command line or web input
    @param table_lines: input lines
    @return: the image data as a string
    """
    rtable = RUtil.RTable(table_lines)
    header_row = rtable.headers
    data_rows = rtable.data
    Carbone.validate_headers(header_row)
    # Read the relevant columns and their labels.
    plot_info = PlotInfo(args, header_row, data_rows)
    # Get info for the temporary data
    augmented_lines = plot_info.get_augmented_table_lines()
    table_string = '\n'.join(augmented_lines)
    temp_table_name = Util.create_tmp_file(table_string, suffix='.table')
    temp_plot_name = Util.get_tmp_filename()
    script = plot_info.get_script(args, temp_plot_name, temp_table_name)
    temp_script_name = Util.create_tmp_file(script, suffix='.R')
    # Call R.
    retcode, r_out, r_err = RUtil.run(temp_script_name)
    if retcode:
        raise ValueError('R error:\n' + r_err)
    # Delete the temporary data table file.
    os.unlink(temp_table_name)
    # Delete the temporary script file.
    os.unlink(temp_script_name)
    # Read the image file.
    try:
        with open(temp_plot_name, 'rb') as fin:
            image_data = fin.read()
    except IOError as e:
        raise HandlingError('the R call seems to not have created the plot')
    # Delete the temporary image file.
    os.unlink(temp_plot_name)
    # Return the image data as a string.
    return image_data