【问题标题】:shiny unable to see dataframe from reactive data object闪亮的无法从反应数据对象中看到数据帧
【发布时间】:2014-09-04 22:03:22
【问题描述】:

我正在尝试制作一个非常简单的闪亮应用,它可以从在线广告中为暴露组和对照组绘制销售置信区间。

这是我的代码:-

ui.R

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Easy Confidence Intervals for Proportions"),

  # Sidebar with input for the number of trials
   sidebarLayout(

 sidebarPanel(
 numericInput("exGrpSize", "Exposed Group Impressions", 10000),
 numericInput("exGrpConv", "Exposed Group Clicks or Conversions", 100),
 numericInput("contGrpSize", "Control Group Impressions", 10000),
 numericInput("contGrpConv", "Control Group Clicks or Conversions", 100)
 ),

# Show a plot of the generated distribution
mainPanel(
  plotOutput("intervalPlot")
    )
   )
))

server.R

library(shiny)
library(ggplot2)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  # This is some very simple code to draw confidence intervals for proportions
  # wrapped in a call to renderPlot to indicate that:
  #  1) It is "reactive" and therefore should re-execute automatically when inputs change
  #  2) Its output type is a plot

  data <- reactive({
    resp1 <- input$exGrpConv
    size1 <- input$exGrpSize
    resp2 <- input$contGrpConv
    size2 <- input$contGrpSize
    rate1 <- resp1/size1
    rate2 <- resp2/size2
    se1 <- 1.96*sqrt((1/size1)*rate1*(1-rate1))
    se2 <- 1.96*sqrt((1/size2)*rate2*(1-rate2))
    dataSet <- data.frame(
                  respVec = c(rate1, rate2),
                  group = factor(c("Exposed", "Control")),
                  se = c(se1, se2))
    })

#   # Define the top and bottom of the errorbars
   limits <- aes(ymax = respVec + se, ymin = respVec - se)
#   
  output$intervalPlot <- renderPlot({  
# 
   p <- ggplot(dataSet, aes(colour=group, y = respVec, x = group))
   p + geom_point() + geom_errorbar(limits, width=0.2, lwd=2)
})
})

但是当我运行它时,我收到以下错误:-

Error in ggplot(dataSet, aes(colour = group, y = respVec, x = group)) : object 'dataSet' not found

如何从绘图范围内的反应代码块中获取数据集?

[我意识到这里的置信区间有点粗略,这是一个初始原型]

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    我相信你必须在你的反应函数结束时返回dataSet,即

    data <- reactive({
        resp1 <- input$exGrpConv
        size1 <- input$exGrpSize
        resp2 <- input$contGrpConv
        size2 <- input$contGrpSize
        rate1 <- resp1/size1
        rate2 <- resp2/size2
        se1 <- 1.96*sqrt((1/size1)*rate1*(1-rate1))
        se2 <- 1.96*sqrt((1/size2)*rate2*(1-rate2))
        dataSet <- data.frame(
                      respVec = c(rate1, rate2),
                      group = factor(c("Exposed", "Control")),
                      se = c(se1, se2))
        dataSet
        })
    

    此外,您应该将data()(不要忘记括号)作为您的data.frame,而不是dataSet。这是 R 没有找到 dataSet 的主要原因;它从未离开您的反应函数的范围,因此它不存在于全局环境中。所以你最终应该使用

    p <- ggplot(data=data(), aes(colour=group, y = respVec, x = group))
    

    【讨论】:

    • 我很难理解是什么类型的对象 - data() 对象 - 以及如何访问它的列...例如,如果我想要 se2,我可以使用 data()$ 访问SE2?在 ggplot 中这不是什么大问题,因为 aes 让您可以自己引用列,但例如 dygraphs 可能也需要列引用。
    猜你喜欢
    • 2017-06-28
    • 1970-01-01
    • 2021-05-05
    • 2016-02-10
    • 2022-01-12
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2017-09-19
    相关资源
    最近更新 更多