【问题标题】:How to show hidden tabs after clicking an actionButton?单击操作按钮后如何显示隐藏的选项卡?
【发布时间】:2022-01-13 00:25:54
【问题描述】:

我正在尝试找到隐藏选项卡并在单击actionButton 后显示它们的方法。我找到了这个post,但建议的解决方案使用navbarPage。我尝试使用sidebarPanel 更改示例:

library(shiny)
library(shinyjs)
ui <- fluidPage(useShinyjs(),

                tags$head(tags$style(HTML("#hello li a[data-value = 'tab2_val'], #hello li a[data-value = 'tab3_val'] 
                { display: none;}"))),
                
      # Application title
      titlePanel("My app"),
      
      sidebarLayout(
        sidebarPanel(
          tabsetPanel("hello", id="hello",
                                
           tabPanel("home", br(), h3("Click the button"),actionButton("enter", "enter")),
           tabPanel("tab2", value = "tab2_val", br(), h4("this is tab2")),
           tabPanel("tab3 with a lot of stuff in it", value = "tab3_val", br(), h4("this is tab3"))),
          
          mainPanel(
          )
        )
      )
)
      

server <- function(input, output, session) {
  
  observeEvent(input$enter, {
    show(selector = '#hello li a[data-value="tab3_val"]')
    show(selector = '#hello li a[data-value="tab2_val"]')
    })}
shinyApp(ui, server)

但我收到此错误:

错误:导航容器需要一个集合 bslib::nav()/shiny::tabPanel()s 和/或 bslib::nav_menu()/shiny::navbarMenu()s。考虑使用headerfooter 如果您希望将内容放在每个面板的上方(或下方) 内容。

我尝试在post 中运行建议的解决方案,但我仍然遇到同样的错误。我想这是因为我拥有的闪亮版本(即 1.7.1)。

谁能帮我解决这个问题?

我附上一个可重现的例子。我们的想法是隐藏 Tab2 和 Tab3,当您单击 actionButton“提交”时,它们会出现。

library(shiny)
library(dplyr)
library(ggplot2)

ui <- fluidPage(
  
  titlePanel("My app"),
  
  sidebarLayout(
    sidebarPanel(
      tabsetPanel(
        
        tabPanel("Tab1",
                 checkboxInput("log2", "Log2 transformation", value = FALSE),
                 actionButton("submit", "Submit")
        ),
        
        tabPanel("Tab2",
                 radioButtons(inputId = "plot_type", label = "I want to see the plot of:",
                              c("All the samples" = "all_samples",
                                "Groups" = "samples_group")),
                 conditionalPanel(
                   condition = "input.plot_type == 'samples_group'",
                   style = "margin-left: 20px;",
                   checkboxGroupInput("group", "Choose the group:",
                                      choices = c("Group1", "Group2", "Group3"))),
                 
                 actionButton("show_plot", "See the plot")
        ),
        
        tabPanel("Tab3",
                 numericInput("alpha", "Opacity of the plot", value=0.2),
                 checkboxInput(inputId = "Kruskalpval", label = "Show the Kruskal Wallis p-value", value = FALSE),
                 conditionalPanel(
                   condition = "input.Kruskalpval == '1'",
                   style = "margin-left: 20px;",
                   checkboxInput(inputId = "changeKW", "I want to change the place of the value", value=FALSE),
                   
                   conditionalPanel(
                     condition = "input.changeKW == '1'",
                     numericInput(inputId = "X_axis", "X_axis:", value=2),
                     numericInput(inputId = "Y_axis", "Y_axis:", value=70)
                   )
                   
                 ),
                 actionButton("show_plot_2", "See the plot")
        )
        
      )
    ),
    
    mainPanel(
      plotOutput("boxplots")
    )
  )
)


server <- function(input, output) {
  
  
  set.seed(1234)
  Gene <- floor(runif(25, min=0, max=101))
  groups_age <- floor(runif(25, min=18, max=75))
  Group <- c("Group1", "Group1", "Group3", "Group2", "Group1", "Group3", "Group2", "Group2", "Group2", "Group1", "Group1", "Group3", "Group1", "Group2", "Group1", "Group2", "Group3", "Group1", "Group3", "Group3", "Group2", "Group1", "Group3", "Group3","Group2")
  
  data <- reactive({
    df <- data.frame(Gene, Group, groups_age)
    
    mybreaks <- seq(min(df$groups_age)-1, to=max(df$groups_age)+10, by=10)
    df$groups_age <- cut(df$groups_age, breaks = mybreaks, by=10)
    
    if(input$plot_type == "samples_group"){
      
      # if the user selects everything, it will take everything. 
      if(all(c("Group1", "Group2", "Group3") %in% input$group)){
        return(df)
        
        # if the user only selects group1 and group2, it will appear only those columns.
      }else if (all(c("Group1", "Group2") %in% input$group)) {
        df <- subset(df, (df$Group == "Group1" | df$Group == "Group2"))
        return(df)
        
        # if the user only selects group1 and group3, it will appear only those columns.
      }else if (all(c("Group1", "Group3") %in% input$group)) {
        df <- subset(df, (df$Group == "Group1" | df$Group == "Group3"))
        return(df)
        
        # if the user only selects Group2 and Group3, it will appear only those columns.
      }else if (all(c("Group2", "Group3") %in% input$group)) {
        df <- subset(df, (df$Group == "Group2" | df$Group == "Group3"))
        return(df)
        
        # if the user only selects Group1
      } else if ("Group1" %in% input$group) {
        df <- subset(df, (df$Group == "Group1"))
        return(df)
        
        # if the user only selects group2
      } else if ("Group2" %in% input$group) {
        df <- subset(df, (df$Group == "Group2"))
        return(df)
        
        
        # if the user only selects group3
      } else if ("Group3" %in% input$group) {
        df <- subset(df, (df$Group == "Group3"))
        return(df)
        
        # if the user doesn't select anything.
      } else {
        return(df)
      }
    }else{
      df$Group <- NULL
      return(df)
    }
  })
  
  
  mydata <- reactive({
    req(input$submit)
    
    if(input$log2 == TRUE){
      data <- data()
      cols <- sapply(data, is.numeric)
      data[cols] <- lapply(data[cols], function(x) log2(x+1))
      
    }
    else{
      data <- data()
    }
    return(data)
  })
  
  draw_bp <- eventReactive(c(input$show_plot, input$show_plot_2), {
    
    if(ncol(mydata())==2){
      bp <- ggplot(mydata(), aes(x=groups_age, y=Gene)) +
        geom_boxplot(aes(fill=groups_age), alpha = input$alpha) +
        labs(fill = "groups_age")
      
      if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
        pval <- mydata() %>%
          summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
        
        bp <- bp + geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
      }
      
      if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
        pval <- mydata() %>%
          summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
        
        bp <- bp + geom_text(data=pval, aes(x=isolate(input$X_axis), y=isolate(input$Y_axis), label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
      }
      return(bp)
    }
    else{
      bp <- ggplot(mydata(), aes(x=groups_age, y=Gene)) +
        geom_boxplot(aes(fill=groups_age), alpha=input$alpha) +
        facet_grid(. ~ Group)
      
      if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
        pval <- mydata() %>%
          group_by(Group) %>%
          summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
        
        bp <- bp + geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
      }
      
      if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
        pval <- mydata() %>%
          group_by(Group) %>%
          summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
        
        bp <- bp + geom_text(data=pval, aes(x=isolate(input$X_axis), y=isolate(input$Y_axis), label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
      }
      return(bp)
      
    }
  })
  
  v <- reactiveValues()
  observeEvent(input$show_plot | input$show_plot_2, {
    v$plot <- draw_bp()
    
  })
  
  output$boxplots <- renderPlot({
    req(input$submit)
    draw_bp()
  })
}

shinyApp(ui = ui, server = server)

提前非常感谢

问候

【问题讨论】:

  • 由于tabsetPanel 中的"hello" 而出现错误。 tabsetPanel 期望 tabPanels 不是字符串。

标签: r shiny hide tabpanel action-button


【解决方案1】:

这是隐藏/显示第二个标签的代码。其他选项卡的代码类似。

library(shiny)

js <- "$(document).ready(function(){
    var $tab2 = $('#hello li > a[data-value=tab2_val]').parent(); 
    $tab2.removeClass('active').addClass('hide');
    $('#enter').on('click', function(){
      $tab2.removeClass('hide');
    });
  });
"

ui <- fluidPage(
  tags$head(
    tags$script(HTML(js))
  ),

  # Application title
  titlePanel("My app"),
  sidebarLayout(
    sidebarPanel(
      tabsetPanel(
        tabPanel(
          "home", br(), h3("Click the button"), actionButton("enter", "enter")
        ),
        tabPanel(
          "tab2", value = "tab2_val", br(), h4("this is tab2")
        ),
        tabPanel(
          "tab3 with a lot of stuff in it", value = "tab3_val", br(), 
          h4("this is tab3")
        ),
        id = "hello"
      )
    ),
    mainPanel()
  )
)


server <- function(input, output, session) {
}

shinyApp(ui, server)

【讨论】:

  • 非常感谢您的帮助。但是,我怎样才能为 Tab3 做到这一点?我不知道 JS 是如何处理空格的……我也尝试过同样的事情……但似乎它不喜欢空格。如果我只写“Tab3”作品.. 但是,如果我有 2 或 3 个字怎么办?你知道我该怎么做吗?谢谢
  • 我尝试添加一个常规空间,就像我在 Google (&amp;nbsp) 上找到的那样,$Tab3&amp;nbspwith&amp;nbspa&amp;nbsplot&amp;nbspof&amp;nbspstuff&amp;nbspin&amp;nbspit,但它不起作用。
  • @Eva Tab3 (tab3_val) 的值中没有空格。但是如果你有一些空间,你可以试试a[data-value=\"tab3 value with spaces\"]
  • 不,我不是指标签的值。我指的是每个选项卡的名称。您使用了var $tab2$tab2.removeClass 的名称为tab2...(即tab2),但是tab3 有这个名称(“tab3 里面有很多东西”)...但是如果我尝试和你对 tab2 做的一样,它不起作用。您能告诉我如何将第三个标签添加到您的答案中吗?再次非常感谢
  • 这是我尝试过的(但它不起作用)js &lt;- "$(document).ready(function(){ var $tab2 = $('#hello li &gt; a[data-value=tab2_val]').parent(); var $tab3 with a lot of stuff in it = $('#hello li &gt; a[data-value=tab3_val]').parent(); $tab2.removeClass('active').addClass('hide'); $tab3 with a lot of stuff in it.removeClass('active').addClass('hide'); $('#submit').on('click', function(){ $tab2.removeClass('hide'); $tab3 with a lot of stuff in it.removeClass('hide'); }); }); "
【解决方案2】:

这是一个使用appendTab 的解决方案,正如here 所做的那样:

您的初始示例:

library(shiny)

ui <- fluidPage(
                titlePanel("My app"),
                sidebarLayout(
                  sidebarPanel(
                    tabsetPanel(id="hello",
                                tabPanel("home", br(), h3("Click the button"),actionButton("enter", "enter"))
                                )
                  ),
                  mainPanel()
                )
)


server <- function(input, output, session) {
  
  observeEvent(input$enter, {
    appendTab(inputId = "hello", tabPanel("tab2", value = "tab2_val", br(), h4("this is tab2")))
    appendTab(inputId = "hello", tabPanel("tab3 with a lot of stuff in it", value = "tab3_val", br(), h4("this is tab3")))
  }, once = TRUE)
  
  }

shinyApp(ui, server)

第二个例子:

library(shiny)
library(dplyr)
library(ggplot2)

ui <- fluidPage(
  
  titlePanel("My app"),
  
  sidebarLayout(
    sidebarPanel(
      tabsetPanel(
        id = "tabsetPanelID",
        tabPanel("Tab1",
                 checkboxInput("log2", "Log2 transformation", value = FALSE),
                 actionButton("submit", "Submit")
        )
      )
    ),
    mainPanel(
      plotOutput("boxplots")
    )
  )
)


server <- function(input, output) {
  
  observeEvent(input$submit, {
    appendTab(inputId = "tabsetPanelID", tab = tabPanel("Tab2",
                                                        radioButtons(inputId = "plot_type", label = "I want to see the plot of:",
                                                                     c("All the samples" = "all_samples",
                                                                       "Groups" = "samples_group")),
                                                        conditionalPanel(
                                                          condition = "input.plot_type == 'samples_group'",
                                                          style = "margin-left: 20px;",
                                                          checkboxGroupInput("group", "Choose the group:",
                                                                             choices = c("Group1", "Group2", "Group3"))),
                                                        
                                                        actionButton("show_plot", "See the plot")
    ))
    appendTab(inputId = "tabsetPanelID", tab = tabPanel("Tab3",
                                                        numericInput("alpha", "Opacity of the plot", value=0.2),
                                                        checkboxInput(inputId = "Kruskalpval", label = "Show the Kruskal Wallis p-value", value = FALSE),
                                                        conditionalPanel(
                                                          condition = "input.Kruskalpval == '1'",
                                                          style = "margin-left: 20px;",
                                                          checkboxInput(inputId = "changeKW", "I want to change the place of the value", value=FALSE),
                                                          
                                                          conditionalPanel(
                                                            condition = "input.changeKW == '1'",
                                                            numericInput(inputId = "X_axis", "X_axis:", value=2),
                                                            numericInput(inputId = "Y_axis", "Y_axis:", value=70)
                                                          )
                                                          
                                                        ),
                                                        actionButton("show_plot_2", "See the plot")
    ))
  }, once = TRUE)
  
  set.seed(1234)
  Gene <- floor(runif(25, min=0, max=101))
  groups_age <- floor(runif(25, min=18, max=75))
  Group <- c("Group1", "Group1", "Group3", "Group2", "Group1", "Group3", "Group2", "Group2", "Group2", "Group1", "Group1", "Group3", "Group1", "Group2", "Group1", "Group2", "Group3", "Group1", "Group3", "Group3", "Group2", "Group1", "Group3", "Group3","Group2")
  
  data <- reactive({
    df <- data.frame(Gene, Group, groups_age)
    
    mybreaks <- seq(min(df$groups_age)-1, to=max(df$groups_age)+10, by=10)
    df$groups_age <- cut(df$groups_age, breaks = mybreaks, by=10)
    
    if(input$plot_type == "samples_group"){
      
      # if the user selects everything, it will take everything. 
      if(all(c("Group1", "Group2", "Group3") %in% input$group)){
        return(df)
        
        # if the user only selects group1 and group2, it will appear only those columns.
      }else if (all(c("Group1", "Group2") %in% input$group)) {
        df <- subset(df, (df$Group == "Group1" | df$Group == "Group2"))
        return(df)
        
        # if the user only selects group1 and group3, it will appear only those columns.
      }else if (all(c("Group1", "Group3") %in% input$group)) {
        df <- subset(df, (df$Group == "Group1" | df$Group == "Group3"))
        return(df)
        
        # if the user only selects Group2 and Group3, it will appear only those columns.
      }else if (all(c("Group2", "Group3") %in% input$group)) {
        df <- subset(df, (df$Group == "Group2" | df$Group == "Group3"))
        return(df)
        
        # if the user only selects Group1
      } else if ("Group1" %in% input$group) {
        df <- subset(df, (df$Group == "Group1"))
        return(df)
        
        # if the user only selects group2
      } else if ("Group2" %in% input$group) {
        df <- subset(df, (df$Group == "Group2"))
        return(df)
        
        
        # if the user only selects group3
      } else if ("Group3" %in% input$group) {
        df <- subset(df, (df$Group == "Group3"))
        return(df)
        
        # if the user doesn't select anything.
      } else {
        return(df)
      }
    }else{
      df$Group <- NULL
      return(df)
    }
  })
  
  
  mydata <- reactive({
    req(input$submit)
    
    # browser()
    if(input$log2 == TRUE){
      data <- data()
      cols <- sapply(data, is.numeric)
      data[cols] <- lapply(data[cols], function(x) log2(x+1))
    }
    else{
      data <- data()
    }
    return(data)
  })
  
  draw_bp <- eventReactive(c(input$show_plot, input$show_plot_2), {
    
    if(ncol(mydata())==2){
      bp <- ggplot(mydata(), aes(x=groups_age, y=Gene)) +
        geom_boxplot(aes(fill=groups_age), alpha = input$alpha) +
        labs(fill = "groups_age")
      
      req(input$Kruskalpval)
      
      if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
        pval <- mydata() %>%
          summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
        
        bp <- bp + geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
      }
      
      if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
        pval <- mydata() %>%
          summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
        
        bp <- bp + geom_text(data=pval, aes(x=isolate(input$X_axis), y=isolate(input$Y_axis), label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
      }
      return(bp)
    }
    else{
      bp <- ggplot(mydata(), aes(x=groups_age, y=Gene)) +
        geom_boxplot(aes(fill=groups_age), alpha=input$alpha) +
        facet_grid(. ~ Group)
      
      req(input$Kruskalpval)
      
      if((input$Kruskalpval == "TRUE") && (input$changeKW==FALSE)){
        pval <- mydata() %>%
          group_by(Group) %>%
          summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
        
        bp <- bp + geom_text(data=pval, aes(x=2, y=max(mydata()$Gene)-10, label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
      }
      
      if((input$Kruskalpval == "TRUE") && (input$changeKW==TRUE)){
        pval <- mydata() %>%
          group_by(Group) %>%
          summarize(Kruskal_pvalue = kruskal.test(Gene ~ groups_age)$p.value)
        
        bp <- bp + geom_text(data=pval, aes(x=isolate(input$X_axis), y=isolate(input$Y_axis), label=paste0("Kruskal-Wallis\n p = ",Kruskal_pvalue)))
      }
      return(bp)
      
    }
  })
  
  v <- reactiveValues()
  observeEvent(input$show_plot | input$show_plot_2, {
    v$plot <- draw_bp()
    
  })
  
  output$boxplots <- renderPlot({
    req(input$submit)
    draw_bp()
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多