[英]How to display default plot in www/ before actionButton is clicked R shiny
[英]R Shiny: How not to display plot when NULL
如果您在 R 工作室中運行此代碼,您會發現 NULL 數據的繪圖仍然是一大塊白色。
當數據為NULL時,我們怎么能不顯示它。
在其他圖表中,大白板看起來不太好。
library(shiny)
server <- function(input, output) {
output$x = renderPlot(NULL)
}
ui <- fluidPage(
br(),
plotOutput("x"),
tags$head(tags$style(HTML("
body {
margin: 0;
font-family: helvetica, sans-serif;
background: #F2F0F0;
}
.shiny-plot-output{
max-width: 100%;
box-shadow: 0px 0px 6px #888;
margin-left: auto;
margin-right: auto;
}
")))
)
shinyApp(ui = ui, server = server)
我找到了另一種解決方案(因為上面的解決方案對我不起作用)。 它使用了shinyjs
包。 這是一個最小的可重現示例。
library(shinyjs)
library(shinythemes)
shinyApp(
ui = fluidPage(theme = shinytheme("darkly"),
useShinyjs(), # IMPORTANT, you have to call this function in your UI to use shinyjs
numericInput("num","Number", 5),
plotOutput("text1")
),
server = function(input, output) {
observe({
if (input$num < 5) {
hide("text1")
} else {
show("text1")
}
})
output$text1 <- renderPlot({
plot(rnorm(input$num))
})
}
)
該圖僅在 x > 5 時顯示。否則該圖(以及空白 - 此處通過使用 Shinythemes 中的darkly
主題突出顯示 - 不存在)
你可以在 server.R 中做這樣的事情:
if(is.null(df)){}
else{
output$x = renderPlot(df)
}
這將檢查您發送到renderPlot
的數據是否為 NULL,並且僅在數據存在時才執行renderPlot
。 這可能會使畫布不會顯示為白色,而是顯示為灰色背景。 唯一的問題是您正在制作陰影效果,該效果也會出現在灰色上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.