Activate the same event with the use of two different actionbuttons in a shiny app

时光总嘲笑我的痴心妄想 提交于 2021-01-07 01:26:04

问题


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.

  1. The user can press whichever of these 3 header buttons he wishes before pressing the Get Started button. Its ok that he cannot see the Get 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.

  1. The user press the Get Started button first and he moves to the Consent tab in order to type a name and then Run 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!