Hi there,
As the title states, I'm having trouble storing an input in a dynamic vector. It's an almost identical problem to
this question, but I could not make the workaround work for me. I would like to do the following: Input the result of a dice roll, add the result to a vector of previous results and display a histogram of previous results. The full app has slightly more to it, but if I could achieve this, I could get the rest done myself. It is necessary to hide the histogram until the new input has been made -- the app is for a science festival, and we want children to guess the roll of a die, then we record both their guess and the actual result, showing them both histograms.
I have looked all over the forum and tried a lot of different things, but I couldn't get any of them to do what I wanted. I would really appreciate any advice that would get me over this final hurdle.
Below is my code so far. I have changed the code a little to show that I can get the histogram no problem for a single roll, but not for a dynamic vector.
server.R:
library(shiny)
allguesses <- c()
allrolls <- c()
shinyServer(function(input, output) {
formulaText <- reactive({
paste("You guessed ", input$myguess, ", and you rolled a ", input$roll, sep="")
})
formulaText2 <- reactive({
paste("You are guesser number ", input$goButton, sep="")
})
guessInput <- reactive(function(){
allguesses <<- c(allguesses, as.numeric(input$myguess))
return(allguesses)
})
output$caption <- renderText({
formulaText()
})
output$outk <- renderText({
formulaText2()
})
output$guesshist <- renderPlot({
hist(as.numeric(allguesses), main="Everyone's guesses so far", xlab="", cex.main=2,col=4,
breaks=c(seq(0.5, 6.5, by=1)))
})
output$rollhist <- renderPlot({
hist(as.numeric(input$roll), main="Everyone's rolls so far (just last roll at the moment)", xlab="", cex.main=2,col=3,
breaks=c(seq(0.5, 6.5, by=1)))
})
})
ui.R:
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Dice game"),
sidebarPanel(
selectInput("myguess", "My guess:",
list("1" = 1,
"2" = 2,
"3" = 3,
"4" = 4,
"5" = 5,
"6" = 6),
"1"),
selectInput("roll", "Actual roll:",
list("1" = 1,
"2" = 2,
"3" = 3,
"4" = 4,
"5" = 5,
"6" = 6),
"1"),
actionButton("goButton", "Click here if you're sure"),
# submitButton("Let's go!"),
wellPanel(
p(strong("Show me the money!")),
checkboxInput(inputId = "plots_tick", label = "aka click here", value = F)
)
),
mainPanel(
conditionalPanel(condition = "input.plots_tick",
br(),
h3(textOutput(outputId = "caption"))),
conditionalPanel(condition = "input.plots_tick",
br(),
h3(textOutput(outputId = "outk"))),
conditionalPanel(condition = "input.plots_tick",
br(),
div(plotOutput(outputId = "guesshist"))),
conditionalPanel(condition = "input.plots_tick",
br(),
div(plotOutput(outputId = "rollhist")))
)
))
Regards,
Martin Law