gpt4 book ai didi

在调用依赖于 renderUI 的响应式(Reactive)函数之前,不会执行 renderUI

转载 作者:行者123 更新时间:2023-12-04 10:27:59 25 4
gpt4 key购买 nike

在我 Shiny 的应用程序中,我有两个选项卡:选项卡 1 有一个 checkboxInput 和一个 selectInput,它在 server.R 中编码为 renderUI,并且仅在选中该框时显示。在选项卡 2 中,只有在选项卡 1 中显示 selectInput 时,才会有一个 ggvis 函数来绘制通过 react 函数生成的数据框。

出乎意料的是,selectInput 没有显示在选项卡 1 中,除非我先单击选项卡 2 然后返回选项卡 1,即使 selectInput 仅依赖于同一选项卡中的复选框,即选项卡 1。

显然,我没有正确理解响应式(Reactive)函数的概念。你能指出我的错误在哪里吗?谢谢!

附言结构相当复杂,但它是我的“真实”应用程序所需要的。

ui.R

library(ggvis)

shinyUI(navbarPage("",
tabPanel("1",
checkboxInput("START", label = "Start calculations", value = F),
htmlOutput("SELECT")
),
tabPanel("2",
ggvisOutput("PLOT")
)
))

server.R
library(ggvis)

shinyServer(function(input, output, session) {

output$SELECT<-renderUI({
if (input$START==T) {
selectInput("SELECTINPUT","Make your choice:",c(1,2))
}
})

PLOTDF<-reactive({
if (is.null(input$SELECTINPUT)==F) {
plotdf<-data.frame(c(1,1,2,2),c(1,2,3,4))
colnames(plotdf)<-c("X","Y")
plotdf
}
})

reactive({
PLOTDF() %>% ggvis(~X,~Y) %>% layer_points
}) %>% bind_shiny("PLOT")

})

最佳答案

不确定是什么 is.null(input$SELECTINPUT)==F正在做 我猜你想要类似 !is.null(input$SELECTINPUT) 的东西也可以包装 ggvis调用观察者并检查输入

以下对我有用:

library(ggvis)
library(shiny)
runApp(list(ui = navbarPage("",
tabPanel("1",
checkboxInput("START", label = "Start calculations", value = F),
htmlOutput("SELECT")
),
tabPanel("2",
ggvisOutput("PLOT")
)
)
, server = function(input, output, session) {

output$SELECT<-renderUI({
if (input$START==T) {
selectInput("SELECTINPUT","Make your choice:",c(1,2))
}
})
PLOTDF<-reactive({
if (!is.null(input$SELECTINPUT)) {
plotdf<-data.frame(c(1,1,2,2),c(1,2,3,4))
colnames(plotdf)<-c("X","Y")
plotdf
}
})
observe({
if (!is.null(input$SELECTINPUT)) {
PLOTDF() %>% ggvis(~X,~Y) %>% layer_points%>% bind_shiny("PLOT")
}
})
}
)
)

关于在调用依赖于 renderUI 的响应式(Reactive)函数之前,不会执行 renderUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266948/

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