在Shiny R中,有没有一种方法可以在没有延迟(带有renderUI的问题)的observeEvent之后立即运行observeEvent? [英] In Shiny R, is there a way to run an observeEvent right after an observeEvent with no delay (issue with renderUI)?

查看:117
本文介绍了在Shiny R中,有没有一种方法可以在没有延迟(带有renderUI的问题)的observeEvent之后立即运行observeEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的标题有点令人困惑,但让我解释一下.

Ok my title is a little bit confusing but let me explain.

我正在使用renderUI来获取音频标签,但是,我想以较低的音量启动音频,因为它太响了.

I am using renderUI to get an audio tag, however, I want to start the audio at a lower volume because it is simply too loud.

下面的代码可以正常工作,除了我添加了延迟之外,它从较高的音量开始并迅速转到较低的音量,但是仍然非常引人注目.尝试减少延迟不会有帮助.如果取消延迟,则两个watchEvents将同时运行,并且不会更改音量.如果我将js $ runs标记(降低了音量)移动到第一个watchEvent内,则也将无效.我认为这是因为renderUI不会真正呈现,直到observeEvent完全完成为止.另外,我认为我无法删除renderUI,因为在我的完整应用程序中,我需要用户输入来播放音频.

The code below works fine, except because I added the delay it starts off at a higher volume and quickly goes to the lower volume but it is still very noticeable. Lowering the delay does not help, I've tried. If I remove the delay, the two observeEvents will run simultaneously and the volume will not be changed. If I move the js$runs tag (which lowers the volume) inside the first observeEvent, it won't work either. I think this is because renderUI does not actually render until the observeEvent has fully completed. Also, I don't think I can remove the renderUI because in my full app, I take a user input to play the audio.

有没有办法以较低的音量初始启动音频标签? 还是有一种方法可以立即运行renderUI从而不会造成延迟?

Is there a way to initially start the audio tag at a lower volume? Or is there a way to run the renderUI immediately so that there will be no delay?

感谢所有帮助,谢谢.

library(shiny)
library(shinyjs)


jsCode <- 'shinyjs.runs = function setHalfVolume() {document.getElementById("myaudio").volume = 0.2;}'

get_audio <- function(){
    tags$audio(id = "myaudio", controls = NA, autoplay = NA, tags$source(src="aud.mpeg"))
}


ui <- fluidPage(
    useShinyjs(),
    extendShinyjs(text = jsCode),

    uiOutput("my_audio"),

    actionButton("guessbutton", "Submit")
)

server <- function(input, output) {
    observeEvent(input$guessbutton, {
        output$my_audio <- renderUI(get_audio())
        #js$runs()
    })

    observeEvent(input$guessbutton, {
        delay(100, js$runs())
    })
}

shinyApp(ui = ui, server = server)

推荐答案

据我所知,您需要删除renderUI才能使它起作用.否则,当您尝试立即减小音量时,音频标签的id将不存在(请参见浏览器的控制台以获取相应的错误消息).

As far as I can tell you'll need to drop renderUI for this to work. Otherwise the id of your audio tag won't be existing, when you try to reduce the volume immediately (see your browser's console for according error messages).

我建议您仅生成一次音频标签,并减少应用程序或会话启动时的音量,同时动态更改音频标签的src参数.

I'd suggest you generate the audio tag only once and reduce the volume on app or session start-up, while dynammically changing the src argument of the audio tag.

请参阅以下内容:

library(shiny)
library(shinyjs)

if(!dir.exists("www")){
    dir.create("www")
}

if(!file.exists("./www/Flamenco.ogg")){
    # for license see: https://commons.wikimedia.org/wiki/File:JCZA_-_JCzarnecki-Flamenco.ogg
    download.file("https://upload.wikimedia.org/wikipedia/commons/7/76/JCZA_-_JCzarnecki-Flamenco.ogg", destfile = "./www/Flamenco.ogg", mode = "wb") 
}

ui <- fluidPage(
    useShinyjs(),
    tags$audio(id = "myaudio", controls = NA, autoplay = NA, src = ""),
    p(),
    actionButton("guessbutton", "Submit")
)

server <- function(input, output) {
    runjs("document.getElementById('myaudio').volume = 0.2;") # initially reduce volume
    observeEvent(input$guessbutton, {
        runjs(sprintf("document.getElementById('myaudio').src = '%s';", "Flamenco.ogg")) # dynamically change src, replace "Flamenco.ogg" with your user input
    })
}

shinyApp(ui = ui, server = server)

PS:如果要隐藏用户的音频标签直到按下该按钮,则可以使用Shinyjs的hiddenhideshowconditionalPanel.

PS: you can use shinyjs' hidden, hide and show or conditionalPanel if you want to hide the audio tag from your user until that button is pushed.

这篇关于在Shiny R中,有没有一种方法可以在没有延迟(带有renderUI的问题)的observeEvent之后立即运行observeEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆