Reputation: 2322
I load a csv file as data frame in a shinyTable. How can I add a button which saves changed edits in the shinyTable?
Here are the current files
server.R
library(shiny)
library(shinyTable)
shinyServer(function(input, output, session) {
cachedTbl <- NULL
output$tbl <- renderHtable({
if (is.null(input$tbl)){
setwd("~/projects/shinyTable")
datafile <- read.csv("data.csv", header=TRUE, sep=",", quote="")
tbl <- datafile
cachedTbl <<- tbl
print(tbl)
return(tbl)
} else{
cachedTbl <<- input$tbl
print(input$tbl)
return(input$tbl)
}
})
})
ui.R
library(shiny)
library(shinyTable)
shinyUI(pageWithSidebar(
headerPanel("Simple Shiny Table!"),
sidebarPanel(
helpText(HTML("A simple editable matrix.
<p>Created using <a href = \"http://github.com/trestletech/shinyTable\">shinyTable</a>."))
),
mainPanel(
htable("tbl", colHeaders="provided"),
actionButton("actionButtonID","update data file")
)
))
I added the button, but so far it has no functionality. In a later iteration I also want to add a side menu with fields which allows to add a new line to the data frame (which can also saved to the csv file).
Any ideas how to perform this?
Upvotes: 1
Views: 624
Reputation: 5600
I've achieved a similar thing with an app I've made. I did it by creating a function to save the data. I'm assuming tbl is a new line of data to be added to the existing csv "data.csv", is that right?
So create a function, something like:
saveData <- function( tbl ) {
setwd("~/projects/shinyTable")
datafile <- read.csv("data.csv", header=TRUE, sep=",", quote="")
datafile <- rbind( datafile, tbl )
write.csv( datafile, file = "data.csv" )
}
Then activate that function on a button press, using an observeEvent to detect it:
observeEvent( input$actionButtonID, {
saveData( input$tbl )
}
I haven't used htable before, so I'm not sure about the data call "input$tbl", but it seems to me like that should work.
Can you arrange that into your app?
Upvotes: 1