闪亮的无法看到来自被动数据对象的数据帧 [英] shiny unable to see dataframe from reactive data object

查看:86
本文介绍了闪亮的无法看到来自被动数据对象的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个非常简单的闪亮应用程序,它可以为曝光和控制组的在线广告的销售额提供置信区间。 这里是我的代码: -



ui.R

  library(闪亮)

shinyUI(fluidPage(

#应用程序标题
titlePanel(简单置信区间的比例),

#边栏输入试验次数
sidebarLayout(

sidebarPanel(
numericInput(exGrpSize,Exposed Group Impressions,10000),
numericInput(exGrpConv ,暴露群组点击次数或转化次数,100),
numericInput(contGrpSize,控制组展示次数,10000),
numericInput(contGrpConv,控制组点击次数或转化次数, 100)
),

#显示生成的分布图
mainPanel(
plotOutput(intervalPlot)


))

server.R

 库(闪亮)
库(ggplot2)

#定义所需的服务器逻辑绘制一个直方图
shinyServer(函数(输入,输出){

#这是一些非常简单的代码,用于绘制比例
#的置信区间,以调用renderPlot来指示那么:
#1)它是被动的,因此当输入变化
#2时应该自动重新执行)它的输出类型是一个绘图

data < - reactive ({
resp1< - input $ exGrpConv
size1< - 输入$ exGrpSize
resp2< - 输入$ contGrpConv
size2< - 输入$ 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 =因子(c(Exposed,Control)),
se = c(se1 ,se2))
})

##定义错误条的顶部和底部
limits <-aes(ymax = respVec + se,ymin = respVec - se)
$ $ b $ output $ intervalPlot < - renderPlot({

p < - ggplot(dataSet,aes(color = group,y = respVec,x = group))
p + geom_point()+ geom_errorbar(limits,width = 0.2,lwd = 2)
})
})

但是当我运行这个时,我得到以下错误: -
$ b

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



我从剧情反应代码块获得dataSet的数据集?



<我>认识到这里的置信区间有点粗糙,这是一个初始原型] / p>

解决方案

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

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

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

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


I'm trying to make a very simple shiny app that draws confidence intervals around sales from online advertising for exposed and control groups.

Here's my code:-

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)
})
})

But when I run this I get the following error:-

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

How can I get the dataSet from the reactive code block in scope for the plot?

[I realise the confidence intervals in here are a bit crude, this is an initial prototype]

解决方案

I believe you have to return dataSet at the end of your reactive function, i.e.

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
    })

Additionally, you should be passing data() (don't forget the parentheses) as your data.frame, not dataSet. That's the primary reason R isn't finding dataSet; it never left the scope of your reactive function, so it doesn't exist in the global environment. So you should end up using

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

这篇关于闪亮的无法看到来自被动数据对象的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆