Debian Codegraph @

R/html-tools.R

raw
1
2 show_html <- function(x,output=NULL,...){
3
4 ht <- format_html(x,...)
5 ht <- as.character(ht)
6
7 if(interactive()){
8 # Test whether running under RStudio
9 isRStudio <- Sys.getenv("RSTUDIO") == "1"
10 if(isRStudio)
11 deflt.output <- "file-show"
12 else
13 deflt.output <- "browser"
14 }
15 else
16 deflt.output <- "stdout"
17
18 if(missing(output))
19 output <- getOption("html_viewer",deflt.output)
20
21 if(mode(output)=="character")
22 output <- match.arg(output,c("stdout","browser","file-show"))
23 else if(!is.function(output))
24 stop("'output' should be either a character string of a function")
25
26 if(is.function(output)){
27
28 tf <- tempfile()
29 tf <- paste0(tf,".html")
30 ht <- paste0("<!DOCTYPE html>\n",ht)
31 cat(ht,file=tf)
32
33 output(tf)
34 }
35 else if(nzchar(Sys.getenv("JPY_PARENT_PID"))){
36 ## Inside Jupyter
37 return(html_div(ht))
38 }
39 else if(output=="stdout") cat(ht)
40 else {
41
42 tf <- tempfile()
43 tf <- paste0(tf,".html")
44 ht <- paste0("<!DOCTYPE html>\n",ht)
45 cat(ht,file=tf)
46
47 if(output=="file-show")
48 file.show(tf,title=deparse(substitute(x)))
49 else
50 browseURL(tf)
51 }
52
53 }
54
55 write_html <- function(x,file,...,standalone=TRUE){
56 ht <- format_html(x,...)
57 ht <- as.character(ht)
58 if(standalone)
59 ht <- paste0("<!DOCTYPE html>\n",ht)
60 cat(ht,file=file)
61 }
62
63 format_html <- function(x,...)
64 UseMethod("format_html")
65
66
67
68 spltDec <- function(x,at="."){
69 y <- strsplit(x,at,fixed=TRUE)
70 y1 <- sapply(y,"[",1)
71 y3 <- sapply(y,"[",2)
72 y1[is.na(y1)] <- ""
73 y3[is.na(y3)] <- ""
74 y2 <- ifelse(grepl("[[:digit:]]+",y3),at,"")
75 y <- rbind(y1,y2,y3)
76 as.vector(y)
77 }
78
79
80
81 upd_vect <- function(x,...){
82 y <- c(...)
83 n <- names(y)
84 x[n] <- y
85 x
86 }