单击 r 中的操作按钮时的自定义消息
Custom Message upon clicking action button in r
在 shiny 中单击操作按钮后,如何获得自定义消息(该消息包括一个迷你 table,具体取决于反应值)?这是我的代码。该消息应包含反应式 table,我将其称为 rows()。
library(shiny)
library(datasets)
library(rhandsontable)
library(data.table)
my_message= writeLines("validation is not successful. \nCheck following
commodities:\nCPCCode Commodity Year")
# This is the message I want when action button is clicked
# validation is not successful. Please check the following commodities:
#CPCCode Commodity Year
# 100 Maize 2010
# 200 Rice 2015
# 300 Tea 2016
# 400 Banana 2014
#.
#.
#.
# and so on. The table can be upto max 50 rows
ui = fluidPage(
actionButton("message", "message")
)
server = function(input,output,session){
rows= reactive({
d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010)
d$Commodity = as.character(d$Commodity)
d=rbind(d, c(200, "Rice", "2015"))
d=rbind(d, c(300,"Tea", 2016))
})
observeEvent(input$message,{
if (is.null(input$message) || input$message == 0){return()}
isolate({
input$message
the_message <- paste("validation is not successful")
js_string <- 'alert("SOMETHING");'
js_string <- sub("SOMETHING",the_message,js_string)
session$sendCustomMessage(type='jsCode', list(value = js_string))
})
})
}
shinyApp(ui,server)
已编辑。它仅显示 table 的第一行。如有任何建议,我们将不胜感激
library(shiny)
library(datasets)
library(rhandsontable)
library(data.table)
my_message= writeLines("validation is not successful. \nCheck following
commodities:\nCPCCode Commodity Year")
# This is the message I want when action button is clicked
# validation is not successful. Please check the following commodities:
#CPCCode Commodity Year
# 100 Maize 2010
# 200 Rice 2015
# 300 Tea 2016
# 400 Banana 2014
#.
#.
#.
# and so on. The table can be upto max 50 rows
ui = fluidPage(
actionButton("show", "Show modal dialog")
)
server = function(input, output) {
rows= reactive({
d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010)
d$Commodity = as.character(d$Commodity)
d=rbind(d, c(200, "Rice", "2015"))
d=rbind(d, c(300,"Tea", 2016))
})
# Return the UI for a modal dialog with data selection input. If 'failed'
is
# TRUE, then display a message that the previous value was invalid.
dataModal <- function(failed = FALSE) {
modalDialog(
span("Validation is not successful", rows()),
footer = tagList(
actionButton("ok", "OK")
)
)
}
observeEvent(input$show, {
showModal(dataModal())
})
observeEvent(input$ok, {
removeModal()
})
}
shinyApp(ui,server)
要在 modalDialog
中显示 table,您需要在 modalDialog
中添加一个 dataTableOutput
,并在 modalDialog
之后渲染 table =] 被创建。我已经编辑了你的服务器代码来做同样的事情。希望对你有帮助。
server = function(input, output) {
rows= reactive({
d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010)
d$Commodity = as.character(d$Commodity)
d=rbind(d, c(200, "Rice", "2015"))
d=rbind(d, c(300,"Tea", 2016))
})
# Return the UI for a modal dialog with data selection input. If 'failed'
# is
# TRUE, then display a message that the previous value was invalid.
dataModal <- function(failed = FALSE) {
modalDialog(
span("Validation is not successful"),
dataTableOutput("table"),
footer = tagList(
actionButton("ok", "OK")
)
)
}
observeEvent(input$show, {
showModal(dataModal())
output$table <- renderDataTable({rows()})
})
observeEvent(input$ok, {
removeModal()
})
}
在 shiny 中单击操作按钮后,如何获得自定义消息(该消息包括一个迷你 table,具体取决于反应值)?这是我的代码。该消息应包含反应式 table,我将其称为 rows()。
library(shiny)
library(datasets)
library(rhandsontable)
library(data.table)
my_message= writeLines("validation is not successful. \nCheck following
commodities:\nCPCCode Commodity Year")
# This is the message I want when action button is clicked
# validation is not successful. Please check the following commodities:
#CPCCode Commodity Year
# 100 Maize 2010
# 200 Rice 2015
# 300 Tea 2016
# 400 Banana 2014
#.
#.
#.
# and so on. The table can be upto max 50 rows
ui = fluidPage(
actionButton("message", "message")
)
server = function(input,output,session){
rows= reactive({
d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010)
d$Commodity = as.character(d$Commodity)
d=rbind(d, c(200, "Rice", "2015"))
d=rbind(d, c(300,"Tea", 2016))
})
observeEvent(input$message,{
if (is.null(input$message) || input$message == 0){return()}
isolate({
input$message
the_message <- paste("validation is not successful")
js_string <- 'alert("SOMETHING");'
js_string <- sub("SOMETHING",the_message,js_string)
session$sendCustomMessage(type='jsCode', list(value = js_string))
})
})
}
shinyApp(ui,server)
已编辑。它仅显示 table 的第一行。如有任何建议,我们将不胜感激
library(shiny)
library(datasets)
library(rhandsontable)
library(data.table)
my_message= writeLines("validation is not successful. \nCheck following
commodities:\nCPCCode Commodity Year")
# This is the message I want when action button is clicked
# validation is not successful. Please check the following commodities:
#CPCCode Commodity Year
# 100 Maize 2010
# 200 Rice 2015
# 300 Tea 2016
# 400 Banana 2014
#.
#.
#.
# and so on. The table can be upto max 50 rows
ui = fluidPage(
actionButton("show", "Show modal dialog")
)
server = function(input, output) {
rows= reactive({
d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010)
d$Commodity = as.character(d$Commodity)
d=rbind(d, c(200, "Rice", "2015"))
d=rbind(d, c(300,"Tea", 2016))
})
# Return the UI for a modal dialog with data selection input. If 'failed'
is
# TRUE, then display a message that the previous value was invalid.
dataModal <- function(failed = FALSE) {
modalDialog(
span("Validation is not successful", rows()),
footer = tagList(
actionButton("ok", "OK")
)
)
}
observeEvent(input$show, {
showModal(dataModal())
})
observeEvent(input$ok, {
removeModal()
})
}
shinyApp(ui,server)
要在 modalDialog
中显示 table,您需要在 modalDialog
中添加一个 dataTableOutput
,并在 modalDialog
之后渲染 table =] 被创建。我已经编辑了你的服务器代码来做同样的事情。希望对你有帮助。
server = function(input, output) {
rows= reactive({
d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010)
d$Commodity = as.character(d$Commodity)
d=rbind(d, c(200, "Rice", "2015"))
d=rbind(d, c(300,"Tea", 2016))
})
# Return the UI for a modal dialog with data selection input. If 'failed'
# is
# TRUE, then display a message that the previous value was invalid.
dataModal <- function(failed = FALSE) {
modalDialog(
span("Validation is not successful"),
dataTableOutput("table"),
footer = tagList(
actionButton("ok", "OK")
)
)
}
observeEvent(input$show, {
showModal(dataModal())
output$table <- renderDataTable({rows()})
})
observeEvent(input$ok, {
removeModal()
})
}