如何使用server.R中的observer更新ui.R中的变量

如何使用server.R中的observer更新ui.R中的变量,r,shiny,R,Shiny,我有一个在ui.R中声明的变量 cond1 <- 0 这个想法是要有一个if语句 if(cond1==1){ #display panel in ui.R} 您可以使用conditionalPanel、使用renderUI或使用jQuery,以下是所有这些工具的示例: library(shiny) shinyApp( ui = shinyUI( fluidPage( tags$head( tags$script( HTML(

我有一个在ui.R中声明的变量

cond1 <- 0
这个想法是要有一个if语句

if(cond1==1){
#display panel in ui.R}

您可以使用
conditionalPanel
、使用
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))
      }
    })
  })
) 
库(闪亮)
shinyApp(
ui=shinyUI(
流动摄影(
标签$head(
标记$script(
HTML(
'
addCustomMessageHandler(“toggleUI”,函数(消息){
如果(message.show){
$(“#”+message.selector).show();
}
否则{
$(“#”+message.selector).hide();
}
})
'
)
)
),
侧边栏布局(
侧栏面板(
selectInput(“选择”、“选择UI”,选项=c(“ui1”=“ui1”、“ui2”=“ui2”、“ui3”=“ui3”))
),
主面板(
uiOutput(“ui1”),
条件板(
condition=“input.selection=”ui2“,
sliderInput(“中断计数”,“中断计数”,最小值=1,最大值=1000,值=10)
),
fluidRow(id=“ui3”,
div(“这里还有一些”)
)
)
)
)
),
服务器=shinyServer(功能(输入、输出、会话){
observeEvent(输入$selection{
#获取选定值

sel.val我不确定你想做什么,但这可能是一种错误的方法。如果ui只包含接口内容,而服务器包含逻辑,那么ui和服务器的意义就在于此。你不应该在ui中声明和修改变量。你尝试做的事可能是通过
conditionalPanel()实现的
,看看这个功能Hi Oskar,我希望你做得很好。我有一个新问题,我想你可以帮忙。我非常感谢你的帮助和你的时间。谢谢。
if(cond1==1){
#display panel in ui.R}
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))
      }
    })
  })
)