 # Help Requested! Shiny Interface: Conditional Output/Variable Carry-Over

Hello! I am working on making a game for students to practice estimating the standard deviation of a set of data using a histogram and a given sample size.

How could I incorporate the sd(x) stored in y (line 50; two asterisks surrounding) into lines 55-58 (three asterisks surrounding) for the logical ifelse statement (since they are within different output command sequences)?

Secondly, how could I get the code to regenerate new values for lines 44-49 (one set of asterisks surrounding) each time a student selects a new sample size.

``````library(shiny)

ui <- fluidPage(

titlePanel("Standard Deviation Estimation Practice"),

# Sidebar with a slider input for sample size
sidebarLayout(
sidebarPanel(
sliderInput("samplesize", h3("Enter a positive whole number as a sample size"), min = 10, max = 500, value = 250),
submitButton("Submit"),
numericInput("guess", "What is your estimation of the standard deviation of the data in the histogram?", ""),
submitButton("Submit")
),

# Show a plot of the generated distribution and pose question
mainPanel(
plotOutput("distPlot"),
textOutput("result")
)
)
)

server <- function(input, output)({

output\$distPlot <- renderPlot({

*
seed1 <- sample.int(1000000, 1)
set.seed(seed1)
sd1 <- sample.int(25, 1)
mean1 <- sample((-300):400, 1)
x <- rnorm(n = input\$samplesize, mean = mean1, sd = sd1)
**y <- sd(x)**         *

# draw the histogram with the specified number of bins
hist(x, main = "Histogram", xlab = "Sample Values", ylab = "Frequency Count", col = 'darkgray', border = 'white')

})

output\$result <- renderText({
***ifelse(q >= (y - 0.5), ifelse(q <= (y + 0.5),
"That is a correct estimation! Good job! Enter a new sample size.",
"That estimate is too big. Try again!"),
"That estimate is too small. Try again!")***
})
})

shinyApp(ui = ui, server = server)
``````

Thank you for your help!

``````library(shiny)

ui <- fluidPage(
titlePanel("Standard Deviation Estimation Practice"),

# Sidebar with a slider input for sample size
sidebarLayout(
sidebarPanel(
sliderInput("samplesize", h3("Enter a positive whole number as a sample size"), min = 10, max = 500, value = 250),
actionButton(
inputId = "submit_hist",
label = "Press for new Histogram"
),
numericInput("guess", "What is your estimation of the standard deviation of the data in the histogram?", ""),
),
# Show a plot of the generated distribution and pose question
mainPanel(
plotOutput("distPlot"),
textOutput("result")
)
)
)

server <- function(input, output) {
x_val <- eventReactive(input\$submit_hist, {
seed1 <- sample.int(1000000, 1)
set.seed(seed1)
sd1 <- sample.int(25, 1)
mean1 <- sample((-300):400, 1)
rnorm(n = req(input\$samplesize), mean = mean1, sd = sd1)
})

y_val <- reactive({
sd(req(x_val()))
})

output\$distPlot <- renderPlot({
# draw the histogram with the specified number of bins
hist(req(x_val()),
main = "Histogram",
xlab = "Sample Values",
ylab = "Frequency Count",
col = "darkgray",
border = "white"
)
})

output\$result <- renderText({
q <- req(input\$guess)
if (!isTruthy(q)) {
return(NULL)
}
# print(y_val())
ifelse(q >= (y_val() - 0.5), ifelse(q <= (y_val() + 0.5),
"That is a correct estimation! Good job! Enter a new sample size.",
"That estimate is too big. Try again!"
),
"That estimate is too small. Try again!"
)
})
}

shinyApp(ui = ui, server = server)``````

Thank you very much for the help, Nir! There's no way I would have found that solution on my own.

One additional question, if you are able: how does the data from the "rnorm" function end up being stored in "x_val" when there are the four other lines (i.e. set.seed, sd1, and mean1)?

Thank you again. Stay safe and healthy!