闪亮应用中的 ggplot:return 空行

ggplot in shiny app: return a blank line

我正在尝试将条件趋势线添加到绘图中。下面的代码在选中 1 个或 2 个复选框时有效,但在选中 0 个复选框时无效。如何 return 仍然满足参数的空白或空行?我试过 returning geom_blank() 和 returning NULL.

选择0个复选框时,下面的代码return s错误:

Warning in is.na(e2) :
  is.na() applied to non-(list or vector) of type 'NULL'
Warning: Error in eval: incorrect length (0), expecting: 100

我应该补充一点,我的真实应用程序有许多不同的复选框组,因此在实际的情节创建中添加条件是不可行的。

app.R

library(shiny)
library(ggplot2)
library(plotly)

df1 <- as.data.frame(list('user'=c(rep('A',50),rep('B',50)),'x'=1:100,'y'=rnorm(100)))

ui <- fluidPage(
  sidebarLayout(
    # select a user
    sidebarPanel(
      checkboxGroupInput("userInput","User",
                  choices=sort(unique(df1$user)),
                  selected='A')
    ),
    # plot selected user in plotly
    mainPanel(
      plotlyOutput("mainPlot")
    )
  )
)

server <- function(input, output, session) {

  output$mainPlot <- renderPlotly({

    # filter based on selected user
    filteredForUser <- reactive({
      df1 %>%
        filter(
          user == input$userInput
        )
    })
    # add a trend line based on user
    addTrendLine <- reactive({
      if (is.null(filteredForUser())) {
        return(geom_blank())
        }
      g <- geom_smooth(data=filteredForUser(), aes(x=x,y=y), method='lm')
      return(g)
    })

    # create a plot
    g <- ggplot(data=df1, aes(x=x,y=y)) + geom_line() + addTrendLine()
    ggplotly(p=g)
  })
}

shinyApp(ui, server)

问题是 filteredForUser 的反应式没有处理 null 情况。此外,您需要有条件地添加到 ggplot 对象。试试这个代码,或者它的一些变体:

server <- function(input, output, session) {
  output$mainPlot <- renderPlotly({
    # filter based on selected user
    filteredForUser <- reactive({
      if (is.null(input$userInput)) {return(df1)}
      df1 %>%
        filter(
          user == input$userInput
        )
    })
    # add a trend line based on user
    getPlot <- reactive({
      g <- ggplot(data = df1, aes(x=x, y=y)) + geom_line() 
      if (nrow(filteredForUser()) == nrow(df1)) {
        return(g)
      }
      g <- g + geom_smooth(data=filteredForUser(), aes(x=x, y=y), method='lm')
      return(g)
    })

    # create a plot
    g <- getPlot()
    ggplotly(p=g)
  })
}

这是另一种解决方法,因为您不想在反应性输入上使用条件。在此示例中,当 none 个复选框被勾选时,df1 %>% filter(user == input$unserInput) returns 出错。

我只是更改了代码的 is.null() 部分以检查 filteredForUser 的 class。

输出类似于@Gopala 的解决方案。希望这对你有用?

服务器功能

server <- function(input, output, session) {

  output$mainPlot <- renderPlotly({

    # filter based on selected user
    filteredForUser <- reactive({
      try(
      df1 %>%
        filter(
          user == input$userInput
        ), silent = T)
    })

    # add a trend line based on user
    addTrendLine <- reactive({
      if (class(filteredForUser()) == "try-error") {
        return(geom_blank())
      }else{
      g <- geom_smooth(data=filteredForUser(), aes(x=x,y=y), method='lm')
      return(g)
      }
    })

    # create a plot
    g <- ggplot(data=df1, aes(x=x,y=y)) + geom_line() + addTrendLine()
    ggplotly(p=g)
  })
}