Révision 1771

tmp/org.txm.statsengine.r.core/src/org/txm/statsengine/r/core/RWorkspace.java (revision 1771)
460 460
	 * @throws REXPMismatchException the rEXP mismatch exception
461 461
	 * @throws StatException the stat exception
462 462
	 */
463
	public void plot(File file, String expr)throws REXPMismatchException,
464
	StatException {
465
		plot(file, expr, "svg");
463
	public File plot(File file, String expr) throws REXPMismatchException, StatException, RserveException {
464
		return plot(file, expr, "svg");
466 465
	}
467 466
	/**
468 467
	 * Plot.
......
472 471
	 * @param device the device (svg, png, jpeg, ...)
473 472
	 * @throws REXPMismatchException the rEXP mismatch exception
474 473
	 * @throws StatException the stat exception
474
	 * @throws RserveException 
475 475
	 */
476
	public void plot(File file, String expr, String devicename)throws REXPMismatchException,
477
	StatException
478
	{
476
	public File plot(File file, String expr, String devicename) throws REXPMismatchException, StatException, RserveException	{
477
		
479 478
		try {
480 479
			file.createNewFile();
481
		} catch (IOException e1) {
482
			org.txm.utils.logger.Log.printStackTrace(e1);
483
			return;
484
		}
485
		if (!file.canWrite()) {
486
			System.out.println(NLS.bind("{0} cannot be written.", file)); //$NON-NLS-1$
487
		}
488
		
489
		String name;
490
		if (OSDetector.isFamilyWindows()) { //$NON-NLS-1$ //$NON-NLS-2$
491
			try {
480

  
481
			if (!file.canWrite()) {
482
				System.out.println(NLS.bind("{0} cannot be written.", file)); //$NON-NLS-1$
483
				return null;
484
			}
485
			
486
			String name;
487
			if (OSDetector.isFamilyWindows()) { //$NON-NLS-1$ //$NON-NLS-2$
492 488
				name = file.getCanonicalPath().replaceAll("/", "\\");
493
			} catch (IOException e) {
494
				throw new StatException(e);
495 489
			}
496
		} else {
497
			name = file.getAbsolutePath();
498
		}
490
			else {
491
				name = file.getAbsolutePath();
492
			}
499 493

  
500
		if (devicename.equals("devSVG")) voidEval("library(RSvgDevice);");
494
			if (devicename.equals("devSVG")) safeEval("library(RSvgDevice);");
501 495

  
502
		// FIXME : test for window dimensions so the large plots can be entirely displayed. Need to pass width and height to RWorkspace.plot() so
503
		// RChartsEngine will be able to dynamically compute the width from the number of bars in a barplot for example.
504
		//REXP xp = eval("try("+devicename+"(\"" + name + "\", width=20, height=10))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
496
			// FIXME : test for window dimensions so the large plots can be entirely displayed. Need to pass width and height to RWorkspace.plot() so
497
			// RChartsEngine will be able to dynamically compute the width from the number of bars in a barplot for example.
498
			//REXP xp = eval("try("+devicename+"(\"" + name + "\", width=20, height=10))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
505 499

  
506
		REXP xp = eval("try("+devicename+"(\"" + name + "\"))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
500
			REXP xp = eval("try("+devicename+"(\"" + name + "\"))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
507 501

  
508
		if (xp.isString() && xp.asString() != null) { // if there's a string
509
			// then we have a
510
			// problem, R sent an
511
			// error
512
			// System.out.println("Can't open svg graphics device:\n"+xp.asString());
513
			// this is analogous to 'warnings', but for us it's sufficient to
514
			// get just the 1st warning
515
			REXP w = eval("if (exists(\"last.warning\") && length(last.warning)>0) names(last.warning)[1] else 0"); //$NON-NLS-1$
516
			// if (w.asString()!=null) System.out.println(w.asString());
517
			throw new StatException(w.asString());
502
			if (xp.isString() && xp.asString() != null) { // if there's a string
503
				// then we have a
504
				// problem, R sent an
505
				// error
506
				// System.out.println("Can't open svg graphics device:\n"+xp.asString());
507
				// this is analogous to 'warnings', but for us it's sufficient to
508
				// get just the 1st warning
509
				REXP w = eval("if (exists(\"last.warning\") && length(last.warning)>0) names(last.warning)[1] else 0"); //$NON-NLS-1$
510
				// if (w.asString()!=null) System.out.println(w.asString());
511
				throw new StatException(w.asString());
512
			}
513

  
514
			// ok, so the device should be fine - let's plot
515
			safeEval(expr);
516
			safeEval("dev.off()"); //$NON-NLS-1$
517
			
518
			return file;
518 519
		}
519

  
520
		// ok, so the device should be fine - let's plot
521
		voidEval(expr);
522
		voidEval("dev.off()"); //$NON-NLS-1$
520
		catch (Exception e) {
521
			// TODO Auto-generated catch block
522
			e.printStackTrace();
523
			return null;
524
		}
523 525
	}
524 526
	
525 527
	/**

Formats disponibles : Unified diff