Create data with function inside shinyServer to feed ggplot

function, ggplot2, r, shiny

Solution

First of all, you have `n.cases` named inconsistently I think. It's `n.cases` sometimes, and `ncases` other times. Is that a mistake?

Anyway, `output$mydata()` is incorrect. It isn't an output. It should be just:

mydata <- reactive(f(input$n.cases,
  input$p0,
  input$OR.cas.ctrl,
  input$Nh,
  input$sig.level))

And then when executing it in `output$powHap()` it should be:

output$powHap <- renderPlot(
{
   print(ggplot(data = mydata(), aes(ncases, power)) + 
     theme_bw() + 
     theme(text=element_text(family="Helvetica", size=12)) + 
     labs(title = "Ad-hoc power for haplogroup") +
     scale_color_brewer(palette = "Dark2", guide = guide_legend(reverse=TRUE)) +
     xlab("number of cases/controls") +
     ylab("power") +
     scale_x_log10() +
     geom_line(alpha=0.8, size=0.2) +
     geom_point(aes(shape = factor(OR)), colour="black"))
})

The important part there is that you need to do:

data = mydata()

rather than

data = output$mydata

Because `output$mydata` is a (reactive) function.

I would suggest reading the documentation on how reactives work. The whole thing should make a lot more sense afterwards. +1 for a very reproducible example by the way. This is how all questions should be posted.

Problem

I have a Shiny app what calculates some power estimates for a type of genetic association study. The ui.R is pretty simple, and the server.R has a function that gives a data frame (I think I can't have this function as `reactive` because it has some parameters). The link to the Gist is here. To run it: ``` library(shiny) shiny:: runGist('5895082') ``` The app calculates correctly the estimates, but I have two questions regarding it: Is it possible to have the `output$powTable` actually represent all the values contained within the range, in the first `sliderInput(n.cases)`?. It only seems to represent the two extreme values of the range... what I'm doing wrong? There's an error when running the app: `Error: Reading objects from shinyoutput object not allowed.` How can I pass the data (reactivity?) from the function `f()` to feed the ggplot? After much trial and error, I am very lost. Where can be the error in my code? Many thaks in advance! The original code of the function works well: (EDITED) ``` f <- function(ncases, p0, OR.cas.ctrl, Nh, sig.level) { num.cases <- ncases p0 <- p0 Nh <- Nh OR.cas.ctrl <- OR.cas.ctrl sig.level <- sig.level # Parameters related to sig.level, from [Table 2] of Samuels et al. # For 90% power and alpha = .05, Nscaled = 8.5 if (sig.level == 0.05){ A <- -28 # Parameter A for alpha=.05 x0 <- 2.6 # Parameter x0 for alpha=.05 d <- 2.4 # Parameter d for alpha=.05 } if (sig.level == 0.01){ A <- -13 # Parameter A for alpha=.01 x0 <- 5 # Parameter x0 for alpha=.01 d <- 2.5 # Parameter d for alpha=.01 } if (sig.level == 0.001){ A <- -7 # Parameter A for alpha=.001 x0 <- 7.4 # Parameter x0 for alpha=.001 d <- 2.8 # Parameter d for alpha=.001 } out.pow <- NULL # initialize vector for(ncases in ncases){ OR.ctrl.cas <- 1 / OR.cas.ctrl # 1. CALCULATE P1 FROM A PREDEFINED P0, AND A DESIRED OR OR <- OR.ctrl.cas bracket.pw <- p0 / (OR - OR*p0) # obtained after isolating p1 in OR equation [3]. p1 <- bracket.pw / (1 + bracket.pw) Nh037 <- Nh^0.37 # 2. CALCULATE NSCALED num.n <- num.cases*((p1-p0)^2) den.n <- (p1*(1-p1) + p0*(1-p0))*Nh037 Nscaled <- num.n/den.n num.power <- A - 100 # 3. CALCULATE POWER den.power <- 1 + exp((Nscaled - x0)/d) power <- 100 + (num.power/den.power) # The power I have to detect a given OR with my data, at a given alpha } OR <- OR.cas.ctrl out.pow <- data.frame(num.cases, Nh, Nscaled, p0, OR, sig.level, power) out.pow } mydata <- f(ncases=seq(50,1000, by=50), 0.4, 2.25, 11, 0.05) mydata library(ggplot2) print(ggplot(data = mydata, aes(num.cases, power)) + theme_bw() + theme(text=element_text(family="Helvetica", size=12)) + labs(title = "Ad-hoc power for haplogroup") + scale_color_brewer(palette = "Dark2", guide = guide_legend(reverse=TRUE)) + xlab("number of cases/controls") + ylab("power") + scale_x_log10() + geom_line(alpha=0.8, size=0.2) + geom_point(aes(shape = factor(OR)), colour="black")) ```

Original source