2016-07-05 164 views
1

在R/Shiny中,我想添加一個tooltip來通知用戶由於必填字段未完成而導致按鈕被禁用。將工具提示添加到閃亮的禁用按鈕?

我可以使用ShinyBS軟件包獲取工具提示,但是當按鈕被禁用時它似乎無法工作。以下是最低工作示例。

是否有一個簡單的修復方法來獲取工具提示在Shiny中禁用的按鈕上工作?

ui.R

library(shinyBS) 
library(shiny) 
library(shinyjs) 


shinyUI(pageWithSidebar(
headerPanel("actionButton test"), 
sidebarPanel(
numericInput("n", "N:", min = 0, max = 100, value = 50), 
br(), 
actionButton("goButton", "Disabled!"), 
actionButton("goButton2", "Go!"), 
bsTooltip("goButton", "Tooltip broken", placement = "bottom", trigger = "hover", 
      options = NULL), 
bsTooltip("goButton2", "Tooltip works", placement = "bottom", trigger = "hover", 
      options = NULL) 
), 
mainPanel(useShinyjs(), 
verbatimTextOutput("nText") 
) 
)) 

server.R

library(shiny) 
library(shinyjs) 
library(shinyBS) 

shinyServer(function(input, output,session) { 

ntext <- eventReactive(input$goButton, { 
input$n 
}) 

shinyjs::disable("goButton2") 

output$nText <- renderText({ 
ntext() 
}) 
}) 

回答

5

這是解決你的問題,通過提供標題到您的按鈕,而不是一個辦法:

rm(list=ls()) 
library(shinyBS) 
library(shiny) 
library(shinyjs) 

ui <- pageWithSidebar(
    headerPanel("actionButton test"), 
    sidebarPanel(numericInput("n", "N:", min = 0, max = 100, value = 50), 
    tags$div(style="display:inline-block",title="Tooltip broken",actionButton("goButton", "Disabled!")), 
    tags$div(style="display:inline-block",title="Tooltip works",actionButton("goButton2", "Go!"))  
), 
    mainPanel(useShinyjs(), 
      verbatimTextOutput("nText") 
) 
) 

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

    ntext <- eventReactive(input$goButton, {input$n}) 
    shinyjs::disable("goButton2") 
    output$nText <- renderText({ntext()}) 

}) 
shinyApp(ui = ui, server = server) 

enter image description here

+0

Thanks fo fo解決方案 - 僅僅是我還是偶爾延遲顯示此方法的工具提示? – Iain

+0

是的,會有延遲,你可以用自定義'JS'覆蓋這個,但我不認爲它是必要的 –