2017-05-12 2 views
5

プロット/リアクションエレメントがロードされているときに光沢のあるボタンを無効にすることはできますか? shinyjsは入力要素を無効にして有効にすることができますが、負荷プロット/リアクティブ要素への接続を設定する方法はわかりません。この例はSingle-file shiny apps pageに基づいています。私はちょうどボタンと孤立した部分を追加しました。 shinyjsに基づいていないソリューションも評価されています:)このようなプロットがロードされているときに光沢のあるボタンを無効にする

library(shiny) 
server <- function(input, output) { 
    output$distPlot <- renderPlot({ 
    input$button 

    isolate(
     hist(rnorm(input$obs), col = 'darkgray', border = 'white') 
    ) 
    }) 
} 

ui <- fluidPage(
    shinyjs::useShinyjs(), 
    sidebarLayout(
    sidebarPanel(
     sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100), 
     actionButton("button", "OK!") 
    ), 
    mainPanel(plotOutput("distPlot")) 
) 
) 

shinyApp(ui = ui, server = server) 
+0

別の簡単な方法は、[使用REQ](https://shiny.rstudio.com/articles/req.html)にあるので、いくつかのデータが準備されていないとき、それはあなたのニーズに合うかどうかは、動作しません。 – dracodoc

答えて

3

何か?

library(shiny) 
library(shinyjs) 
server <- function(input, output) { 

    PlotData <- eventReactive(input$button,{ 
    disable("button") 
    Sys.sleep(2) 
    hist(rnorm(input$obs), col = 'darkgray', border = 'white') 
    enable("button") 
    }) 

    output$distPlot <- renderPlot({ 
    PlotData() 
    }) 
} 

ui <- fluidPage(
    shinyjs::useShinyjs(), 
    sidebarLayout(
    sidebarPanel(
     sliderInput("obs", "Number of observations:", min = 10, max = 1000, value = 2000), 
     actionButton("button", "OK!") 
    ), 
    mainPanel(plotOutput("distPlot")) 
) 
) 

shinyApp(ui = ui, server = server) 
+0

はい、それは私が探していたものです!プロットが 'eventReactive'の一部であることを知らなかった。あなたの速い応答に感謝します! –

3

次のように、時間を設定する必要はありません。このボタンは計算中のみ無効になります。

library(shiny) 

js <- " 
$(document).ready(function() { 
    $('#distPlot').on('shiny:recalculating', function() { 
    $('button').prop('disabled', true); 
    $('button').css('color', 'red'); 
    }); 
    $('#distPlot').on('shiny:recalculated', function() { 
    $('button').prop('disabled', false); 
    $('button').css('color', 'black'); 
}); 
}); 
" 

server <- function(input, output) { 

    output$distPlot <- renderPlot({ 
    hist(rnorm(input$obs), col = 'darkgray', border = 'white') 
    }) 
} 

ui <- fluidPage(
    tags$head(tags$script(HTML(js))), 
    sidebarLayout(
    sidebarPanel(
     sliderInput("obs", "Number of observations:", min = 10000, max = 100000, value = 20000), 
     actionButton("button", "OK!") 
    ), 
    mainPanel(plotOutput("distPlot")) 
) 
) 

shinyApp(ui = ui, server = server) 
+0

また、素晴らしいソリューションです、ありがとう! :) –

関連する問題