Rshiny- save the results and talk to server

Hello, everyone,
here is an very urgent question for R-shiny:
I am very new to the functionality of R-shiny, could anyone let me know please if there is a way when I retrieve and display the data in the R-shiny , could I modify and save the data to the original sheet , or create a modified version with update time, then the next user could see the change?
I really hope my app can talk to background dataset...

Any similar experience?

I will so appreciate your help, that will be of great importance!
Rachel

Yes, it is possible, but it depends on how your app is deployed and where the data is being saved.

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one for a shiny app

Hey, Andresrcs,
thank you for the quick response, I will make a reprex as soon as possible, thanks!
Rachel

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)

Hi, Andrescrs,
I just uploaded the code,sorry for the late response ,it works well on my server before publishing to shinyapps.io. But after publishing, it shows nothing. I checked the logs, it says"Warning: Error in as.character: cannot coerce type 'closure' to vector of type 'character'" besides packages masked. I tried many ways to fix but fail.:frowning:

The logic is one editing table for editing, one history table that records any changes.
Any editing on column" salesmen" will be saved into table Rating;and if change the mailing zip, column"rating " in the table Rating(sorry, its naming is kind of confusing) will look up to the maketing map and update re-actively .

what I need is after publishing, the user can edit the salesmen to any names s/he needs, the work will be saved into global environment. Next user will be able to see the updated table and see the change in history.

Thank you Andresrcs or anyone who could help me with some ideas, that couldn't be more helpful!

Rachel

This might not work since depending on your account not all users are going to share the same R session and you are going to lose the environment content each time your app stops for inactivity.
Take a look into persistent data storage options when working with shinyapps.io

Thank you, this is really a good article!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.