我正在构建R Shiny Dashboard。我已经建立了代码UI部分。现在,我想添加应该动态显示在右侧的菜单项的内容。我已经为标签项应用了observerevent。但是它无法正常工作,如下所示:
代码:
library(shiny)
library(shinythemes)
#UI client..............................
ui <- fluidPage(
tags$style(HTML("
h4 {
color: white;
padding-top: 5px;
font-weight: bold;
}
.content-wrapper{
overflow:auto;
width:150%;
}
"
)
),
dashboardPage(
dashboardHeader(title="Title",
tags$li(h4("Welcome User!"),
class = "dropdown",
dropdownMenu(type = "messages",
messageItem(
from = "Logout",
message = "Session Ends"
))
)),
## Sidebar content
dashboardSidebar(
sidebarMenu(
menuItem("OverView", tabName = "Sitecontent", icon = icon("th")),
menuItem("Candidate Information", tabName = "CandidateInformation", icon = icon("th")),
menuItem("Assesment", tabName = "Assesment", icon = icon("th"),
menuItem('Affect',
tabName = 'Affect',
icon = icon('th'))
)
)
),
dashboardBody(
tabItems(
# OverView tab content
tabItem(tabName = "OverView",
h2("OverView ")
),
# Assesment tab content
tabItem(tabName = "Assesment",
h2("Assesment ")
),
# Predictive Result tab content
tabItem(tabName = "PredictAny",
h2("PredictAny")
),
# PreventiveResults tab content
tabItem(tabName = "PreventiveResults",
h2("PreventiveResults")
),
tabItem(tabName = "Affect",
h2("Assesment on Affect")
)
)
)
)
)
server <- function(input, output,session) {
observeEvent(input$Affect, { #Not triggering
sendSweetAlert(
session = session,
title = "Done!",
text = "Affect Triggered.",
type = "success"
)
})
}
shinyApp(ui, server)
我无法打observeEvent(input$Affect,
。不确定我在这里做错了什么..请帮助我。
答案 0 :(得分:1)
尝试一下:
library(shiny)
library(shinythemes)
library(shinydashboard)
#UI client..............................
ui <- fluidPage(
tags$style(HTML("
h4 {
color: white;
padding-top: 5px;
font-weight: bold;
}
.content-wrapper{
overflow:auto;
width:150%;
}
"
)
),
dashboardPage(
dashboardHeader(title="OPTUM",
tags$li(h4("Welcome User!"),
class = "dropdown",
dropdownMenu(type = "messages",
messageItem(
from = "Logout",
message = "Session Ends"
))
)),
## Sidebar content
dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem("OverView", tabName = "Sitecontent", icon = icon("th")),
menuItem("Candidate Information", tabName = "CandidateInformation", icon = icon("th")),
menuItem("Assesment", tabName = "Assesment", icon = icon("th"),
menuItem('Affect',
tabName = 'AffectTab',
icon = icon('th'))
)
)
),
dashboardBody(
tabItems(
# OverView tab content
tabItem(tabName = "OverView",
h2("OverView ")
),
# Assesment tab content
tabItem(tabName = "Assesment",
h2("Assesment ")
),
# Predictive Result tab content
tabItem(tabName = "PredictAny",
h2("PredictAny")
),
# PreventiveResults tab content
tabItem(tabName = "PreventiveResults",
h2("PreventiveResults")
),
tabItem(tabName = "AffectTab",
h2("Assesment on Affect")
)
)
)
)
)
server <- function(input, output,session) {
observeEvent(input$tabs, { #Not triggering
print(input$tabs)
if(input$tabs=="AffectTab"){
sendSweetAlert(
session = session,
title = "Done!",
text = "Affect Triggered.",
type = "success"
)
}
})
}
shinyApp(ui, server)