Updating SelectInput,TextInput,Date in the modal window to previously set values if any for each row

725 views
Skip to first unread message

Sanjay Jayaraman

unread,
Oct 21, 2016, 12:34:44 AM10/21/16
to Shiny - Web Framework for R

I am currently working on a scenario where I have created a dashboard which has a datatable. The datatable has a click button which opens up a modal window pertaining to that row in the table. The modal window has a set of dropdowns, textinputs and dates that will be updated into the DB(again for that row alone) once the Save button in the modal window is clicked.

Is it possible to retrieve the previously saved values for the dropdowns,textinputs and dates if any and display them in the same dropdowns,text inputs and dates for each row?

Thanks in advance for helping out.


I am attaching my ui and server code.


ui.R


ui <- dashboardPage(
  dashboardHeader(title = "Simple App"),
  dashboardSidebar(
    sidebarMenu(id = "tabs",
                menuItem("Menu Item 1", tabName = "one", icon = icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "one",h2("Datatable Modal Popup"),
              DT::dataTableOutput('my_table'),uiOutput("popup")
      )
    )
  )
)

server.R
server <- function(input, output, session) {
  
 
  
  samplevaluedata<-c("","Prime","Optimus")  ##add source data here

  shinyInput <- function(FUN, len, id, ...) {inputs <- character(len)
  for (i in seq_len(len)) {
    inputs[i] <- as.character(FUN(paste0(id, i), ...))}
  inputs
  }
  
  my_data <- reactive({
    a = dbGetQuery(hcltcprod,paste0("select name,mobile,email,assignedto,id from public.tempnew order by 3;"))
    as.data.frame(cbind(View = shinyInput(actionButton, nrow(a),'button_', label = "View", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),a))
  })  
  output$my_table <- DT::renderDataTable(my_data(),selection = 'single',options = list(searching = FALSE,pageLength = 10),server = FALSE, escape = FALSE,rownames= FALSE)
  
  # Here I created a reactive to save which row was clicked which can be stored for further analysis
  SelectedRow <- eventReactive(input$select_button,{
    as.numeric(strsplit(input$select_button, "_")[[1]][2])
  })
  
  # This is needed so that the button is clicked once for modal to show, a bug reported here
  # https://github.com/ebailey78/shinyBS/issues/57
  observeEvent(input$select_button, {
    toggleModal(session, "modalExample", "open")
  })
  
  DataRow <- eventReactive(input$select_button,{
    as.data.frame(my_data()[SelectedRow(),6])
  })
  
  output$popup <- renderUI({
    print(DataRow())
    bsModal("modalExample", paste0("Data for Row Number: ",SelectedRow()), "", size = "large",
            #column(width=12,DT::renderDataTable(DataRow())),
            column(width=6,selectInput("samplevalue","Select Custom Source*",choices=c("Please select",samplevaluedata))),
            column(width=6,textInput("sampletext",label = "Enter Text",value = NULL,placeholder = NULL)),
            actionButton("openpage","Open"),
            actionButton("savepage","Save"),
            DT::dataTableOutput("t1"))
  })

 tttt <- reactive({
   if(input$openpage==0)
     return()
   isolate({
     a = dbGetQuery(hcltcprod,paste0("select * from public.tempnew where id in (",DataRow(),");"))
     a
   })
 })
 
 output$t1 <- DT::renderDataTable(tttt())

 observeEvent(input$savepage,{
     a = dbGetQuery(hcltcprod,paste0("update public.tempnew set s_text='",input$sampletext,"',s_value='",input$samplevalue,"' where id in (",DataRow(),");"))
     #a = dbGetQuery(hcltcprod,paste0("insert into public.tempnew values ('Sandeep','1234567891','s...@gmail.com','Bat',getdate(),'Prime','Number');"))
 })
}

Bárbara Borges

unread,
Oct 26, 2016, 6:49:18 AM10/26/16
to Shiny - Web Framework for R
"Is it possible to retrieve the previously saved values for the dropdowns,textinputs and dates if any and display them in the same dropdowns,text inputs and dates for each row?"

You mean you choose some values in the inputs for row A, hit save, then open the modal for row B and you want to see which were the values you used for row A?
Or that you choose some values in the inputs for row A, hit save, then open the modal for row A again and want to see the the values you chose previously for row A?

In either case, I think you should create a dataframe that stores each values specified for that row (and NULL if no value was specified). Does that make sense?

Sanjay Jayaraman

unread,
Nov 1, 2016, 12:59:06 AM11/1/16
to Shiny - Web Framework for R
Yeah thanks, that's how it is supposed to be done. 
Reply all
Reply to author
Forward
0 new messages