Dear all
After some hours trying to figure out myself what the error is, I give up.
I want to save a confidence interval, a vector, in a reactiveValue. But when I want to use this inside a renderUI inside a renderPlot i get into trouble.
I have tried to cut away as much as possible to make my example minimal, which I doubt it is but you surely get this idea quickly.
Any help is much appreciated. I would very much like to LEARN why this error is occurring, not just fix it (as I already made a work around)
best
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Screening failures"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
numericInput("screened",
"Number of Subjects finished screening",
min = 1,
max = 2000,
value = 107)
,
numericInput("failures",
"Number of screening failures",
min = 1,
max = 200,
value = 88)
,
uiOutput("freq_CI")
,
uiOutput("sf_slider")
,
numericInput("n_in_screening", label = "In Screening", min = 0, max = 200, value = 51)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("binom_plot"),
verbatimTextOutput("text3"),
uiOutput("info_text_lowerCI")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
#create reactive value for use across renderfuns
df <- reactiveValues()
#UI rate slider
output$sf_slider <- renderUI({
n_screen <- input$screened
n_fail <- input$failures
sliderInput(inputId = "sf_rate", label = "SF rate, adjustable", min = 0, max = 1, value = n_fail/n_screen)
})
output$freq_CI <- renderUI({
req(input$sf_rate)
p_hat <- as.numeric(input$sf_rate)
n <- input$screened
n_s <- input$failures
n_f <- n - n_s
z <- qnorm(p = 1-0.05/2)
Wil_CI <- (p_hat + z^2/(2*n))/(1 + z^2/n) + c(-1,1)*z/(1 + z^2/n)*sqrt(p_hat*(1-p_hat)/n + z^2/(4*n^2))
Wil_CI <- round(Wil_CI, 3)
df$CI <- Wil_CI
p("hello, lower is available -> ", df$CI[1])
})
#Test
output$text3 <- renderPrint({
#This works
df$CI
})
output$target_slider <- renderUI({
target <- input$target
sliderInput(inputId = "range", label = "Range around target. Both values included.", min = -10 + target, max = target + 20, value = c(-2 + target, 5 + target))
})
output$binom_plot <- renderPlot({
req(input$sf_rate)
n_in_screening <- input$n_in_screening
p_randomised <- 1-input$sf_rate
n_randomised <- input$screened - input$failures
n_vek <- n_randomised + (0:n_in_screening)
p_vek <- dbinom(x = 0:n_in_screening, size = n_in_screening, prob = p_randomised)
#I can access the reactive variable here just fine;
p_vek2 <- dbinom(x = 0:n_in_screening, size = n_in_screening, prob = df$CI[1])
p_vek3 <- dbinom(x = 0:n_in_screening, size = n_in_screening, prob = df$CI[2])
df <- data.frame(x = rep(n_vek,3), y = c(p_vek,p_vek2,p_vek3))
p <- ggplot(data = df, aes(x = x, y = y)) + geom_segment(aes(x = x, xend = x, y = 0, yend = y))
#Probabilities given lower CI, i.e. best case
output$info_text_lowerCI <- renderUI({
print(df$CI) #This prints NULL
print(df$CI[1])#Also NULL
tagList(
h3("I would like to display/USE Lower limit here ->>>",df$CI[1]),
)
})
#return plot
p
})
}
# Run the application
shinyApp(ui = ui, server = server)