gpt4 book ai didi

R Shiny 的 observeEvent 在模块中不起作用

转载 作者:行者123 更新时间:2023-12-05 05:16:14 26 4
gpt4 key购买 nike

预先感谢您的帮助。我试图将我的问题归结为尽可能简单的应用程序。我有一个调用 observeEvent 并返回一些 UI 元素的函数 sliderResetInput。我可以在模块外使用 sliderResetInput(将在下面演示),但我不能在模块内使用它。我认为这是在我的输入周围包装命名空间的问题,但我认为 ID 都在这里 checkout 。这是怎么回事?请注意,虽然这种结构对于这个应用程序来说似乎不必要地复杂,但我需要一个更大的应用程序的结构。

这个有效:

library(shiny) # Version 1.0.5

sliderResetInput = function(id, input, output, session) {

observeEvent(input[[paste0(id, "_reset_slider")]],
updateSliderInput(session = session, inputId = paste0(id, "_slider"), value = c(0, 10)))

out = list(
sliderInput(inputId = paste0(id, "_slider"), label = "Slider", min = 0, max = 10, value = c(0, 10)),
actionButton(inputId = paste0(id, "_reset_slider"), label = "Reset slider")
)

return(out)
}


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

getSliders = reactive(sliderResetInput(id = "test1", input, output, session))

output$sliders = renderUI(getSliders())

observeEvent(input$browser, browser())
}


ui <- fluidPage(
uiOutput(outputId = "sliders"),
actionButton(inputId = "browser", "Click me to go into browser()")
)


shinyApp(ui = ui, server = server, options = list(launch.browser = T))

使用相同的 sliderResetInput() 函数,这不起作用:

library(shiny) # Version 1.0.5

sliderResetInput = function(id, input, output, session) {

observeEvent(input[[paste0(id, "_reset_slider")]],
updateSliderInput(session = session, inputId = paste0(id, "_slider"), value = c(0, 10)))

out = list(
sliderInput(inputId = paste0(id, "_slider"), label = "Slider", min = 0, max = 10, value = c(0, 10)),
actionButton(inputId = paste0(id, "_reset_slider"), label = "Reset slider")
)

return(out)
}


myModuleUI = function(id) {
ns = NS(id)

tagList(
uiOutput(outputId = ns("sliders"))
)
}



myModule = function(input, output, session) {
ns = session$ns

getSliders = reactive(sliderResetInput(id = ns("test1"), input, output, session))

output$sliders = renderUI(getSliders())
}

server <- function(input, output, session) {
callModule(myModule, id = "A")
observeEvent(input$browser, browser())
}


ui <- fluidPage(
myModuleUI(id = "A"),
actionButton(inputId = "browser", "Click me to go into browser()")
)


shinyApp(ui = ui, server = server, options = list(launch.browser = T))

有人知道这是怎么回事吗?

感谢您的宝贵时间!

最佳答案

您只需为小部件的 ID 添加前缀。即:

sliderResetInput = function(id, NSid, input, output, session) {

observeEvent(input[[paste0(id, "_reset_slider")]], {
updateSliderInput(session = session, inputId = paste0(id, "_slider"), value = c(0, 10))
})

out = list(
sliderInput(inputId = paste0(NSid, "_slider"), label = "Slider", min = 0, max = 10, value = c(0, 10)),
actionButton(inputId = paste0(NSid, "_reset_slider"), label = "Reset slider")
)

return(out)
}

  getSliders = reactive(sliderResetInput(id = "test1", NSid = ns("test1"), input, output, session))

关于R Shiny 的 observeEvent 在模块中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com