我有一个在ui.R
中声明的变量cond1 <- 0
如果在server.R中满足某些条件,我想更新
cond1 <- 1
我在server.R
中尝试了以下操作observer({
###if certain conditions are met, update value of cond1
updateTextInput(session,"cond1",value=1)})
想法是有一个if语句
if(cond1==1){
#display panel in ui.R}
答案 0 :(得分:2)
你可以conditionalPanels
,使用renderUI
或使用jQuery,这是所有这些的例子:
library(shiny)
shinyApp(
ui = shinyUI(
fluidPage(
tags$head(
tags$script(
HTML(
'
Shiny.addCustomMessageHandler("toggleUI",function(message){
if( message.show ){
$("#"+message.selector).show();
}
else{
$("#"+message.selector).hide();
}
})
'
)
)
),
sidebarLayout(
sidebarPanel(
selectInput("selection","Select UI",choices = c("ui1"="ui1","ui2"="ui2","ui3"="ui3"))
),
mainPanel(
uiOutput("ui1"),
conditionalPanel(
condition = "input.selection == 'ui2'",
sliderInput("breakCount", "Break Count", min=1, max=1000, value=10)
),
fluidRow( id="ui3",
div("Some more here")
)
)
)
)
),
server = shinyServer(function(input,output,session){
observeEvent(input$selection,{
# Get selected value
sel.val <- input$selection
# If selection = ui1
if (sel.val == 'ui1'){
output$ui1 <- renderUI({
plotOutput('plt')
})
output$plt <- renderPlot({plot(runif(100))})
} else{
# Remove ui1
output$ui1 <- renderUI({ NULL})
}
# Toggle ui3
if (sel.val == 'ui3'){
session$sendCustomMessage(type='toggleUI', message=list(selector='ui3',show=TRUE))
} else{
session$sendCustomMessage(type='toggleUI', message=list(selector='ui3',show=FALSE))
}
})
})
)