Create data with function inside shinyServer to feed ggplot

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")) 
+4
source share
1 answer

First of all, you have n.cases called inconsistently, I think. This is sometimes n.cases and ncases another time. This is mistake?

In any case, output$mydata() is incorrect. This is not an option. It should be simple:

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

And then, 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 is what you need to do:

 data = mydata() 

but not

 data = output$mydata 

Because output$mydata is a (reactive) function.


I would suggest reading the documentation on how the reaction responds . After that, everything should turn out much more. +1 for a very reproducible example, by the way. This is how all questions should be published.

+4
source

Source: https://habr.com/ru/post/1489059/


All Articles