| 1 |
#' Edit a Data Frame. |
| 2 |
#' |
| 3 |
#' Interactively edit a \code{data.frame} or \code{data.table}. The resulting |
| 4 |
#' code will be emitted as a call to reload the data from a temp RDS file. |
| 5 |
#' |
| 6 |
#' This addin can be used to interactively edit. The intended way to use this is |
| 7 |
#' as follows: |
| 8 |
#' |
| 9 |
#' 1. Highlight a symbol naming a \code{data.frame} or \code{data.table} in your |
| 10 |
#' R session, e.g. \code{mtcars}. 2. Execute this addin, to interactively edit |
| 11 |
#' it. |
| 12 |
#' |
| 13 |
#' When you're done, the code performing this operation will be emitted at the |
| 14 |
#' cursor position. |
| 15 |
#' |
| 16 |
#' This function borrows heavily from |
| 17 |
#' \href{https://github.com/rstudio/addinexamples/blob/master/R/subsetAddin.R}{rstudio/addinexamples/subsetAddin} |
| 18 |
#' |
| 19 |
#' @export |
| 20 |
editAddin <- function() { |
| 21 |
|
| 22 |
# Get the document context. |
| 23 |
context <- rstudioapi::getActiveDocumentContext() |
| 24 |
|
| 25 |
# Set the default data to use based on the selection. |
| 26 |
text <- context$selection[[1]]$text |
| 27 |
defaultData <- text |
| 28 |
|
| 29 |
# create a temp file |
| 30 |
fname = gsub("\\\\", "/", tempfile()) |
| 31 |
|
| 32 |
# Generate UI for the gadget. |
| 33 |
ui <- miniUI::miniPage( |
| 34 |
miniUI::gadgetTitleBar("Edit a data.frame"), |
| 35 |
miniUI::miniContentPanel( |
| 36 |
stableColumnLayout( |
| 37 |
shiny::textInput("data", "Data", value = defaultData), |
| 38 |
shiny::radioButtons("outType", "Output type", |
| 39 |
choices = c("Update original data" = "update", |
| 40 |
"Print updates to script (no update)" = "print") |
| 41 |
) |
| 42 |
), |
| 43 |
shiny::uiOutput("pending"), |
| 44 |
rHandsontableOutput("hot") |
| 45 |
) |
| 46 |
) |
| 47 |
|
| 48 |
# Server code for the gadget. |
| 49 |
server <- function(input, output, session) { |
| 50 |
values = shiny::reactiveValues() |
| 51 |
setHot = function(x) values[["hot"]] = x |
| 52 |
|
| 53 |
reactiveData <- shiny::reactive({ |
| 54 |
|
| 55 |
# Collect inputs. |
| 56 |
dataString <- input$data |
| 57 |
|
| 58 |
# Check to see if there is data called 'data', |
| 59 |
# and access it if possible. |
| 60 |
if (!nzchar(dataString)) |
| 61 |
return(errorMessage("data", "No dataset available.")) |
| 62 |
|
| 63 |
if (!exists(dataString, envir = .GlobalEnv)) |
| 64 |
return(errorMessage("data", paste("No dataset named '", dataString, "' available."))) |
| 65 |
|
| 66 |
data <- get(dataString, envir = .GlobalEnv) |
| 67 |
|
| 68 |
data |
| 69 |
}) |
| 70 |
|
| 71 |
output$pending <- shiny::renderUI({ |
| 72 |
data <- reactiveData() |
| 73 |
if (isErrorMessage(data)) |
| 74 |
htmltools::h4(style = "color: #AA7732;", data$message) |
| 75 |
}) |
| 76 |
|
| 77 |
output$hot <- renderRHandsontable({ |
| 78 |
data <- reactiveData() |
| 79 |
if (isErrorMessage(data)) |
| 80 |
return(NULL) |
| 81 |
|
| 82 |
if (is.null(input$hot)) |
| 83 |
DF = data |
| 84 |
else |
| 85 |
DF = hot_to_r(input$hot) |
| 86 |
|
| 87 |
setHot(DF) |
| 88 |
rhandsontable(DF) %>% |
| 89 |
hot_table(highlightCol = TRUE, highlightRow = TRUE) |
| 90 |
}) |
| 91 |
|
| 92 |
# Listen for 'done'. |
| 93 |
shiny::observeEvent(input$done, { |
| 94 |
|
| 95 |
if (input$outType == "print") { |
| 96 |
rslt <- capture.output(dput(values[["hot"]])) |
| 97 |
rstudioapi::insertText(Inf, paste0(input$data, " = ", |
| 98 |
paste(rslt, collapse = "\n"))) |
| 99 |
} else { |
| 100 |
if (nzchar(input$data) && !is.null(values[["hot"]])) { |
| 101 |
saveRDS(values[["hot"]], fname) |
| 102 |
code <- paste(input$data, " = readRDS('", fname, "')", sep = "") |
| 103 |
rstudioapi::sendToConsole(code) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
invisible(shiny::stopApp()) |
| 108 |
}) |
| 109 |
} |
| 110 |
|
| 111 |
# Use a modal dialog as a viewr. |
| 112 |
viewer <- shiny::dialogViewer("Edit", width = 1000, height = 800) |
| 113 |
shiny::runGadget(ui, server, viewer = viewer) |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
# these functions come from rstudio/addinexamples |
| 118 |
stableColumnLayout <- function(...) { |
| 119 |
dots <- list(...) |
| 120 |
n <- length(dots) |
| 121 |
width <- 12 / n |
| 122 |
class <- sprintf("col-xs-%s col-md-%s", width, width) |
| 123 |
shiny::fluidRow( |
| 124 |
lapply(dots, function(el) { |
| 125 |
shiny::div(class = class, el) |
| 126 |
}) |
| 127 |
) |
| 128 |
} |
| 129 |
|
| 130 |
isErrorMessage <- function(object) { |
| 131 |
inherits(object, "error_message") |
| 132 |
} |
| 133 |
|
| 134 |
errorMessage <- function(type, message) { |
| 135 |
structure( |
| 136 |
list(type = type, message = message), |
| 137 |
class = "error_message" |
| 138 |
) |
| 139 |
} |