I am trying to create a shiny dashboard with few buttons (save, edit, download and view)
I did not create a button for edit. Instead, I used the following code,
I created a proxy table for the edits to save temporarily because without proxy tables I do not know to capture the values being edited
I created a save button to save the edits permanently.
library(shiny)
library(shinydashboard)
library(DT)
library(readxl)
library(shinyWidgets)
ui <- dashboardPage(
dashboardHeader(title = "Validation Report"),
dashboardSidebar(
sidebarMenu(
menuItem("Conversions",tabName = "conversions",icon = icon("check"))
)
),
dashboardBody(
tabItem(tabName = "conversions",
h2("sample data"),
DT::dataTableOutput("sample_data"),
br(),
actionButton("viewBtn","View"),
br(),
verbatimTextOutput(outputId = "res1"),
tags$br(),
actionButton(
inputId = "saveBtn",
label = "Save"),
downloadButton("download1", "Download CSV"), # no label: this button will be hidden
br(),
DT::dataTableOutput("updated.df")
)
)
)
server <- function(input,output,session) {
d1 <- read.csv("work.csv")
output$sample_data <- DT::renderDataTable({
DT::datatable(d1,
editable = list(target = 'cell',
disable = list(columns = c(0,1)))
)
})
output$download1 <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
#d1$Value <- format(d1$Value,big.mark = ",")
write.csv(d1, file)
}
)
proxy5 = dataTableProxy('sample_data')
observeEvent(input$sample_data_cell_edit, {
info = input$sample_data_cell_edit
#str(info) # check what info looks like (a data frame of 3 columns)
observeEvent(input$myconfirmation2, {
if (isTRUE(input$myconfirmation2)) {
d1 <<- editData(d1, info)
replaceData(proxy5, d1, resetPaging = FALSE)
unlink("work.csv")
write.csv(d1,'work.csv')
}
else
{
last_df <- read.csv("work.csv")
output$sample_data <- DT::renderDataTable({
DT::datatable(last_df,
editable = list(target = 'cell',
disable = list(columns = c(0,1)))
)
})
}
})
})
view_fun<-eventReactive(input$myconfirmation2,{
if(is.null(input$myconfirmation2)||input$myconfirmation2==0)
{
returnValue()
}
else
{
DT::datatable(d1,selection = 'none')
}
})
observeEvent(input$myconfirmation2, {
if (isTRUE(input$myconfirmation2)) {
output$res1 <- renderText(invisible("Saved Successfully"))
}
else {
output$res1 <- renderText(invisible(" Unsuccessfully"))
}
})
observeEvent(input$saveBtn, {
confirmSweetAlert(
session = session,
inputId = "myconfirmation2",
type = "warning",
title = "Are you sure you want to save?",
btn_labels = c("No", "Yes"),
btn_colors = c("#FE642E", "#04B404")
)
})
observeEvent(input$myconfirmation2, {
if (isTRUE(input$myconfirmation2)) {
write.csv(d1,'work.csv')
}
else
{
last_df <- read.csv("work.csv")
output$sample_data <- DT::renderDataTable({
DT::datatable(last_df,
editable = list(target = 'cell',
disable = list(columns = c(0,1)))
)
})
}
})
output$updated.df<-renderDataTable({
view_fun()
}
)
}
shinyApp(ui,server)