Skip to content

Commit

Permalink
initial work on allowing editing of empty table
Browse files Browse the repository at this point in the history
  • Loading branch information
lbusett committed Mar 26, 2018
1 parent 6946868 commit 136eb41
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
128 changes: 128 additions & 0 deletions R/edit_table.R
@@ -0,0 +1,128 @@
library(rhandsontable)
library(shiny)
editTable <- function(DF, outdir=getwd(), outfilename="table"){
ui <- shinyUI(fluidPage(

titlePanel("Edit and save a table"),
sidebarLayout(
sidebarPanel(
helpText("Shiny app based on an example given in the rhandsontable package.",
"Right-click on the table to delete/insert rows.",
"Double-click on a cell to edit"),

wellPanel(
h3("Table options"),
radioButtons("useType", "Use Data Types", c("TRUE", "FALSE"))
),
br(),

wellPanel(
h3("Save table"),
div(class='row',
div(class="col-sm-6",
actionButton("save", "Save")),
div(class="col-sm-6",
radioButtons("fileType", "File type", c("ASCII", "RDS")))
)
)

),

mainPanel(
wellPanel(
uiOutput("message", inline=TRUE)
),

actionButton("cancel", "Cancel last action"),
br(), br(),

rHandsontableOutput("hot"),
br(),

wellPanel(
h3("Add a column"),
div(class='row',
div(class="col-sm-5",
uiOutput("ui_newcolname"),
actionButton("addcolumn", "Add")),
div(class="col-sm-4",
radioButtons("newcolumntype", "Type", c("integer", "double", "character"))),
div(class="col-sm-3")
)
)

)
)
))

server <- shinyServer(function(input, output) {

values <- reactiveValues()

## Handsontable
observe({
if (!is.null(input$hot)) {
values[["previous"]] <- isolate(values[["DF"]])
DF = hot_to_r(input$hot)
} else {
if (is.null(values[["DF"]]))
DF <- DF
else
DF <- values[["DF"]]
}
values[["DF"]] <- DF
})

output$hot <- renderRHandsontable({
DF <- values[["DF"]]
if (!is.null(DF))
rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all")
})

## Save
observeEvent(input$save, {
fileType <- isolate(input$fileType)
finalDF <- isolate(values[["DF"]])
if(fileType == "ASCII"){
dput(finalDF, file=file.path(outdir, sprintf("%s.txt", outfilename)))
}
else{
saveRDS(finalDF, file=file.path(outdir, sprintf("%s.rds", outfilename)))
}
}
)

## Cancel last action
observeEvent(input$cancel, {
if(!is.null(isolate(values[["previous"]]))) values[["DF"]] <- isolate(values[["previous"]])
})

## Add column
output$ui_newcolname <- renderUI({
textInput("newcolumnname", "Name", sprintf("newcol%s", 1+ncol(values[["DF"]])))
})
observeEvent(input$addcolumn, {
DF <- isolate(values[["DF"]])
values[["previous"]] <- DF
newcolumn <- eval(parse(text=sprintf('%s(nrow(DF))', isolate(input$newcolumntype))))
values[["DF"]] <- setNames(cbind(DF, newcolumn, stringsAsFactors=FALSE), c(names(DF), isolate(input$newcolumnname)))
})

## Message
output$message <- renderUI({
if(input$save==0){
helpText(sprintf("This table will be saved in folder \"%s\" once you press the Save button.", outdir))
}else{
outfile <- ifelse(isolate(input$fileType)=="ASCII", "table.txt", "table.rds")
fun <- ifelse(isolate(input$fileType)=="ASCII", "dget", "readRDS")
list(helpText(sprintf("File saved: \"%s\".", file.path(outdir, outfile))),
helpText(sprintf("Type %s(\"%s\") to get it.", fun, outfile)))
}
})

})

## run app
runApp(list(ui=ui, server=server))
return(invisible())
}
10 changes: 9 additions & 1 deletion R/insert_table.R
Expand Up @@ -90,7 +90,6 @@ insert_table = function(nrows = 1,
# If "Done" is clicked, create an empty data frame, then return
# the empty data frame along with the selected table format
out_df <- create_empty_df(input$nrows, input$ncols)

shiny::stopApp(returnValue = list(out_df, input$format))
})
shiny::observeEvent(input$cancel, {
Expand Down Expand Up @@ -158,6 +157,15 @@ insert_table = function(nrows = 1,

}



# library(shiny)
# library(rhandsontable)



aa = edit_table()

if (out_tbl[[2]] == "kable") {

output_table_str <-
Expand Down
20 changes: 20 additions & 0 deletions insert_table.Rproj
@@ -0,0 +1,20 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source

0 comments on commit 136eb41

Please sign in to comment.