Data input via shinyTable in R shiny application

Mahmoud picture Mahmoud · Mar 8, 2014 · Viewed 31.3k times · Source

I want to build a shiny app that gets matrix data as input and returns a table based on some operations on it as output. By search I find that ShinyTable package could be useful. I tried below shiny codes but the result app appears gray out and without result.

library(shinyTable)
shiny::runApp(list(
  ui=pageWithSidebar(
    headerPanel('Simple matrixInput')
    ,
    sidebarPanel(
      htable("tbl")
      ,
      submitButton("OK")
    )
    ,
    mainPanel(

      tableOutput(outputId = 'table.output')
    ))
  ,
  server=function(input, output){
    output$table.output <- renderTable({
      input$tbl^2
    }
    , sanitize.text.function = function(x) x 
    )
  }
))

Any Idea?

Answer

St&#233;phane Laurent picture Stéphane Laurent · Aug 15, 2016

The shinyTable package has been greatly improved in the rhandsontable package.

Here is a minimal function that takes a data frame and runs a shiny app allowing to edit it and to save it in a rds file:

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"), 
          actionButton("save", "Save table")
        )        

      ),

      mainPanel(

        rHandsontableOutput("hot")

      )
    )
  ))

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

    values <- reactiveValues()

    ## Handsontable
    observe({
      if (!is.null(input$hot)) {
        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, {
      finalDF <- isolate(values[["DF"]])
      saveRDS(finalDF, file=file.path(outdir, sprintf("%s.rds", outfilename)))
    })

  })

  ## run app 
  runApp(list(ui=ui, server=server))
  return(invisible())
}

For example, take the following data frame:

> ( DF <- data.frame(Value = 1:10, Status = TRUE, Name = LETTERS[1:10],
                    Date = seq(from = Sys.Date(), by = "days", length.out = 10),
                    stringsAsFactors = FALSE) )
   Value Status Name       Date
1      1   TRUE    A 2016-08-15
2      2   TRUE    B 2016-08-16
3      3   TRUE    C 2016-08-17
4      4   TRUE    D 2016-08-18
5      5   TRUE    E 2016-08-19
6      6   TRUE    F 2016-08-20
7      7   TRUE    G 2016-08-21
8      8   TRUE    H 2016-08-22
9      9   TRUE    I 2016-08-23
10    10   TRUE    J 2016-08-24

Run the app and have fun (especially with the calendars ^^):

enter image description here

Edit the handsontable:

enter image description here

Click on the Save button. It saves the table in the file table.rds. Then read it in R:

> readRDS("table.rds")
   Value Status    Name       Date
1   1000  FALSE Mahmoud 2016-01-01
2   2000  FALSE       B 2016-08-16
3      3  FALSE       C 2016-08-17
4      4   TRUE       D 2016-08-18
5      5   TRUE       E 2016-08-19
6      6   TRUE       F 2016-08-20
7      7   TRUE       G 2016-08-21
8      8   TRUE       H 2016-08-22
9      9   TRUE       I 2016-08-23
10    10   TRUE       J 2016-08-24