library(plyr)
library(dplyr)
library(DT)
library(shiny)
MarketMapping<- data.frame("id" = 1:7, "Rating" = c("Good", "Moderate", "Bad ", "Good", "Moderate", "Bad ", "Good"), "Zip" = c(99501, 99502, 99503, 99504, 99505, 99506, 99507))
Rating<-data.frame("id" = 1:7, "Account.Number"=c(345454, 31058, 10001, 3466256, 23526, 2646464, 26485),"Salesmen" = c("Rachel", "Ann", "Gege", "Jim ", "Kevin ", "Susan", "Cindy "),"Mailing.Zip" = c(99501, 99502, 99503, 99504, 99505, 99506, 99507),"Rating"=c("Good", "Moderate", "Bad ", "Good", "Moderate", "Bad ", "Good") )
Rating$Time.Stamp<-'1900-01-01'
Rating$Salesmen<-as.character(Rating$Salesmen)
df0<-Rating
render_dt = function(data, editable = 'cell', server = TRUE, ...) {
DT::renderDataTable(data, selection = 'none', server = server, filter = "top",options = list( pageLength = 5, autoWidth = TRUE), editable = editable, ...)
}
ui<-fluidPage((
pageWithSidebar(
headerPanel (headerPanel(title, windowTitle = title)),
sidebarPanel(
conditionalPanel(condition = "input.tabselected==1",
h5("Editing Panel"),
p(""),
br(),
)
,conditionalPanel(condition = "input.tabselected==2",
br()
)
)
,
mainPanel(
tabsetPanel(
tabPanel(tags$b("Editor Info"),value=1,
DT::dataTableOutput("x5"),
)
,tabPanel(tags$b("History Editting"),value=2,
tableOutput('history')
),
id="tabselected")
)
)
))
server<-shinyServer({
function(input, output, session) {
# server-side processing
output$x5 = render_dt(Rating, 'cell')
proxy5 = dataTableProxy('x5',deferUntilFlush = FALSE)
observeEvent(input$x5_cell_edit, {
info = input$x5_cell_edit
str(info) # check what info looks like (a data frame of 3 columns)
Rating <<- editData(Rating, info)
Rating[info$row,6]<<-as.character(as.POSIXct(as.numeric(Sys.time()), origin = '1970-01-01', tz = 'GMT'))
df0<<-rbind(Rating,df0)%>%unique()
Rating[info$row,5]<<-MarketMapping[which(as.integer(MarketMapping$Zip)==as.integer( Rating[info$row,4])),]$Rating
replaceData(proxy5, Rating, resetPaging = FALSE) # important
output$history<-renderTable({
df <-merge(df0,subset(count(df0$id),freq!=1), by.x="id", by.y="x",all.y = TRUE)%>%unique()
df
})
})
}
})
shinyApp(ui = ui, server = server)