Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knitr related issues #424

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/org/renjin/primitives/Primitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ public Primitives() {
f("writeChar", /*writechar*/ null, 211);
f("open", Connections.class, 11);
f("isOpen", Connections.class, 11);
f("isIncomplete", /*isincomplete*/ null, 11);
f("isIncomplete", Connections.class, 11);
f("isSeekable", /*isseekable*/ null, 11);
f("close", Connections.class, 11);
f("flush", Connections.class, 11);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,11 @@ enum Type {
* @return the type of Connection: either {@link Type#BINARY} or {@link Type#TEXT}
*/
Type getType();

/**
* @return true if connection type is {@link Type#TEXT} and buffer is empty and false otherwise.
*/
default boolean isIncomplete() {
return false;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.renjin.invoke.annotations.Internal;
import org.renjin.invoke.annotations.Recycle;
import org.renjin.invoke.reflection.converters.StringArrayConverter;
import org.renjin.primitives.Deparse;
import org.renjin.primitives.Identical;
import org.renjin.primitives.io.connections.Connection.Type;
import org.renjin.primitives.text.RCharsets;
Expand Down Expand Up @@ -419,4 +420,12 @@ private static IntVector newConnection(final Context context, String open, Conne
}
return context.getSession().getConnectionTable().newConnection(conn);
}

@Internal
public static boolean isIncomplete(@Current Context context, SEXP conn) throws IOException {

Connection con = Connections.getConnection(context, conn);

return con.isIncomplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class WriteTextConnection implements Connection {
private Symbol objectName;
private Environment environment;
private boolean open = true;
private StringBuilder buffer = new StringBuilder();

private PrintWriter printWriter;

Expand All @@ -38,7 +39,6 @@ public WriteTextConnection(final Symbol objectName, final Environment environmen
this.printWriter = new PrintWriter(new Writer() {

private List<String> lines = new ArrayList<>();
private StringBuilder buffer = new StringBuilder();

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
Expand Down Expand Up @@ -113,6 +113,11 @@ public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("TODO");
}

@Override
public boolean isIncomplete() {
return buffer.length() > 0;
}

@Override
public PrintWriter getPrintWriter() throws IOException {
return printWriter;
Expand Down
4 changes: 2 additions & 2 deletions packages/grDevices/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export(Hershey, adjustcolor, as.graphicsAnnot, as.raster, axisTicks,
make.rgb, n2mfrow, nclass.Sturges, nclass.FD, nclass.scott,
palette, pdf, pdf.options, pdfFonts, pictex, postscript,
postscriptFonts, ps.options, rainbow, recordGraphics,
recordPlot, replayPlot, rgb, rgb2hsv, savePlot, setEPS,
recordPlot, replayPlot, rgb, rgb2hsv, setEPS, # removed: savePlot,
setGraphicsEventEnv, setGraphicsEventHandlers, setPS,
terrain.colors, topo.colors, trans3d, Type1Font, .axisPars,
xfig, xyTable, xy.coords, xyz.coords)
Expand All @@ -24,7 +24,7 @@ export(Hershey, adjustcolor, as.graphicsAnnot, as.raster, axisTicks,
export(.Call.graphics, .External.graphics)

## devices common to all platforms
export(X11, x11, bitmap, bmp, cairo_pdf, cairo_ps, jpeg, png, svg, tiff)
export(bitmap, cairo_pdf, cairo_ps, png, svg) # removed: jpeg, tiff, x11, X11, bmp,

S3method(print, recordedplot)
S3method(print, colorConverter)
Expand Down
113 changes: 113 additions & 0 deletions packages/grDevices/src/main/R/dev2bitmap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# File src/library/grDevices/R/unix/dev2bitmap.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2014 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/

dev2bitmap <- function(file, type = "png16m", height = 7, width = 7, res = 72,
units = "in", pointsize, ...,
method = c("postscript", "pdf"), taa = NA, gaa = NA)
{
if(missing(file)) stop("'file' is missing with no default")
if(!is.character(file) || length(file) != 1L || !nzchar(file))
stop("'file' must be a non-empty character string")
method <- match.arg(method)
units <- match.arg(units, c("in", "px", "cm", "mm"))
height <- switch(units, "in"=1, "cm"=1/2.54, "mm"=1/25.4, "px"=1/res) * height
width <- switch(units, "in"=1, "cm"=1/2.54, "mm"=1/25.4, "px"=1/res) * width
gsexe <- tools::find_gs_cmd()
if(!nzchar(gsexe)) stop("GhostScript was not found")
check_gs_type(gsexe, type)
if(missing(pointsize)) pointsize <- 1.5*min(width, height)
tmp <- tempfile("Rbit")
on.exit(unlink(tmp))
din <- graphics::par("din"); w <- din[1L]; h <- din[2L]
if(missing(width) && !missing(height)) width <- w/h * height
if(missing(height) && !missing(width)) height <- h/w * width

current.device <- dev.cur()
if(method == "pdf")
dev.off(dev.copy(device = pdf, file = tmp, width = width,
height = height,
pointsize = pointsize, paper = "special", ...))
else
dev.off(dev.copy(device = postscript, file = tmp, width = width,
height = height,
pointsize = pointsize, paper = "special",
horizontal = FALSE, ...))
dev.set(current.device)
extra <- ""
if (!is.na(taa)) extra <- paste0(" -dTextAlphaBits=", taa)
if (!is.na(gaa)) extra <- paste0(extra, " -dGraphicsAlphaBits=", gaa)
cmd <- paste0(shQuote(gsexe), " -dNOPAUSE -dBATCH -q -sDEVICE=", type,
" -r", res,
" -dAutoRotatePages=/None",
" -g", ceiling(res*width), "x", ceiling(res*height),
extra,
" -sOutputFile=", shQuote(file), " ", tmp)
system(cmd)
invisible()
}

bitmap <- function(file, type = "png16m", height = 7, width = 7, res = 72,
units = "in", pointsize, taa = NA, gaa = NA, ...)
{
if(missing(file)) stop("'file' is missing with no default")
if(!is.character(file) || length(file) != 1L || !nzchar(file))
stop("'file' must be a non-empty character string")
units <- match.arg(units, c("in", "px", "cm", "mm"))
height <- switch(units, "in"=1, "cm"=1/2.54, "mm"=1/25.4, "px"=1/res) * height
width <- switch(units, "in"=1, "cm"=1/2.54, "mm"=1/25.4, "px"=1/res) * width
gsexe <- tools::find_gs_cmd()
if(!nzchar(gsexe)) stop("GhostScript was not found")
check_gs_type(gsexe, type)
if(missing(pointsize)) pointsize <- 1.5*min(width, height)
extra <- ""
if (!is.na(taa)) extra <- paste0(" -dTextAlphaBits=", taa)
if (!is.na(gaa)) extra <- paste0(extra, " -dGraphicsAlphaBits=", gaa)
cmd <- paste0("|", shQuote(gsexe),
" -dNOPAUSE -dBATCH -q -sDEVICE=", type,
" -r", res,
" -dAutoRotatePages=/None",
" -g", ceiling(res*width), "x", ceiling(res*height),
extra,
" -sOutputFile=", shQuote(file), " -")
postscript(file = cmd, width = width, height = height,
pointsize = pointsize, paper = "special", horizontal = FALSE, ...)
invisible()
}


## unexported
check_gs_type <- function(gsexe, type)
{
gshelp <- system(paste(gsexe, "-help"), intern = TRUE)
st <- grep("^Available", gshelp)
en <- grep("^Search", gshelp)
if(!length(st) || !length(en))
warning("unrecognized format of gs -help")
else {
gsdevs <- gshelp[(st+1L):(en-1L)]
devs <- c(strsplit(gsdevs, " "), recursive = TRUE)
if(match(type, devs, 0L) == 0L) {
op <- options(warning.length = 8000L)
on.exit(options(op))
stop(gettextf("device '%s' is not available\n", type),
gettextf("Available devices are:\n%s",
paste(gsdevs, collapse = "\n")),
domain = NA)
}
}
}
25 changes: 25 additions & 0 deletions packages/grDevices/src/main/R/png.R
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# File src/library/grDevices/R/unix/png.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2015 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/

# png() and .geometry() moved to JavaGD.R file


grSoftVersion <- function() {
bm <- .Call(C_bmVersion)
if(nzchar(bm[3L])) bm[3L] <- strsplit(bm[3L], "\n")[[1L]][1L]
c(cairo = cairoVersion(), bm)
}