如何处理尚未准备好显示的闪亮图

问题描述 投票:0回答:1

考虑以下玩具示例

library(shiny)
library(dplyr)
library(magrittr)
library(shinyWidgets)

data(mtcars)
cylinders <- unique(mtcars$cyl)


# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("MWE"),

    # Sidebar  
    sidebarLayout(
        sidebarPanel(
          pickerInput("cylinders",
                        "Number of cylinders:",
                        choices = cylinders,
                        options = list(`actions-box` = TRUE),
                        multiple = TRUE)
        ),

        # Show a plot
        mainPanel(
           plotOutput("mpg")
        )
    )
)

# Define server logic required
server <- function(input, output) {

    
    output$mpg <- renderPlot({
        plotData <- mtcars %>% filter(cyl == input$cylinders)
        mpgPlot <- plotData %>% ggplot(aes(x=hp, y = mpg)) + geom_point()
        print(mpgPlot)
        
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

当您第一次运行您看到的应用程序时

有没有一种“优雅”的方式可以在绘图输出中显示除错误消息之外的其他内容,直到绘图准备好为止?

在我的实际情况中,我需要用户从 selectInput 中进行选择。根据该选择,一组允许的值将添加到第二个 selectInput。一旦从该列表中做出选择,就可以生成情节......

r ggplot2 shiny
1个回答
0
投票

req() 函数

提供在继续之前可用的值 计算或行动。如果任何给定值不真实,则 通过引发“静默”异常来停止操作

按如下方式将其应用于您的绘图将默默地停止绘图渲染,直到用户选择

input$cylinders
并变为“真实”

output$mpg <- renderPlot({
    req(input$cylinders)
    plotData <- mtcars %>% filter(cyl == input$cylinders)
    mpgPlot <- plotData %>% ggplot(aes(x=hp, y = mpg)) + geom_point()
    print(mpgPlot)
    
  })
© www.soinside.com 2019 - 2024. All rights reserved.