How to change a value of a dataTable when it is clicked?

1

I'm doing some tests with shiny and I've created a data.frame for the messages as if it were an inbox .

I show the messages both in notifications and in a library table DT . I would like that when you click on them in the table change their value read to TRUE

I leave you what I've been trying:

Data frame messages

from <- c("A","B","C")
content <- c("Mensaje 1","Mensaje2","Mensaje leido")
leido <- c(FALSE,FALSE,TRUE)
messages <- data.frame(from,content,leido)

DT :: datatableoutput of messages

output$tablaMensajes <- DT::renderDataTable({
    messages
})

Message notifications

output$mensajes <- renderMenu({
    if(! is.null(input$tablaMensajes_rows_selected)){
        s<-input$tablaMensajes_rows_selected
        messages[s,"leido"] <- TRUE
    }
    msgs <- apply(messages[which(messages$leido == FALSE),], 1, function(row) {
        messageItem(from = row[["from"]], message = row[["content"]],href = paste0("javascript:mensaje('",row[["content"]],"')"))
    })
     dropdownMenu(type = "messages", .list = msgs)
})

It is in the message notifications where I check if it has been pressed and if so it changes its value read to TRUE , the problem is that it is not saved in the data frame as TRUE .

    
asked by Lombarda Arda 01.03.2017 в 08:51
source

1 answer

1

Create an event observe and assign with <<- to save the change

I changed the code a bit since there was a bug that when there were no more messages left, I gave you the following error:

  

Error: subscript out of bounds

    output$mensajes <- renderMenu({
    if(nrow(messages[which(messages$leido == FALSE),]) >0) {
        msgs <- apply(messages[which(messages$leido == FALSE),], 1, function(row) {
            messageItem(from = row[["from"]], message = row[["content"]],href = paste0("javascript:mensaje('",row[["content"]],"')"))
        }) 
    }else{
        msgs = NULL
    }

    dropdownMenu(type = "messages", .list = msgs)
})

output$tablaMensajes <- DT::renderDataTable({
    messages
})
observe({
    if(! is.null(input$tablaMensajes_rows_selected)){
        #browser()
        messages
        s<-input$tablaMensajes_rows_selected
        messages[s,"leido"] <<- TRUE
        output$mensajes <- renderMenu({
            if(nrow(messages[which(messages$leido == FALSE),]) >0) {
                msgs <- apply(messages[which(messages$leido == FALSE),], 1, function(row) {
                    messageItem(from = row[["from"]], message = row[["content"]],href = paste0("javascript:mensaje('",row[["content"]],"')"))
                })
            }else{
                msgs = NULL
            }
            dropdownMenu(type = "messages", .list = msgs)
        })


        output$tablaMensajes <- DT::renderDataTable({
            messages
        })


    }
})
    
answered by 01.03.2017 / 12:46
source