如果您在 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 时才会显示该图。否则,该图(以及空白 - 此处使用darkly
shinythemes 中的主题 - 不存在)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)