├── .Rprofile ├── .gitignore ├── app.R ├── html2r.Rproj └── packrat ├── init.R ├── packrat.lock └── packrat.opts /.Rprofile: -------------------------------------------------------------------------------- 1 | #### -- Packrat Autoloader (version 0.4.8-1) -- #### 2 | source("packrat/init.R") 3 | #### -- End Packrat Autoloader -- #### 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | packrat/lib*/ 2 | packrat/src*/ 3 | .Rproj.user 4 | rsconnect/ 5 | -------------------------------------------------------------------------------- /app.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(XML) 3 | library(magrittr) 4 | library(purrr) 5 | library(stringr) 6 | 7 | ui <- fluidPage( 8 | titlePanel("HTML to R Converter"), 9 | fluidRow( 10 | column(5, textAreaInput("html", "HTML", rows=20, value = ' 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
FirstnameLastnameAge
JillSmith50
EveJackson94
') 27 | ), 28 | column(2, checkboxInput("prefix", "Prefix"), actionButton("convert", "Convert")), 29 | column(5, tags$pre(textOutput("rCode"))) 30 | ), 31 | fluidRow(tags$a(href = "https://github.com/alandipert/html2r", "Github")) 32 | ) 33 | 34 | makeAttrs <- function(node) { 35 | attrs <- xmlAttrs(node) 36 | names(attrs) %>% 37 | Map(function (name) { 38 | val <- attrs[[name]] 39 | paste0(name, ' = ', if (val == "") "NA" else paste0('"', val, '"')) 40 | }, .) 41 | } 42 | 43 | Keep <- function(fun, xs) Map(fun, xs) %>% Filter(Negate(is.null), .) 44 | 45 | renderNode <- function(node, indent = 0, prefix = FALSE) { 46 | if (xmlName(node) == "text") { 47 | txt <- xmlValue(node) 48 | if (nchar(trimws(txt)) > 0) { 49 | paste0('"', trimws(txt), '"') 50 | } 51 | } else { 52 | tagName <- if (prefix) paste0("tags$", xmlName(node)) else xmlName(node) 53 | newIndent <- indent + length(tagName) + 1 54 | xmlChildren(node) %>% 55 | Keep(partial(renderNode, indent = newIndent, prefix = prefix), .) %>% 56 | append(makeAttrs(node), .) %>% 57 | paste(collapse = str_pad(",\n", width = newIndent, side = c("right"))) %>% 58 | trimws(which = c("left")) %>% 59 | paste0(tagName, "(", ., ")") 60 | } 61 | } 62 | 63 | html2R <- function(htmlStr, prefix = FALSE) { 64 | htmlStr %>% 65 | htmlParse %>% 66 | getNodeSet("/html/body/*") %>% 67 | `[[`(1) %>% 68 | renderNode(prefix = prefix) 69 | } 70 | 71 | server <- function(input, output, session) { 72 | 73 | rcode <- eventReactive(input$convert, { 74 | html2R(input$html, prefix = input$prefix) 75 | }, ignoreInit = TRUE) 76 | 77 | output$rCode <- renderText(rcode()) 78 | } 79 | 80 | shinyApp(ui, server) -------------------------------------------------------------------------------- /html2r.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | -------------------------------------------------------------------------------- /packrat/init.R: -------------------------------------------------------------------------------- 1 | local({ 2 | 3 | ## Helper function to get the path to the library directory for a 4 | ## given packrat project. 5 | getPackratLibDir <- function(projDir = NULL) { 6 | path <- file.path("packrat", "lib", R.version$platform, getRversion()) 7 | 8 | if (!is.null(projDir)) { 9 | 10 | ## Strip trailing slashes if necessary 11 | projDir <- sub("/+$", "", projDir) 12 | 13 | ## Only prepend path if different from current working dir 14 | if (!identical(normalizePath(projDir), normalizePath(getwd()))) 15 | path <- file.path(projDir, path) 16 | } 17 | 18 | path 19 | } 20 | 21 | ## Ensure that we set the packrat library directory relative to the 22 | ## project directory. Normally, this should be the working directory, 23 | ## but we also use '.rs.getProjectDirectory()' if necessary (e.g. we're 24 | ## rebuilding a project while within a separate directory) 25 | libDir <- if (exists(".rs.getProjectDirectory")) 26 | getPackratLibDir(.rs.getProjectDirectory()) 27 | else 28 | getPackratLibDir() 29 | 30 | ## Unload packrat in case it's loaded -- this ensures packrat _must_ be 31 | ## loaded from the private library. Note that `requireNamespace` will 32 | ## succeed if the package is already loaded, regardless of lib.loc! 33 | if ("packrat" %in% loadedNamespaces()) 34 | try(unloadNamespace("packrat"), silent = TRUE) 35 | 36 | if (suppressWarnings(requireNamespace("packrat", quietly = TRUE, lib.loc = libDir))) { 37 | 38 | # Check 'print.banner.on.startup' -- when NA and RStudio, don't print 39 | print.banner <- packrat::get_opts("print.banner.on.startup") 40 | if (print.banner == "auto" && is.na(Sys.getenv("RSTUDIO", unset = NA))) { 41 | print.banner <- TRUE 42 | } else { 43 | print.banner <- FALSE 44 | } 45 | return(packrat::on(print.banner = print.banner)) 46 | } 47 | 48 | ## Escape hatch to allow RStudio to handle bootstrapping. This 49 | ## enables RStudio to provide print output when automagically 50 | ## restoring a project from a bundle on load. 51 | if (!is.na(Sys.getenv("RSTUDIO", unset = NA)) && 52 | is.na(Sys.getenv("RSTUDIO_PACKRAT_BOOTSTRAP", unset = NA))) { 53 | Sys.setenv("RSTUDIO_PACKRAT_BOOTSTRAP" = "1") 54 | setHook("rstudio.sessionInit", function(...) { 55 | # Ensure that, on sourcing 'packrat/init.R', we are 56 | # within the project root directory 57 | if (exists(".rs.getProjectDirectory")) { 58 | owd <- getwd() 59 | setwd(.rs.getProjectDirectory()) 60 | on.exit(setwd(owd), add = TRUE) 61 | } 62 | source("packrat/init.R") 63 | }) 64 | return(invisible(NULL)) 65 | } 66 | 67 | ## Bootstrapping -- only performed in interactive contexts, 68 | ## or when explicitly asked for on the command line 69 | if (interactive() || "--bootstrap-packrat" %in% commandArgs(TRUE)) { 70 | 71 | message("Packrat is not installed in the local library -- ", 72 | "attempting to bootstrap an installation...") 73 | 74 | ## We need utils for the following to succeed -- there are calls to functions 75 | ## in 'restore' that are contained within utils. utils gets loaded at the 76 | ## end of start-up anyhow, so this should be fine 77 | library("utils", character.only = TRUE) 78 | 79 | ## Install packrat into local project library 80 | packratSrcPath <- list.files(full.names = TRUE, 81 | file.path("packrat", "src", "packrat") 82 | ) 83 | 84 | ## No packrat tarballs available locally -- try some other means of installation 85 | if (!length(packratSrcPath)) { 86 | 87 | message("> No source tarball of packrat available locally") 88 | 89 | ## There are no packrat sources available -- try using a version of 90 | ## packrat installed in the user library to bootstrap 91 | if (requireNamespace("packrat", quietly = TRUE) && packageVersion("packrat") >= "0.2.0.99") { 92 | message("> Using user-library packrat (", 93 | packageVersion("packrat"), 94 | ") to bootstrap this project") 95 | } 96 | 97 | ## Couldn't find a user-local packrat -- try finding and using devtools 98 | ## to install 99 | else if (requireNamespace("devtools", quietly = TRUE)) { 100 | message("> Attempting to use devtools::install_github to install ", 101 | "a temporary version of packrat") 102 | library(stats) ## for setNames 103 | devtools::install_github("rstudio/packrat") 104 | } 105 | 106 | ## Try downloading packrat from CRAN if available 107 | else if ("packrat" %in% rownames(available.packages())) { 108 | message("> Installing packrat from CRAN") 109 | install.packages("packrat") 110 | } 111 | 112 | ## Fail -- couldn't find an appropriate means of installing packrat 113 | else { 114 | stop("Could not automatically bootstrap packrat -- try running ", 115 | "\"'install.packages('devtools'); devtools::install_github('rstudio/packrat')\"", 116 | "and restarting R to bootstrap packrat.") 117 | } 118 | 119 | # Restore the project, unload the temporary packrat, and load the private packrat 120 | packrat::restore(prompt = FALSE, restart = TRUE) 121 | 122 | ## This code path only reached if we didn't restart earlier 123 | unloadNamespace("packrat") 124 | requireNamespace("packrat", lib.loc = libDir, quietly = TRUE) 125 | return(packrat::on()) 126 | 127 | } 128 | 129 | ## Multiple packrat tarballs available locally -- try to choose one 130 | ## TODO: read lock file and infer most appropriate from there; low priority because 131 | ## after bootstrapping packrat a restore should do the right thing 132 | if (length(packratSrcPath) > 1) { 133 | warning("Multiple versions of packrat available in the source directory;", 134 | "using packrat source:\n- ", shQuote(packratSrcPath)) 135 | packratSrcPath <- packratSrcPath[[1]] 136 | } 137 | 138 | 139 | lib <- file.path("packrat", "lib", R.version$platform, getRversion()) 140 | if (!file.exists(lib)) { 141 | dir.create(lib, recursive = TRUE) 142 | } 143 | lib <- normalizePath(lib, winslash = "/") 144 | 145 | message("> Installing packrat into project private library:") 146 | message("- ", shQuote(lib)) 147 | 148 | surround <- function(x, with) { 149 | if (!length(x)) return(character()) 150 | paste0(with, x, with) 151 | } 152 | 153 | ## The following is performed because a regular install.packages call can fail 154 | peq <- function(x, y) paste(x, y, sep = " = ") 155 | installArgs <- c( 156 | peq("pkgs", surround(packratSrcPath, with = "'")), 157 | peq("lib", surround(lib, with = "'")), 158 | peq("repos", "NULL"), 159 | peq("type", surround("source", with = "'")) 160 | ) 161 | installCmd <- paste(sep = "", 162 | "utils::install.packages(", 163 | paste(installArgs, collapse = ", "), 164 | ")") 165 | 166 | fullCmd <- paste( 167 | surround(file.path(R.home("bin"), "R"), with = "\""), 168 | "--vanilla", 169 | "--slave", 170 | "-e", 171 | surround(installCmd, with = "\"") 172 | ) 173 | system(fullCmd) 174 | 175 | ## Tag the installed packrat so we know it's managed by packrat 176 | ## TODO: should this be taking information from the lockfile? this is a bit awkward 177 | ## because we're taking an un-annotated packrat source tarball and simply assuming it's now 178 | ## an 'installed from source' version 179 | 180 | ## -- InstallAgent -- ## 181 | installAgent <- 'InstallAgent: packrat 0.4.8-1' 182 | 183 | ## -- InstallSource -- ## 184 | installSource <- 'InstallSource: source' 185 | 186 | packratDescPath <- file.path(lib, "packrat", "DESCRIPTION") 187 | DESCRIPTION <- readLines(packratDescPath) 188 | DESCRIPTION <- c(DESCRIPTION, installAgent, installSource) 189 | cat(DESCRIPTION, file = packratDescPath, sep = "\n") 190 | 191 | # Otherwise, continue on as normal 192 | message("> Attaching packrat") 193 | library("packrat", character.only = TRUE, lib.loc = lib) 194 | 195 | message("> Restoring library") 196 | restore(restart = FALSE) 197 | 198 | # If the environment allows us to restart, do so with a call to restore 199 | restart <- getOption("restart") 200 | if (!is.null(restart)) { 201 | message("> Packrat bootstrap successfully completed. ", 202 | "Restarting R and entering packrat mode...") 203 | return(restart()) 204 | } 205 | 206 | # Callers (source-erers) can define this hidden variable to make sure we don't enter packrat mode 207 | # Primarily useful for testing 208 | if (!exists(".__DONT_ENTER_PACKRAT_MODE__.") && interactive()) { 209 | message("> Packrat bootstrap successfully completed. Entering packrat mode...") 210 | packrat::on() 211 | } 212 | 213 | Sys.unsetenv("RSTUDIO_PACKRAT_BOOTSTRAP") 214 | 215 | } 216 | 217 | }) 218 | -------------------------------------------------------------------------------- /packrat/packrat.lock: -------------------------------------------------------------------------------- 1 | PackratFormat: 1.4 2 | PackratVersion: 0.4.8.1 3 | RVersion: 3.4.1 4 | Repos: CRAN=https://cloud.r-project.org 5 | 6 | Package: PKI 7 | Source: CRAN 8 | Version: 0.1-5.1 9 | Hash: 8c194fb34ebaab38a13e43ce84feedee 10 | Requires: base64enc 11 | 12 | Package: R6 13 | Source: CRAN 14 | Version: 2.2.2 15 | Hash: b2366cd9d2f3851a5704b4e192b985c2 16 | 17 | Package: RCurl 18 | Source: CRAN 19 | Version: 1.95-4.10 20 | Hash: 06af5153f969a90c6cd6c87ee57baa44 21 | Requires: bitops 22 | 23 | Package: RJSONIO 24 | Source: CRAN 25 | Version: 1.3-0 26 | Hash: fb672e20eb6f3010a3639f855d8ef6de 27 | 28 | Package: Rcpp 29 | Source: CRAN 30 | Version: 0.12.15 31 | Hash: a419463fc86b3ecc7338ae3ac3bbb268 32 | 33 | Package: XML 34 | Source: CRAN 35 | Version: 3.98-1.9 36 | Hash: 02079c4025bbfa0f63400cc0bb414952 37 | 38 | Package: assertthat 39 | Source: CRAN 40 | Version: 0.2.0 41 | Hash: e8805df54c65ac96d50235c44a82615c 42 | 43 | Package: base64enc 44 | Source: CRAN 45 | Version: 0.1-3 46 | Hash: c590d29e555926af053055e23ee79efb 47 | 48 | Package: bitops 49 | Source: CRAN 50 | Version: 1.0-6 51 | Hash: 67d0775189fd0041d95abca618c5c07e 52 | 53 | Package: cli 54 | Source: CRAN 55 | Version: 1.0.0 56 | Hash: f4239f89feb7ddc65821e4514e9734ae 57 | Requires: assertthat, crayon 58 | 59 | Package: crayon 60 | Source: CRAN 61 | Version: 1.3.4 62 | Hash: ff2840dd9b0d563fc80377a5a45510cd 63 | 64 | Package: digest 65 | Source: CRAN 66 | Version: 0.6.14 67 | Hash: 8017201df840795327c67825e98b52c1 68 | 69 | Package: htmltools 70 | Source: CRAN 71 | Version: 0.3.6 72 | Hash: f6fa1ff1db422a2c5227be30cab6c273 73 | Requires: Rcpp, digest 74 | 75 | Package: httpuv 76 | Source: CRAN 77 | Version: 1.3.5 78 | Hash: e84ff210704225e98da111beaa238472 79 | Requires: Rcpp 80 | 81 | Package: jsonlite 82 | Source: CRAN 83 | Version: 1.5 84 | Hash: 9c51936d8dd00b2f1d4fe9d10499694c 85 | 86 | Package: magrittr 87 | Source: CRAN 88 | Version: 1.5 89 | Hash: bdc4d48c3135e8f3b399536ddf160df4 90 | 91 | Package: mime 92 | Source: CRAN 93 | Version: 0.5 94 | Hash: 463550cf44fb6f0a2359368f42eebe62 95 | 96 | Package: packrat 97 | Source: CRAN 98 | Version: 0.4.8-1 99 | Hash: 6ad605ba7b4b476d84be6632393f5765 100 | 101 | Package: pillar 102 | Source: CRAN 103 | Version: 1.1.0 104 | Hash: 576bf712d620ca2b6ca76ca7b6cb5547 105 | Requires: cli, crayon, rlang, utf8 106 | 107 | Package: purrr 108 | Source: CRAN 109 | Version: 0.2.4 110 | Hash: a80b244ccd3fdb33a0ff2be3c50dd648 111 | Requires: magrittr, rlang, tibble 112 | 113 | Package: rlang 114 | Source: CRAN 115 | Version: 0.1.6 116 | Hash: 8eb4b2dce38b8f663cb9364b28eaa76b 117 | 118 | Package: rsconnect 119 | Source: CRAN 120 | Version: 0.8.5 121 | Hash: eeb742b99cb0b2b98545b0582d43a4b2 122 | Requires: PKI, RCurl, RJSONIO, digest, packrat, rstudioapi, yaml 123 | 124 | Package: rstudioapi 125 | Source: CRAN 126 | Version: 0.7 127 | Hash: e2ebaff8160aff3e6b32e6e78a693c2d 128 | 129 | Package: shiny 130 | Source: CRAN 131 | Version: 1.0.5 132 | Hash: 4d47532a807122bfd6bc3818310f9d2e 133 | Requires: R6, digest, htmltools, httpuv, jsonlite, mime, sourcetools, 134 | xtable 135 | 136 | Package: sourcetools 137 | Source: CRAN 138 | Version: 0.1.6 139 | Hash: 226d56d7469587da40b0f96180e711b4 140 | 141 | Package: stringi 142 | Source: CRAN 143 | Version: 1.1.6 144 | Hash: 4430faf2bcbe1b8de0d9be55bcfdcc0b 145 | 146 | Package: stringr 147 | Source: CRAN 148 | Version: 1.2.0 149 | Hash: 25a86d7f410513ebb7c0bc6a5e16bdc3 150 | Requires: magrittr, stringi 151 | 152 | Package: tibble 153 | Source: CRAN 154 | Version: 1.4.2 155 | Hash: 83895360ce4f8d2ce92eee00526b5b0b 156 | Requires: cli, crayon, pillar, rlang 157 | 158 | Package: utf8 159 | Source: CRAN 160 | Version: 1.1.3 161 | Hash: a2fe6a996668ee5850b7719f365e831b 162 | 163 | Package: xtable 164 | Source: CRAN 165 | Version: 1.8-2 166 | Hash: 7293235cfcc14cdff1ce7fd1a0212031 167 | 168 | Package: yaml 169 | Source: CRAN 170 | Version: 2.1.16 171 | Hash: 784ea5d8302d4a81f166a32a33c10711 172 | -------------------------------------------------------------------------------- /packrat/packrat.opts: -------------------------------------------------------------------------------- 1 | auto.snapshot: TRUE 2 | use.cache: FALSE 3 | print.banner.on.startup: auto 4 | vcs.ignore.lib: TRUE 5 | vcs.ignore.src: FALSE 6 | external.packages: 7 | local.repos: 8 | load.external.packages.on.startup: TRUE 9 | ignored.packages: 10 | quiet.package.installation: TRUE 11 | snapshot.recommended.packages: FALSE 12 | snapshot.fields: 13 | Imports 14 | Depends 15 | LinkingTo 16 | --------------------------------------------------------------------------------