简体   繁体   English

当单击指定输入以外的任何操作或其他按钮时,如何在 R Shiny 中使用 js 隐藏条件面板?

[英]How to hide a conditional panel using js in R Shiny when any action or other button is clicked other than specified inputs?

I am trying to hide the conditional panel illustrated below when there is any user input other than the user clicking on the action button "Delete" or making a selection in the selectInput() function rendered in the conditional panel, as shown in the below image.当除了用户单击操作按钮“删除”或在条件面板中呈现的selectInput() function 中进行选择之外,还有任何用户输入时,我试图隐藏下面所示的条件面板,如下图所示. Other user inputs will be added (action buttons, radio buttons, selectInputs, etc.) so it isn't feasible to list each action that causes the conditional panel to hide.将添加其他用户输入(操作按钮、单选按钮、selectInputs 等),因此列出导致条件面板隐藏的每个操作是不可行的。 That conditional panel should always render upon the click of "Delete".该条件面板应始终在单击“删除”时呈现。 Any suggestions for how to do this?有关如何执行此操作的任何建议? Code is shown at the bottom.代码显示在底部。

在此处输入图像描述

Code:代码:

library(rhandsontable)
library(shiny)

mydata <- data.frame('Col 1' = c(1,24,0,1), check.names = FALSE)
rownames(mydata) <- c('Term A','Term B','Term C','Term D') 

ui <- fluidPage(br(),
      rHandsontableOutput("mytable"),br(),
        fluidRow(
          column(1,actionButton("addCol", "Add",width = '70px')),
          column(1,actionButton("delCol","Delete",width = '70px')),
          column(3,conditionalPanel(condition = "input.delCol",uiOutput("delCol"))) # js here
        )
)

server <- function(input, output) {
  
  output$mytable = renderRHandsontable(df())
  
  df <- eventReactive(input$addCol, {
    if(input$addCol > 0){
      newcol <- data.frame(mydata[,1])
      names(newcol) <- paste("Col",ncol(mydata)+1)
      mydata <<- cbind(mydata, newcol)
    }
    rhandsontable(mydata,rowHeaderWidth = 100, useTypes = TRUE)
  }, ignoreNULL = FALSE)
  
  observeEvent(input$delCol,
    {output$delCol<-renderUI(selectInput("delCol",label=NULL,choices=colnames(mydata),selected="Col 1"))}
    )
  
}

shinyApp(ui,server)

Per Mike´s comment I started with shinyjs and this simple example from the shinyjs reference manual, with minor modification for 2 buttons:根据 Mike 的评论,我从 shinyjs 和 shinyjs 参考手册中的这个简单示例开始,对 2 个按钮进行了微小的修改:

library(shiny)
library(shinyjs)

ui = fluidPage(
       useShinyjs(), # Set up shinyjs
       actionButton("btn", "Click to show"),
       actionButton("btn1","Click to hide"),
       hidden(p(id = "element", "I was invisible"))
     )

server = function(input, output) {
  observeEvent(input$btn, {show("element")})
  observeEvent(input$btn1,{hide("element")})
  }

shinyApp(ui,server)

Then I expanded it to my code in the OP per the below.然后我按照以下将其扩展到我在 OP 中的代码。 Note that this will still require an observeEvent() for each user action that triggers selectInput() to hide instead of a global hide every time there's any user input other than a click of "Delete" action button.请注意,对于每个触发selectInput()隐藏而不是全局隐藏的用户操作,这仍然需要一个observeEvent()每次除了单击“删除”操作按钮之外的任何用户输入时。 I'm not sure this is possible but will continue researching.我不确定这是可能的,但会继续研究。 Multiple observeEvents() won't be too bad of an option in any case.多个observeEvents()在任何情况下都不会是一个太糟糕的选择。

Resolving code:解析代码:

library(rhandsontable)
library(shiny)
library(shinyjs)

mydata <- data.frame('Col 1' = c(1,24,0,1), check.names = FALSE)
rownames(mydata) <- c('Term A','Term B','Term C','Term D') 

ui <- fluidPage(
  useShinyjs(), # set up shinyjs
  br(),
  rHandsontableOutput("mytable"),br(),
  fluidRow(
    column(1,actionButton("addCol", "Add",width = '70px')),
    column(1,actionButton("delCol","Delete",width = '70px')),
    column(3, hidden(uiOutput("delCol2")))) # hide selectInput()
)

server <- function(input,output,session){
  
  output$mytable = renderRHandsontable(dat())
  
  dat <- eventReactive(input$addCol, {
    if(input$addCol > 0){
      newcol <- data.frame(mydata[,1])
      names(newcol) <- paste("Col",ncol(mydata)+1)
      mydata <<- cbind(mydata, newcol)
    }
    rhandsontable(mydata,rowHeaderWidth = 100, useTypes = TRUE)
  }, ignoreNULL = FALSE)
  
  observeEvent(input$delCol, show("delCol2")) # clicking Delete button reveals selectInput()
   
  observeEvent(input$addCol, hide("delCol2")) # clicking Add hides selectInput()
  
  output$delCol2 <-renderUI({
    selectInput(
      "delCol3",
      label=NULL,
      choices=colnames(mydata),
      selected="Col 1"
    )
  })
  
}

shinyApp(ui,server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击其他按钮时如何隐藏已打开的面板? - How to hide already opened panel when click on other button? 单击单选按钮时隐藏其他div - Hide other divs when a radio button is clicked 如果再次单击同一按钮或单击其他按钮,如何隐藏iframe? - How to hide Iframe if the same button is clicked again or other button is clicked? 如何在页面加载时隐藏门户页面上的按钮并在稍后单击其他按钮时显示它 - How to hide a button on portal page when the page loads and display it later when clicked on other button 当单击指定的div以外的其他时,然后隐藏下拉列表 - when click other than a specified div then hide the dropdown 单击除其他元素以外的其他元素时隐藏视频 - Hide video when clicked on any other element but itself 单击时以及单击其他按钮时如何更改按钮的颜色 - How to change the color of a button when clicked, and when other button is clicked jQuery slideToggle-再次单击时隐藏/其他按钮 - jQuery slideToggle - hide when clicked again/other button React JS如何更改元素中的字体颜色而不是单击按钮onClick? - React JS How to change font color in element other than clicked button onClick? Kendo UI内联编辑:如何在单击其他按钮时隐藏按钮 - Kendo UI inline editing: How to hide a button when other one is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM