【问题标题】:ggplot and shiny: ggplot doesn't know how to deal with reactive: yes it is NOT a repeatggplot 和闪亮:ggplot 不知道如何处理反应:是的,它不是重复
【发布时间】:2018-03-23 21:03:09
【问题描述】:

我正在尝试将 ggplot2 与闪亮的反应对象一起使用。 所以,我知道反应提供了一个需要更改为值的反应对象,但我试图在反应部分和 renderPlot 部分将它分配给不同的对象,但没有得到任何结果......

所以,如果我不将它分配给不同的变量,我会收到消息:

ggplot2 doesn't know how to deal with data of class 
reactiveExpr/reactive

我知道至少有 2 个问题(herehere)讨论了这个问题。我确实做了功课并尝试使用这些答案,但没有奏效。据我所知,这些答案建议将反应对象分配给另一个变量,我这样做了,并得到了消息:

Error: object is not a matrix

那里描述的答案并没有真正解释如何解决一般问题,它们只是提供了修复该特定示例的代码(一个答案是“我认为应该使用以下代码解决这个特定问题:”但没有'不要指出代码的哪一部分解决了问题)。

您能否解释一下,在更一般的情况下如何在此示例中解决此问题?

这是一个最小的例子:

服务器.R

library(shiny)
shinyServer(function(input, output) {
  library(ggplot2)
  library(lme4)
  model1 <- lmList((conc) ~ time | Subject, data = Indometh)
  newpoints <- reactive({data.frame("Subject" = c(1, 4, 2, 5, 6, 3),
                          "conc" = predict(model1, newdata = data.frame(time=input$sliderTime)),
                          "time" = rep(input$sliderTime, 6))
  })

  output$myPlot <- renderPlot({
    newpoints2 <- newpoints()
    g <- ggplot(Indometh, aes(time, (conc), color= Subject)) + geom_point()  +
      stat_smooth(method="lm",fullrange=TRUE, fill=NA)
    newg <- g + geom_point(data = newpoints2, mapping =
                             aes(x = time, y = (conc), color= factor(Subject)))
    print(newg)

  })

})

还有 ui.R 文件:

library(shiny)
shinyUI(fluidPage(

  # Application title
  titlePanel("Concentration of Indomethacin"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("slidertime",
                  "Time:",
                  min = 8,
                  max = 15,
                  value = 8)),

    mainPanel(
       plotOutput("myPlot")
    )
  )
))

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    您的示例的问题是您使用错误的名称调用输入它不是sliderTime 它是slidertime 我将服务器更改为此并且它运行完美。

    我该如何解决?只需在反应式表达式中使用browser(),发现input$sliderTime 为NULL,所以我检查了名称。

    library(shiny)
    shinyServer(function(input, output) {
      library(ggplot2)
      library(lme4)
      model1 <- lmList((conc) ~ time | Subject, data = Indometh)
      newpoints <- reactive({
        data.frame("Subject" = c(1, 4, 2, 5, 6, 3),
                   "conc" = predict(model1, newdata = data.frame(time=input$slidertime)),
                   "time" = rep(input$slidertime, 6))
      })
    
      output$myPlot <- renderPlot({
    
        newpoints2 <- newpoints()
        g <- ggplot(Indometh, aes(time, (conc), color= Subject)) + geom_point()  +
          stat_smooth(method="lm",fullrange=TRUE, fill=NA)
        newg <- g + geom_point(data = newpoints2, mapping =
                                 aes(x = time, y = (conc), color= factor(Subject)))
        print(newg)
    
      })
    
    })
    

    【讨论】:

    • 有趣的解决方案,我不知道你可以在闪亮的会话中使用browser
    • 是的,这就像我发现调试闪亮应用程序的唯一方法,特别是如果一切都是反应式的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2019-05-23
    • 2021-02-02
    • 2021-02-13
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多