问题
The code below has action button in rendered in html. But when i click the button in second row(id as "ang") there is no modal box showing up. Can anyone help me? Not sure why this logic is not working.. If I put a normal button, it is working
library(shiny)
library(shinydashboard)
library(DT)
number_compare <- data.frame(replicate(2, sample(1:100, 10, rep=TRUE)))
# number_compare$direction <- ifelse(
# number_compare$X1 < number_compare$X2,
# as.character(icon("angle-up")),
# as.character(icon("angle-down"))
# )
sidebar <- dashboardSidebar()
body <- dashboardBody(
fluidRow(box(width = 12, solidHeader = TRUE,
DTOutput("example_table"))
)
)
ui <- dashboardPage(dashboardHeader(title = "Example"),
sidebar,
body
)
server <- function(input, output) {
number_compare$X2[which(rownames(number_compare) == 2)] = paste(with(number_compare, X2[rownames(number_compare) == 2])," ", as.character(actionButton("ang","")))
number_compare$X2[which(rownames(number_compare) == 4)] = paste(with(number_compare, X2[rownames(number_compare) == 4])," ", as.character(actionButton("angle2","", icon("angle-up"),style = "border: none;
outline:none;background:white")))
number_compare$X2[which(rownames(number_compare) == 6)] = paste(with(number_compare, X2[rownames(number_compare) == 6])," ", as.character(actionButton("angle3","", icon("angle-up"),style = "border: none;
outline:none;background:white")))
output$example_table <- DT::renderDT({
datatable(
number_compare,
escape = FALSE)
})
observeEvent(input$ang,{
showModal(modalDialog(
title = "Somewhat important message",
"This is a somewhat important message.",
easyClose = TRUE,
footer = NULL
))
})
}
shinyApp(ui, server)
I got the solution with below code . Just a small change I did. If i change the input date and click on arrow, it not popping up. Not sure what mistake I am making (Basicall, if I click "sam" button twice, the arrow is not popping up modal box when it is clicked
library(shiny)
library(shinydashboard)
library(DT)
number_compare <- data.frame(replicate(2, sample(1:100, 10, rep=TRUE)))
# number_compare$direction <- ifelse(
# number_compare$X1 < number_compare$X2,
# as.character(icon("angle-up")),
# as.character(icon("angle-down"))
# )
sidebar <- dashboardSidebar()
body <- dashboardBody(
fluidRow(box(width = 12, solidHeader = TRUE,
DTOutput("example_table"),
actionButton("sam","sam"),
dateInput("da","Date", value = Sys.Date(), min = Sys.Date()-1, max = Sys.Date()+1))
)
)
ui <- dashboardPage(dashboardHeader(title = "Example"),
sidebar,
body
)
server <- function(input, output) {
number_compare$X2 <- paste("sd",number_compare$X2)
number_compare$X2[which(rownames(number_compare) == 2)] = paste(with(number_compare, X2[rownames(number_compare) == 2])," ", " ",as.character(actionLink(inputId="ang", label="", icon("caret-up"))))
print(paste(with(number_compare, X2[rownames(number_compare) == 2])," ", as.character(actionButton("ang",""))))
# number_compare$X2[which(rownames(number_compare) == 4)] = paste(with(number_compare, X2[rownames(number_compare) == 4])," ", as.character(actionButton("angle2","", icon("angle-up"),style = "border: none;
# outline:none;background:white")))
observeEvent(input$sam,{
if(input$da == Sys.Date()){
output$example_table <- DT::renderDT({
datatable(
number_compare[c(2,3:4),],
escape = FALSE
,options=list(preDrawCallback=JS(
'function() {
Shiny.unbindAll(this.api().table().node());}'),
drawCallback= JS(
'function(settings) {
Shiny.bindAll(this.api().table().node());}')))
})
}
else {
output$example_table <- DT::renderDT({
datatable(
number_compare[c(2,5:10),],
escape = FALSE
,options=list(preDrawCallback=JS(
'function() {
Shiny.unbindAll(this.api().table().node());}'),
drawCallback= JS(
'function(settings) {
Shiny.bindAll(this.api().table().node());}')))
})
}
})
# tolisten <- reactive({
# list(input$ang, input$da)
# })
observeEvent(input$ang,{
if(!is.null(input$da))
{
print("clicked")
showModal(modalDialog(
title = "dsd"
))
}
})
}
shinyApp(ui, server)
回答1:
You have to bind:
output$example_table <- DT::renderDT({
datatable(
number_compare,
escape = FALSE,
options = list(
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
)
)
})
来源:https://stackoverflow.com/questions/63958260/issue-in-action-button