Skip to content

Commit

Permalink
view mode knit working in v4
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Feb 9, 2024
1 parent 1dc0fe0 commit 05a9db2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Generated by roxygen2: do not edit by hand


if(getRversion() >= "3.6.0") {
S3method(knitr::knit_print, tmap)
} else {
export(knit_print.tmap)
}

if(getRversion() >= "3.6.0") {
S3method(knitr::knit_print, tmap_arrange)
} else {
Expand Down
25 changes: 22 additions & 3 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#' @param vp viewport (for `"plot"` mode)
#' @param ... not used
#' @export
print.tmap = function(x, return.asp = FALSE, show = TRUE, vp = NULL, ...) {
#' @method print tmap
print.tmap = function(x, return.asp = FALSE, show = TRUE, vp = NULL, knit = FALSE, options = NULL, ...) {
args = list(...)
dev = getOption("tmap.devel.mode")
if (dev) timing_init()
x2 = step1_rearrange(x)
Expand All @@ -15,13 +17,30 @@ print.tmap = function(x, return.asp = FALSE, show = TRUE, vp = NULL, ...) {
if (dev) timing_add("step 2")
x4 = step3_trans(x3)
if (dev) timing_add("step 3")
res = step4_plot(x4, vp = vp, return.asp = return.asp, show = show)
res = step4_plot(x4, vp = vp, return.asp = return.asp, show = show, knit = knit, args)
if (dev) timing_add("step 4")
if (dev) timing_eval()
#if (return.asp) return(asp) else invisible(NULL)
invisible(res)
if (knit && tmap_graphics_name() == "Leaflet") {
kp = get("knit_print", asNamespace("knitr"))
return(do.call(kp, c(list(x=res), args, list(options=options))))
} else {
invisible(res)
}
}

#' @rdname print.tmap
#' @rawNamespace
#' if(getRversion() >= "3.6.0") {
#' S3method(knitr::knit_print, tmap)
#' } else {
#' export(knit_print.tmap)
#' }
knit_print.tmap <- function(x, ..., options=NULL) {
print.tmap(x, knit=TRUE, options=options, ...)
}


timing_init = function() {
ts = data.table(s1 = "---------", s2 = "---------", s3 = "---------", s4 = "---------", t = Sys.time())
assign("timings", ts, envir = .TMAP)
Expand Down
4 changes: 2 additions & 2 deletions R/step4_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ process_components2 = function(cdt, o) {
cdt
}

step4_plot = function(tm, vp, return.asp, show) {
step4_plot = function(tm, vp, return.asp, show, knit, args) {
tmx = tm$tmo
o = tm$o
aux = tm$aux
Expand Down Expand Up @@ -750,5 +750,5 @@ step4_plot = function(tm, vp, return.asp, show) {



do.call(FUNrun, list(o = o, show = show))
do.call(FUNrun, list(o = o, show = show, knit = knit, args))
}
2 changes: 1 addition & 1 deletion R/tmapGridRun.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tmapGridRun = function(o, show) {
tmapGridRun = function(o, show, knit, args) {
gts = get("gts", .TMAP_GRID)
if (show) {
mapply(function(gt,i) {
Expand Down
6 changes: 4 additions & 2 deletions R/tmapLeafletRun.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tmapLeafletRun = function(o, show) {
tmapLeafletRun = function(o, show, knit, args) {
lfs = get("lfs", envir = .TMAP_LEAFLET)

lfs2 = lapply(lfs, function(lfsi) {
Expand All @@ -23,6 +23,8 @@ tmapLeafletRun = function(o, show) {
})

if (length(lfs2) == 1) lfs2 = lfs2[[1]]
if (show) print(lfs2)
if (show && !knit) {
print(lfs2)
}
lfs2
}
13 changes: 12 additions & 1 deletion man/print.tmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 05a9db2

@mtennekes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workaround, but ugly.

Please sign in to comment.