簡體   English   中英

R /光澤中的conditionalPanel

[英]conditionalPanel in R/shiny

在conditionalPanel上快速詢問是否有光澤/ R。

使用來自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,並且在沒有顯示圖表的情況下,文本“底部”沒有向上移動。

問題是:有沒有一種方法可以設置我的條件面板,使其根據繪圖函數是否返回NULL來顯示/隱藏該條件面板? 我問的原因是因為觸發器有點復雜(除其他因素外,它取決於輸入文件的選擇,因此,如果加載了其他文件,則需要更改),並且我想避免對它進行編碼在ui.R文件上。

歡迎任何建議,

菲利普

嗨,您可以像這樣在服務器中為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在該函數中的某處調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM