光泽无法看到从反应性的数据对象数据帧(shiny unable to see dataframe f

2019-10-20 10:56发布

我试图做一个非常简单的应用程序有光泽,吸引了各地的销售网络广告的曝光和对照组的置信区间。

这里是我的代码: -

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

我怎样才能从反应代码块范围数据集的情节?

[I实现在这里的置信区间都有点粗,这是一个初始原型]

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


文章来源: shiny unable to see dataframe from reactive data object
标签: r ggplot2 shiny