当我开始一个新的R会话时,下面的代码无法正常工作:当我按下' next'第一次按钮,计数器上升一个增量,但条件面板不会改变。如果我在同一个会话中重新加载应用程序,没有问题。
发生了什么事?任何帮助将不胜感激!
shinyUI(
fluidPage(
fluidRow(
column(12,
fluidRow(
h5(" "),
HTML("<br>"),
column(width = 4, style="height:360px",
tabsetPanel(
tabPanel(h4("Financial",style = "color:black"),
conditionalPanel(width=4,condition="output.Fpanel==undefined|output.Fpanel==0",
h5("Intro to finance",style="color:grey")),
conditionalPanel(width=4,condition="output.Fpanel==1",#|"output.Fpanel==undefined",
h5("1. Description")),
conditionalPanel(width=4,condition="output.Fpanel==2",#|"output.Fpanel==undefined",
h5("2. Products")),
conditionalPanel(width=4,condition="output.Fpanel==3",#|"output.Fpanel==undefined",
h5("3. HR")),
conditionalPanel(width=4,condition="output.Fpanel==4",#|"output.Fpanel==undefined",
h5("4. Reporting")),
value=1),
id = "tabs1"
)
),
column(width = 8,style="height:360px",
HTML("<br><br>"),
hr(),
# --------- Fishery panel guides ---------------------------------------------------------------------------------------------------------------
conditionalPanel(condition="input.tabs1==1&output.Fpanel==0",
h5("",style = "color:grey")
),
conditionalPanel(condition="input.tabs1==1&output.Fpanel==1",
h5("Some text for panel 1",style = "color:grey")
),
conditionalPanel(condition="input.tabs1==1&output.Fpanel==2",
h5("Some text for panel 2",style = "color:grey")
),
conditionalPanel(condition="input.tabs1==1&output.Fpanel==3",
h5("Some text for panel 3",style = "color:grey")
),
conditionalPanel(condition="input.tabs1==1&output.Fpanel==4",
h5("Some text for panel 4",style = "color:grey")
)
)
)
),
column(12,style="height:80px",
fluidRow(
column(width = 1,
conditionalPanel(condition="(input.tabs1==1 & output.Fpanel>1)",
actionButton("Fback","< Back")
),
conditionalPanel(condition="!(input.tabs1==1 & output.Fpanel>1)",
actionButton("FbackD","< Back",style="color: #CFCFCF; border-color: #CFCFCF") #background-color: #CFCFCF;
)
),
column(width = 1,
conditionalPanel(condition="(input.tabs1==1 & output.Fpanel<4)",
actionButton("Fcont","Next >")
),
conditionalPanel(condition="!(input.tabs1==1 & output.Fpanel<4)",
actionButton("FcontD","Next >",style="color: #CFCFCF; border-color: #CFCFCF") #background-color: #CFCFCF;
)
),
column(width=2,#style="height:180px",
conditionalPanel(condition="output.Fpanel>0|output.Fpanel!=undefined",
#h4("Progress",style="color:grey"),
#hr(),
textOutput("Fpanelout"),
textOutput("Mpanelout"),
textOutput("Dpanelout")
)
),
column(4,offset=1,style="height:80px",
conditionalPanel(condition="output.Fpanel==undefined",
actionButton("start",h5("START",style="color:green"),width=300)
),
conditionalPanel(condition="!(output.Fpanel==undefined)",
actionButton("Calculate",h5("CALCULATE",style="color:red"),width=300)
)
)
)
)
)))
shinyServer(function(input, output, session) {
observeEvent(input$Fback,{
if(input$tabs1==1){
if(Fpanel == 1){
Fpanel<<-1
}else{
Fpanel<<-(Fpanel-1)
}
}
output$Fpanelout <- renderText({ paste(Fpanel,"/ 4")})
})
observeEvent(input$Fcont,{
if(input$tabs1==1){
if(Fpanel == 14){
Fpanel<<-14
}else{
Fpanel<<-(Fpanel+1)
}
}
output$Fpanelout <- renderText({ paste(Fpanel,"/ 4")})
})
output$Fpanel <- eventReactive({
input$Fcont
input$Fback
input$Load
input$start
},{
Fpanel
},ignoreInit=FALSE)
observeEvent(input$start,{
output$Fpanelout <- renderText({ paste(Fpanel,"/ 4")})
})
Fpanel<<-1
outputOptions(output,"Fpanel",suspendWhenHidden=FALSE)
})
答案 0 :(得分:1)
您正在观察已安装的可观察项的执行问题顺序。
两个表达式
output$Fpanel <- eventReactive({
input$Fcont
input$Fback
input$Load
input$start
},{
Fpanel
},ignoreInit=FALSE)
和
observeEvent(input$Fcont,{
if(input$tabs1==1){
if(Fpanel == 14){
Fpanel<<-14
}else{
Fpanel<<-(Fpanel+1)
}
}
output$Fpanelout <- renderText({ paste(Fpanel,"/ 4")})
})
通过input$Fcont
对点击下一个按钮作出反应。您可能会注意到这些执行完全是彼此独立的。但是,你构建了你的逻辑,假设第一个总是在第二个之后触发。
从Shiny docs about execution scheduling,您可以了解可观察端点
如果多个端点无效时发生刷新,则不会 端点将执行的保证订单
因此,有时您的面板没有更新,因为之前的值是(重新)分配而不是下一个值。当一个会话重新启动时,这个问题并不常见,并且可靠地发生。相当不寻常。如果你想自己测试一下,在你的observable中添加一些print
命令来查看它们被调用的顺序。
解决方法是避免这些同时执行模式并创建一个坚实的反应链,无论如何都能保持正确的关系。
下面是一个重构的代码示例,它显示了您希望创建依赖项的方式。我将Fpanel
转换为reactiveVal
并直接对其进行操作,以便Fpanel
始终保持正确的值。 (另外,由于JavaScript端的reactiveVal()
的默认值为null
而不是undefined
,因此我重写了条件,因此0
是新的起始值。)< / p>
library(shiny)
ui <- shinyUI(
fluidPage(
fluidRow(
column(12,
fluidRow(
h5(" "),
HTML("<br>"),
column(width = 4, style="height:360px",
tabsetPanel(id = "tabs1",
tabPanel(value=1,
h4("Financial",style = "color:black"),
conditionalPanel(width=4,condition="output.Fpanel==0",
h5("Intro to finance",style="color:grey")
),
conditionalPanel(width=4,condition="output.Fpanel==1",
h5("1. Description")
),
conditionalPanel(width=4,condition="output.Fpanel==2",
h5("2. Products")
),
conditionalPanel(width=4,condition="output.Fpanel==3",
h5("3. HR")
),
conditionalPanel(width=4,condition="output.Fpanel==4",
h5("4. Reporting")
),
)
)
),
column(width = 8,style="height:360px",
HTML("<br><br>"),
hr(),
# --------- Fishery panel guides ---------------------------------------------------------------------------------------------------------------
conditionalPanel(condition="input.tabs1==1&output.Fpanel==0",
h5("",style = "color:grey")
),
conditionalPanel(condition="input.tabs1==1&output.Fpanel==1",
h5("Some text for panel 1",style = "color:grey")
),
conditionalPanel(condition="input.tabs1==1&output.Fpanel==2",
h5("Some text for panel 2",style = "color:grey")
),
conditionalPanel(condition="input.tabs1==1&output.Fpanel==3",
h5("Some text for panel 3",style = "color:grey")
),
conditionalPanel(condition="input.tabs1==1&output.Fpanel==4",
h5("Some text for panel 4",style = "color:grey")
)
)
)
),
column(12,style="height:80px",
fluidRow(
column(width = 1,
conditionalPanel(condition="(input.tabs1==1 & output.Fpanel>1)",
actionButton("Fback","< Back")
),
conditionalPanel(condition="!(input.tabs1==1 & output.Fpanel>1)",
actionButton("FbackD","< Back",style="color: #CFCFCF; border-color: #CFCFCF") #background-color: #CFCFCF;
)
),
column(width = 1,
conditionalPanel(condition="(input.tabs1==1 & output.Fpanel<4)",
actionButton("Fcont","Next >")
),
conditionalPanel(condition="!(input.tabs1==1 & output.Fpanel<4)",
actionButton("FcontD","Next >",style="color: #CFCFCF; border-color: #CFCFCF") #background-color: #CFCFCF;
)
),
column(width=2,#style="height:180px",
conditionalPanel(condition="output.Fpanel>0",
#h4("Progress",style="color:grey"),
#hr(),
textOutput("Fpanelout"),
textOutput("Mpanelout"),
textOutput("Dpanelout")
)
),
column(4,offset=1,style="height:80px",
conditionalPanel(condition="output.Fpanel==0",
actionButton("start",h5("START",style="color:green"),width=300)
),
conditionalPanel(condition="output.Fpanel>0",
actionButton("Calculate",h5("CALCULATE",style="color:red"),width=300)
)
)
)
)
)
)
)
server <- shinyServer(function(input, output, session) {
Fpanel <- reactiveVal(0)
output$Fpanel <- reactive({ Fpanel() })
outputOptions(output,"Fpanel",suspendWhenHidden=FALSE)
output$Fpanelout <- renderText({ paste(Fpanel(),"/ 4")})
observeEvent(input$start, Fpanel(1))
observeEvent(input$Fback,{
if(input$tabs1 == 1 && Fpanel() > 1){
Fpanel(Fpanel() - 1)
}
})
observeEvent(input$Fcont,{
if(input$tabs1==1 && Fpanel() < 14){
Fpanel(Fpanel() + 1)
}
})
})
shinyApp(ui, server)