问题
I have the shiny
app below. When the app is launched for 1st time it displays an actionbutton Get Started
in the main body and 3 actionbuttons in the header.
- The user can press whichever of these 3 header buttons he wishes before
pressing the
Get Started
button. Its ok that he cannot see theGet Started
button again
If he press Consent
he will be moved in the Consent
tabItem. Then he can write a name press Run Project
and see the plot in the Results
tabItem.
If he press Run Project
before having typed a name in Consent
he will be moved in Consent
automatically in order to type a name.
If he press Results
before having typed a name in Consent
he will be moved in Consent automatically in order to type a name.
- The user press the
Get Started
button first and he moves to theConsent
tab in order to type a name and thenRun Project
to move to Results.
I think that my code deos not work because of the conflict of the two actionbuttons Run Project
and Get Started
.
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
mytitle <- paste0("")
dbHeader <- dashboardHeaderPlus(
titleWidth = "0px",
tags$li(a(
div(style="display: inline;margin-top:-35px; padding: 0px 90px 0px 1250px ;font-size: 44px ;color:#001641;font-family:Chronicle Display Light; width: 500px;",HTML(mytitle)),
div(style="display: inline;padding: 0px 0px 0px 0px;vertical-align:top; width: 150px;", actionButton("conse", "Consent",
style=" background-color: #faf0e6; border-color: #faf0e6") ),
div(style="display: inline;padding: 0px 0px 0px 0px;vertical-align:top; width: 150px;", actionButton("rp", "Run Project",
style=" background-color: #faf0e6; border-color: #faf0e6") ),
div(style="display: inline;padding: 0px 0px 0px 0px;vertical-align:top; width: 150px;", actionButton("res", "Results",
style=" background-color: #faf0e6; border-color: #faf0e6") ),
), class = "dropdown")
)
shinyApp(
ui = dashboardPagePlus(
header = dbHeader,
sidebar = dashboardSidebar(width = "0px",
sidebarMenu(id = "sidebar", # id important for updateTabItems
menuItem("Consent", tabName = "conse", icon = icon("line-chart")),
menuItem("Results", tabName = "res", icon = icon("line-chart"))
)
),
body = dashboardBody(
useShinyjs(),
tags$script(HTML("$('body').addClass('fixed');")),
tags$head(tags$style(".skin-blue .main-header .logo { padding: 0px;}")),
actionButton("button", "Get started", style='padding:4px; font-size:140%'),
tabItems(
tabItem("conse",
conditionalPanel(condition = "input.conse >0 || input.button>0",
textInput("nam", label = ("Name"), value = "")
)
),
tabItem("res", uiOutput('markdown')
)
)
)
),
server<-shinyServer(function(input, output,session) {
hide(selector = "body > div > header > nav > a")
observeEvent(input$button, {
updateTabItems(session, "sidebar", selected = "conse")
shinyjs::hide("button")
})
observeEvent(input$conse, {
updateTabItems(session, "sidebar", selected = "conse")
shinyjs::hide("button")
})
observeEvent(input$button, {
if (input$nam=="") {
updateTabItems(session, "sidebar",
selected = "conse")
}
else{
updateTabItems(session, "sidebar",
selected = "res")
}
})
observeEvent(input$rp, {
if (input$nam=="") {
updateTabItems(session, "sidebar",
selected = "conse")
}
else{
updateTabItems(session, "sidebar",
selected = "res")
}
})
output$markdown <- renderUI({input$rp
if (input$nam==""){
return(NULL)
}
else{
isolate(plot(iris))
}
})
}
)
)
回答1:
Perhaps this will suffice.
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(stringi)
mytitle <- paste0("Test")
dbHeader <- dashboardHeaderPlus(
titleWidth = "0px",
tags$li(a(
div(style="display: inline;margin-top:-35px; padding: 0px 90px 0px 1250px ;font-size: 44px ;color:#001641;font-family:Chronicle Display Light; width: 500px;",HTML(mytitle)),
div(style="display: inline;padding: 0px 0px 0px 0px;vertical-align:top; width: 150px;", actionButton("conse", "Consent",
style=" background-color: #faf0e6; border-color: #faf0e6") ),
div(style="display: inline;padding: 0px 0px 0px 0px;vertical-align:top; width: 150px;", actionButton("rp", "Run Project",
style=" background-color: #faf0e6; border-color: #faf0e6") ),
div(style="display: inline;padding: 0px 0px 0px 0px;vertical-align:top; width: 150px;", actionButton("res", "Results",
style=" background-color: #faf0e6; border-color: #faf0e6") ),
), class = "dropdown")
)
shinyApp(
ui = dashboardPagePlus(
header = dbHeader,
sidebar = dashboardSidebar(width = "0px",
sidebarMenu(id = "sidebar", # id important for updateTabItems
menuItem("Consent", tabName = "conse", icon = icon("line-chart")),
menuItem("Results", tabName = "res", icon = icon("line-chart"))
)
),
body = dashboardBody(
useShinyjs(),
tags$script(HTML("$('body').addClass('fixed');")),
tags$head(tags$style(".skin-blue .main-header .logo { padding: 0px;}")),
actionButton("button", "Get started", style='padding:4px; font-size:140%'),
tabItems(
tabItem("conse",
#conditionalPanel(condition = "input.conse >0 || input.button>0 ",
textInput("nam", label = ("Name"), value = "")
#)
),
tabItem("res", plotOutput('markdown')
)
)
)
),
server<-shinyServer(function(input, output,session) {
hide(selector = "body > div > header > nav > a")
shinyjs::hide("nam")
observeEvent(input$button, {
shinyjs::show("nam")
updateTabItems(session, "sidebar", selected = "conse")
shinyjs::hide("button")
})
observeEvent(input$conse, {
shinyjs::show("nam")
updateTabItems(session, "sidebar", selected = "conse")
shinyjs::hide("button")
})
observeEvent(input$rp, {
shinyjs::hide("button")
p <- stri_stats_latex(input$nam)[1]
if (is.null(input$nam) | input$nam=="" | p<1) {
shinyjs::show("nam")
updateTabItems(session, "sidebar", selected = "conse")
}else{
updateTabItems(session, "sidebar", selected = "res")
}
})
observeEvent(input$res, {
shinyjs::hide("button")
p <- stri_stats_latex(input$nam)[1]
if (is.null(input$nam) | input$nam=="" | p<1) {
shinyjs::show("nam")
updateTabItems(session, "sidebar", selected = "conse")
}else{
updateTabItems(session, "sidebar", selected = "res")
}
})
output$markdown <- renderPlot({ # input$rp
p <- stri_stats_latex(input$nam)[1]
if (is.null(input$nam) | input$nam=="" | p<1){
return(NULL)
}else{
isolate(plot(iris))
}
})
}
)
)
来源:https://stackoverflow.com/questions/65567297/activate-the-same-event-with-the-use-of-two-different-actionbuttons-in-a-shiny-a