panels/G1_Code/2_code.panel-server.R

## rename  Date time  create variables missing to cate delete
## categorycal(rename + combine)
sep <- function(width = 80) {
  paste("##", paste(rep("-", width - 6), collapse = ""), "##")
}

output$code.panel <- renderUI({
  code.panel.ui(get.data.set())
})

output$r.show.code <- renderUI({
  tags$div(
    style = "min-width: 80%;
             white-space: nowrap;
             display: inline-block;",
    verbatimTextOutput("r.code.history")
  )
})

code.list <- reactive({
  do.call(c, lapply(code.save$variable, function(x) {
    return(x)
    y <- try(
      {
        iNZightTools::tidy_all_code(
          paste(x, collapse = "\n"),
          width = 80,
          indent = 4
        )
      },
      silent = TRUE
    )
    if (inherits(y, "try-error")) x else c(y, "\n")
  }))
})


packages.call <- reactive({
  package.load <- c("iNZightPlots", "magrittr")
  if (any(grepl("::", code.list()))) {
    m <- regexpr("[a-zA-Z0-9]+::", code.list()[grepl("::", code.list())])
    pkg <- substr(
      code.list()[grepl("::", code.list())], m,
      m + attr(m, "match.length") - 3
    )
    for (i in 1:length(pkg)) {
      if (!pkg[i] %in% package.load) package.load <- c(package.load, pkg[i])
    }
  }
  if (any(grepl("library\\([a-zA-Z0-9]+\\)", code.list()))) {
    m <- regexpr("library\\([a-zA-Z0-9]+\\)", code.list())
    pkg <- gsub(
      ".*library\\(|\\).*", "",
      substr(code.list(), m, m + attr(m, "match.length"))
    )
    for (i in 1:length(pkg)) {
      if (!pkg[i] %in% package.load) package.load <- c(package.load, pkg[i])
    }
  }
  package.load
})


output$r.code.history <- renderText({
  paste0(
    "# iNZight Code History\n",
    "\n",
    "## This script was automatically generated by iNZight Lite\n",
    "\n",
    "## Currently, the program can only generate code history for a few panels\n",
    "## We will gradually add this feature for more panels.\n",
    "\n",
    sep(),
    "\n",
    "## This script assumes you have various iNZight packages installed.\n",
    "## Uncomment the following lines if you don't:\n",
    "\n",
    paste0(sprintf(
      "# install.packages(c('%s'), ",
      paste(packages.call(), collapse = "',\n#                    '")
    ), "\n"),
    "#     repos = c('https://r.docker.stat.auckland.ac.nz',\n",
    "#               'https://cran.rstudio.com'))\n",
    "\n",
    sep(),
    "\n",
    "## Lite cannot access the local directory, so you have to set the the working\n## directory to the folder contains data if the data are imported.\n",
    "\n",
    sep(),
    "\n",
    "library(magrittr)  # enabled the pipe (%>%) operator",
    "\n",
    do.call(paste, c(as.list(code.list()), sep = ""))
  )
})
## initialize
keep.last <- reactive(TRUE)

tidy_assign_pipe <- function(code) {
  code <- gsub(
    "\ +", " ", # one or more spaces with just one space!
    paste(code, collapse = "")
  )
  code <- gsub("%>%", "%<>%", code)
  unlist(strsplit(gsub("(\\) %<>%)", "\\1\n~   ", code), "~"))
}
iNZightVIT/Lite documentation built on Sept. 3, 2024, 12:34 p.m.