【问题标题】:Activate the same event with the use of two different actionbuttons in a shiny app在闪亮的应用程序中使用两个不同的操作按钮激活相同的事件
【发布时间】:2021-01-05 05:50:35
【问题描述】:

我有下面的shiny 应用程序。当应用程序第一次启动时,它会在主体中显示一个操作按钮 Get Started,在标题中显示 3 个操作按钮。

  1. 用户可以按他之前希望的这 3 个标题按钮中的任何一个 按下Get Started 按钮。没关系,他再也看不到Get Started 按钮了

如果他按下Consent,他将被移动到Consent tabItem。然后他可以写一个名字按Run Project 并在Results tabItem 中查看情节。

如果他在Consent 输入姓名之前按Run Project,他将自动移入Consent 以便输入姓名。

如果他在输入姓名之前按ResultsConsent,他将自动进入同意以输入姓名。

  1. 用户首先按下Get Started 按钮,然后移动到 Consent 选项卡以键入名称,然后 Run Project 移动到 结果。

我认为由于Run ProjectGet Started 两个动作按钮的冲突,我的代码deos 无法正常工作。

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))
          
      }
    })
  }
  )
)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    也许这就够了。

    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))
          }
        })
      }
      )
    )
    

    【讨论】:

    • 几乎很棒,除了当我点击运行项目或结果按钮时开始按钮应该消失,就像我点击同意时发生的那样
    猜你喜欢
    • 1970-01-01
    • 2020-12-28
    • 2017-12-12
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2023-04-09
    相关资源
    最近更新 更多