gpt4 book ai didi

R Shiny : How not to display plot when NULL

转载 作者:行者123 更新时间:2023-12-03 20:29:21 27 4
gpt4 key购买 nike

如果您在 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 主题突出显示 - 不存在)

关于R Shiny : How not to display plot when NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34428130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com