Created
November 1, 2020 20:43
-
-
Save eonu/3e27c4c8ca9206cb2ddf3568f84cf47f to your computer and use it in GitHub Desktop.
viewr: Better R object inspection using the FireFox JSON viewer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ================================================================================= # | |
# viewr: Better R object inspection using the FireFox JSON viewer. # | |
# --------------------------------------------------------------------------------- # | |
# Usage: # | |
# Rscript viewr.R [FILE] # Where [FILE] has extension .Rdata or .rds # | |
# # | |
# This script will only work on macOS, and assumes that you have FireFox installed! # | |
# --------------------------------------------------------------------------------- # | |
# Edwin Onuonga (eonu) 2020 # | |
# ================================================================================= # | |
if (!('jsonlite' %in% rownames(installed.packages()))) { | |
install.packages('jsonlite', dependencies=TRUE, repos='http://cran.rstudio.com/') | |
} | |
suppressWarnings(suppressMessages(library(jsonlite))) | |
library(tools) | |
main <- function() { | |
args <- commandArgs(trailingOnly=TRUE) | |
if (length(args) == 1) { | |
ext <- tolower(file_ext(args[1])) | |
if (ext == 'rds') { | |
objects <- 'object' | |
object <- readRDS(args[1]) | |
} else if (ext == 'rdata') { | |
load(args[1]) | |
objects <- ls()[!(ls() %in% c('ext', 'args'))] | |
} else { | |
message('Invalid extension - expected .Rdata or .rds.') | |
quit(status=0) | |
} | |
if (length(objects) == 0) { | |
message('Unable to find any objects in the namespace.') | |
quit(status=0) | |
} else if (length(objects) == 1) { | |
if (ext == 'rds') { | |
name <- objects[1] | |
} else if (ext == 'rdata') { | |
name <- objects[1] | |
object <- get(name) | |
} | |
} else { | |
valid = FALSE | |
while (!valid) { | |
cat(sprintf("\033[1mFound %s objects. Which one to load?\033[0m\n", length(objects))) | |
for (i in 1:length(objects)) { | |
cat(sprintf(" %s: %s\n", i, objects[i])) | |
} | |
cat('Enter number or name: ') | |
input <- readLines('stdin', n=1) | |
cat('\n') | |
if (input %in% objects) { | |
name <- input | |
object <- get(name) | |
valid = TRUE | |
} else if (!grepl('\\D', input)) { | |
idx <- as.integer(input) | |
if (idx <= length(objects)) { | |
name <- objects[idx] | |
object <- get(name) | |
valid = TRUE | |
} else { | |
message(sprintf('Invalid input - expected a number from 1 to %s.', length(objects))) | |
} | |
} else { | |
message('Invalid input.') | |
} | |
} | |
} | |
json <- toJSON(object, pretty=TRUE) | |
filename <- sprintf('.%s.json', name) | |
conn <- file(filename) | |
writeLines(json, conn) | |
close(conn) | |
tryCatch({ | |
system(sprintf('open -a Firefox %s', filename)) | |
cat('Press enter to end: ') | |
invisible(readLines('stdin', n=1)) | |
}, error = function(e) { | |
# TODO | |
}, finally = { | |
if (file.exists(filename)) file.remove(filename) | |
}) | |
} else { | |
cat('\033[4mviewr\033[0m: Better R object inspection using the FireFox JSON viewer.\n\n') | |
cat('\033[1mUsage\033[0m:\n') | |
cat(' Rscript viewr.R [FILE] \033[37m#\033[0m \033[90mWhere [FILE] has extension .Rdata or .rds\033[0m\n') | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment