Jonathan
Jonathan

Reputation: 641

Can you use R's native data editor to edit a csv from within Shiny?

Much like the edit function in R, I would like to manually make changes to a data frame from within Shiny. I have been to the shiny website

http://shiny.rstudio.com/gallery/datatables-options.html

however, I have not find a place where I can manually change data.

Upvotes: 3

Views: 1558

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

you can do what you want with shinysky package. It offer functionality to edit your tables on demand. Below is a simple example with mtcars dataset where you can change the table content and then download the updates you introduced during the session. You can easily add the file input to read .csv files yourself

rm(list = ls())
library(shiny)
library(shinydashboard)
library(shinysky)

ui <- dashboardPage(
  dashboardHeader(title = "How to edit a table"),
  dashboardSidebar(sidebarMenu(id = "tabs",menuItem("Menu Item 1", tabName = "one", icon = icon("dashboard"))
  )),
  dashboardBody(
    tabItems(tabItem(tabName = "one",hotable("hotable1"),downloadButton('downloadData', 'Download')))
    ))

server <- function(input, output) {

  previous <- reactive({mtcars})
  sample_data <- reactive({

    if(is.null(input$hotable1)){return(previous())}
    else if(!identical(previous(),input$hotable1))
    {
      sample_data <- as.data.frame(hot.to.df(input$hotable1))
      sample_data
    }
  })
  output$hotable1 <- renderHotable({sample_data()}, readOnly = F)
  output$downloadData <- downloadHandler(filename = function() {paste(Sys.time(), '- My New Table.csv', sep='') },content = function(file) {write.csv(sample_data(), file, row.names = FALSE)})
}
shinyApp(ui, server) 

enter image description here

The downloaded file looks like so:

enter image description here

Upvotes: 3

Related Questions