【发布时间】:2017-08-15 05:46:44
【问题描述】:
我编写了一个闪亮的应用程序,我在其中从泊松分布中采样。每次我想输出数据的图表或摘要时,我都重写了函数。我宁愿只使用该函数一次,然后让所有绘图和摘要都引用该函数被调用的单次,否则输出的结果将不相同。当我尝试调用该函数一次并将结果存储为 ggplot 使用的变量时,我得到了错误,即 ggplot 无法使用反应数据并且我无法将其强制转换为数据框。
我尝试了以下变化:
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts
})
但是它们不起作用,因为 ggplot 无法使用输入。我想我没有正确使用反应功能。非常感谢任何有助于减少代码冗余并确保绘图和摘要都使用相同的单个基础数据的帮助。提前谢谢你。
我当前的代码:
服务器.R
library(shiny)
library(ggplot2)
# Define server logic required to draw a histogram
function(input, output) {
output$distPlot <- renderPlot({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
# draw the density plot
ggplot(Muts, aes(Muts)) + geom_density()
})
output$distPlot2 <-renderPlot({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts <- as.data.frame(Muts)
ggplot(Muts, aes(Muts)) + geom_histogram()
})
output$summary <- renderPrint({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
summary(Muts)
})
}
【问题讨论】:
-
您需要在
ggplot调用中调用响应式表达式,因为它包含数据。ggplot(myData(), aes(...)) + ...
标签: r shiny redundancy