在我闪亮的应用程序中,我有两个选项卡:选项卡 1 有一个 checkboxInput 和一个 selectInput,它在 server.R 中编码为 renderUI,并且仅在选中该框时才显示。在突片2中,存在GGVIS功能,用于绘制通过在选项卡1中所示的选择input时通过反应函数进行的数据帧。
出乎意料的是,selectInput 没有显示在选项卡 1 中,除非我先单击选项卡 2 然后返回选项卡 1,即使 selectInput 仅依赖于同一选项卡中的复选框,即选项卡 1。
显然,我没有正确理解反应式函数的概念。你能指出我的错误在哪里吗?谢谢!
PS 结构相当复杂,但它是我的“真实”应用程序所需要的。
用户界面
library(ggvis)
shinyUI(navbarPage("",
tabPanel("1",
checkboxInput("START", label = "Start calculations", value = F),
htmlOutput("SELECT")
),
tabPanel("2",
ggvisOutput("PLOT")
)
))
Run Code Online (Sandbox Code Playgroud)
服务器
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")
})
Run Code Online (Sandbox Code Playgroud)
不确定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")
}
})
}
)
)
Run Code Online (Sandbox Code Playgroud)