在数据表中单击时更改数据框的值
Change the value of a data frame when is clicked in a datatable
我正在尝试在 R 中创建一些类似的收件箱。我有一个消息数据框,我将其显示在 DT
库的 table 和 messageItem
中 shinydashboard
图书馆。我希望当您单击 table 中的消息时,将 "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)
DT::datatable消息输出
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)){
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)
希望对您有所帮助!
我正在尝试在 R 中创建一些类似的收件箱。我有一个消息数据框,我将其显示在 DT
库的 table 和 messageItem
中 shinydashboard
图书馆。我希望当您单击 table 中的消息时,将 "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)
DT::datatable消息输出
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)){
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)
希望对您有所帮助!