闪亮应用程序中的r-ggplot:返回一行空白



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

选中0复选框后,下面的代码返回错误:

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)

这是另一种方法,因为您不想在反应输入上使用条件词。在本例中,当没有勾选任何复选框时,df1 %>% filter(user == input$unserInput)将返回一个错误。

我只是简单地更改了代码的is.null()部分,以检查filteredForUser的类。

输出类似于@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)
  })
}

问题是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)
  })
}

最新更新