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?
Your title asks about importing a data frame into shiny. That can be done by storing the data frame either as a binary file using the save() function or a csv file using write. csv() and having the shiny app read it in using load() for a binary file or read. csv() for a csv file.
Along with Shiny elements, you can use HTML elements to stylize your content in your application. In my opinion, R Shiny is very easy to learn despite how powerful the tool is. If you're working on a side project or looking to add something to your portfolio, I highly recommend trying it out.
Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. You can also extend your Shiny apps with CSS themes, htmlwidgets, and JavaScript actions.
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 ^^):
Edit the handsontable:
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
If you are looking for a solution that users can input their matrix data as in excel you could probably take a look at the package "shinySky" and more specifically to its component "Handsontable Input/Output". The relevant web address is: https://github.com/AnalytixWare/ShinySky.
Another similar solution would be the package shinyTable. You can find more information at https://github.com/trestletech/shinyTable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With