[英]How to disable button based on a condition in a R Markdown document with shiny elements?
假設我們有一組8個復選框(8個字母)和一個操作按鈕,該按鈕將打印所有選定復選框的標簽。 我想做的是根據條件啟用和禁用操作按鈕的狀態。 條件是,如果所選復選框的數量在2到5之間,則應該啟用該按鈕,否則禁用該按鈕。 為了更改按鈕的狀態,我想使用shinyjs
包中的功能enable
, disable
或toggleState
功能。 啟用該按鈕后,我將能夠觸發一個事件以打印所選項目的數量。 這是我到目前為止嘗試過的方法:
---
title: "Disable Button"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shinyjs)
library(shiny)
```
```{r, echo=FALSE}
checkboxGroupInput("param_group", label = h3("Letters"),
choices = LETTERS[1:8])
actionButton('action', "Print")
result<-reactive({
length(input$param_group)
})
observe({
if(result()>1 & result()<=5)
enable("action")
else
disable("action")
})
txt<-eventReactive(input$action,{
cat("Number of letters selected: ",length(input$param_group))
})
renderPrint({
txt()
})
```
花了我一段時間才能找到它,但您必須啟用shinyjs
顯式使用R-markdown,在這種情況下,它需要以不同的方式設置其javascript。
您可以通過在使用它的塊中調用: useShinyjs(rmd=T)
來實現。
---
title: "Disable Button"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shinyjs)
library(shiny)
```
```{r, echo=FALSE}
useShinyjs(rmd=T)
checkboxGroupInput("param_group", label = h3("Letters"),
choices = LETTERS[1:8])
actionButton('action', "Print")
result<-reactive({
length(input$param_group)
})
observe({
useShinyjs()
if(result()>1 & result()<=5){
enable("action")
} else {
disable("action")
}
})
txt<-eventReactive(input$action,{
cat("Number of letters selected: ",length(input$param_group))
})
renderPrint({
txt()
})
屏幕截圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.