R / shiny中的条件面板

对于shiny / R,在conditionalPanel上快速提问。

使用来自RStudio的稍微修改的代码示例,请考虑以下简单的闪亮应用程序:

n <- 200


# Define the UI
ui <- bootstrapPage(
   numericInput('n', 'Number of obs', n),
   conditionalPanel(condition = "input.n > 20",
     plotOutput('plot') ),
   HTML("Bottom")
)

# Define the server code
server <- function(input, output) {
   output$plot <- renderPlot({
      if (input$n > 50) hist(runif(input$n)) else return(NULL)
   })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

我的目标是隐藏图形并向上移动HTML文本以避免出现间隙。 现在,您可以看到,如果输入的值低于20,图形将被隐藏,文本“Bottom”将相应地向上移动。 但是,如果输入的值大于20,但小于50,则图表函数返回NULL,并且在未显示图表的情况下,文本“Bottom”不会向上移动。

问题是:有没有一种方法可以根据plot函数是否返回NULL来设置一个conditionalPanel,使其显示/隐藏? 我问的原因是因为触发器有点复杂(除其他因素外,它取决于输入文件的选择,因此如果加载了不同的文件,则需要更改),并且我想避免必须对其进行编码在ui.R文件上。

任何建议欢迎,

菲利普


嗨,你可以创建一个conditionPanel在服务器中的conditionalPanel是这样的:

n <- 200
library("shiny")

# Define the UI
ui <- bootstrapPage(
  numericInput('n', 'Number of obs', n),
  conditionalPanel(condition = "output.cond == true", # here use the condition defined in the server
                   plotOutput('plot') ),
  HTML("Bottom")
)

# Define the server code
server <- function(input, output, session) {
  output$plot <- renderPlot({
    if (input$n > 50) hist(runif(input$n)) else return(NULL)
  })
  # create a condition you use in the ui
  output$cond <- reactive({
    input$n > 50
  })
  outputOptions(output, "cond", suspendWhenHidden = FALSE)
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

不要忘记在服务器功能中添加session ,并在该功能的某处调用outputOptions

链接地址: http://www.djcxy.com/p/89139.html

上一篇: conditionalPanel in R/shiny

下一篇: How to load an image in Shiny