I have a Shiny app that calculates some power ratings to study a type of genetic association. Ui.R is pretty simple, and 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).
Link to Gist here . For start:
library(shiny) shiny:: runGist('5895082')
The app computes grades correctly, but I have two questions:
Is it possible that output$powTable actually represents all the values ββcontained within the range in the first sliderInput(n.cases) ? These seem to be just the two extremes of the range ... what am I doing wrong?
When starting the application, an error appears:
Error: Reading objects from shinyoutput object not allowed.
How to transfer data (reactivity?) From f() function to supply ggplot? After much trial and error, I was very lost. Where could there be an error in my code? A lot in advance!
The function source code 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"))