R 在数据表中单击时更改数据帧的值

R 在数据表中单击时更改数据帧的值,r,dataframe,shiny,shinydashboard,dt,R,Dataframe,Shiny,Shinydashboard,Dt,我试着在R中制作一些类似的收件箱。我有一个消息的数据框,我在DT库的表格和shinydashboard库的messageItem中显示它。我想当你点击表中的一条消息时,将“leido”值改为TRUE 我有这个密码 数据帧 from <- c("A","B","C") content <- c("Mensaje 1","Mensaje2","Mensaje leido") leido <- c(FALSE,FALSE,TRUE) messages <- data.frame

我试着在R中制作一些类似的收件箱。我有一个消息的数据框,我在
DT
库的表格和
shinydashboard
库的
messageItem
中显示它。我想当你点击表中的一条消息时,将“leido”值改为TRUE

我有这个密码

数据帧

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

中的
我已修改了您的代码。我猜这就是你想要的:

    library(shiny)
    library(shinydashboard)
    library(DT)

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

    ui <- dashboardPage(
      dashboardHeader(title = "Dynamic sidebar"),
      dashboardSidebar(
        sidebarMenuOutput("mensajes")
      ),
      dashboardBody( DT::dataTableOutput("tablaMensajes"))
    )


    server = function(input, output, session){

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


      output$mensajes <- renderMenu({
        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)
      })

      observe({
        if(! is.null(input$tablaMensajes_rows_selected)){
          #browser()
          messages
          s<-input$tablaMensajes_rows_selected
          messages[s,"leido"] <<- TRUE
          output$mensajes <- renderMenu({
            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)
          })


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


        }
      })

    }

    shinyApp(ui,server)


**[EDIT]:**

To remove subscript out of bound error I have edited the above code to add conditions if the no rows with false value is present then the message should be be empty. 

library(shiny)
library(shinydashboard)
library(DT)

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

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic sidebar"),
  dashboardSidebar(
    sidebarMenuOutput("mensajes")
  ),
  dashboardBody( DT::dataTableOutput("tablaMensajes"))
)


server = function(input, output, session){

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


  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)
  })

  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
      })


    }
  })

}

shinyApp(ui,server)
库(闪亮)
图书馆(shinydashboard)
图书馆(DT)

从我修改了你的代码。我猜这就是你想要的:

    library(shiny)
    library(shinydashboard)
    library(DT)

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

    ui <- dashboardPage(
      dashboardHeader(title = "Dynamic sidebar"),
      dashboardSidebar(
        sidebarMenuOutput("mensajes")
      ),
      dashboardBody( DT::dataTableOutput("tablaMensajes"))
    )


    server = function(input, output, session){

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


      output$mensajes <- renderMenu({
        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)
      })

      observe({
        if(! is.null(input$tablaMensajes_rows_selected)){
          #browser()
          messages
          s<-input$tablaMensajes_rows_selected
          messages[s,"leido"] <<- TRUE
          output$mensajes <- renderMenu({
            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)
          })


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


        }
      })

    }

    shinyApp(ui,server)


**[EDIT]:**

To remove subscript out of bound error I have edited the above code to add conditions if the no rows with false value is present then the message should be be empty. 

library(shiny)
library(shinydashboard)
library(DT)

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

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic sidebar"),
  dashboardSidebar(
    sidebarMenuOutput("mensajes")
  ),
  dashboardBody( DT::dataTableOutput("tablaMensajes"))
)


server = function(input, output, session){

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


  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)
  })

  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
      })


    }
  })

}

shinyApp(ui,server)
库(闪亮)
图书馆(shinydashboard)
图书馆(DT)

从它的工作原理很好,但当我没有读到消息项返回此错误时,
error:subscript out-bounds
我已编辑代码以删除错误。出现错误是因为带有false的行数将为零。它可以正常工作,但当我没有未读取消息项就返回此错误时,
error:subscript out bounds
我已编辑代码以删除错误。出现错误是因为带有false的行数将为零。
observe({
    if(! is.null(input$tablaMensajes_rows_selected)){
        messages
        s<-input$tablaMensajes_rows_selected
        messages[s,"leido"] = TRUE
        output$mensajes <- renderMenu({
        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)
        })


    }
})
    library(shiny)
    library(shinydashboard)
    library(DT)

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

    ui <- dashboardPage(
      dashboardHeader(title = "Dynamic sidebar"),
      dashboardSidebar(
        sidebarMenuOutput("mensajes")
      ),
      dashboardBody( DT::dataTableOutput("tablaMensajes"))
    )


    server = function(input, output, session){

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


      output$mensajes <- renderMenu({
        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)
      })

      observe({
        if(! is.null(input$tablaMensajes_rows_selected)){
          #browser()
          messages
          s<-input$tablaMensajes_rows_selected
          messages[s,"leido"] <<- TRUE
          output$mensajes <- renderMenu({
            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)
          })


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


        }
      })

    }

    shinyApp(ui,server)


**[EDIT]:**

To remove subscript out of bound error I have edited the above code to add conditions if the no rows with false value is present then the message should be be empty. 

library(shiny)
library(shinydashboard)
library(DT)

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

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic sidebar"),
  dashboardSidebar(
    sidebarMenuOutput("mensajes")
  ),
  dashboardBody( DT::dataTableOutput("tablaMensajes"))
)


server = function(input, output, session){

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


  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)
  })

  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
      })


    }
  })

}

shinyApp(ui,server)