【问题标题】:ggplot and rshiny plot is not refreshedggplot 和 rshiny 绘图未刷新
【发布时间】:2013-07-15 16:47:12
【问题描述】:

我有一个依赖于名为datasetInput 的反应函数的输出图。每当我更改输入变量时,输出函数都会更新,但我在客户端上看到的图最终并没有改变。我输出了 ggplot 用来生成绘图的数据,并且数据正在发生变化。我不确定发生了什么。

datasetInput <- reactive({    
    data <- input$globalData
    table <- c()
    ...
    table
})

output$plot <- renderPlot({    
    table <- datasetInput()   
    cat('32hr: ',unlist(table[which(table$group=='32hr'),3]),'\n')     
    cat('24hr: ',unlist(table[which(table$group=='24hr'),3]),'\n')     
    range <- max(table$centroidDistances.RG) - min(table$centroidDistances.RG)
    cat('range: ',range,'\n')
    plot <- ggplot(table,aes(x=table$centroidDistances.RG,fill=table$group)) + 
        geom_histogram(aes(y=..density..),pos="dodge") +
        geom_density(alpha=0.2)
    print(plot)
},height=300,width=600)

我之前没有见过这个问题,当output$plot发生变化时如何让客户端改变它的输出(这是ggplot的特定问题吗?)

【问题讨论】:

  • 猜测cat 语句的输出会干扰renderPlot 绘图输出。你能把它们移到语句之外,看看情节是否改变了吗?另外,我不知道您可以在花括号之外调用heightweight。我以为你必须用 }) 关闭响应式输出?
  • 我认为 cat 语句根本不会影响输出,它们在花括号内或花括号外的行为相同。
  • 高度和宽度也可以在右大括号之外。它们只是 renderPlot 的更多参数。

标签: r ggplot2 shiny


【解决方案1】:

这个问题似乎可以通过在 ggplot 中设置 environment = environment() 来解决:

ggplot(table,aes(x=table$centroidDistances.RG,fill=table$group),environment=environment()) + 
  geom_histogram(aes(y=..density..),pos="dodge",binwidth=range/30,minx=0,width=600) +
  geom_density(alpha=0.2)

不确定为什么这会解决问题,但包含此环境设置会使图形在客户端按预期更新。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2021-10-09
    • 1970-01-01
    • 2016-02-26
    • 2012-09-14
    • 2021-07-16
    • 2018-06-27
    • 2015-12-26
    • 1970-01-01
    相关资源
    最近更新 更多