Raphael_QN
Raphael_QN

Reputation: 3

UpdateSelectInput by User Input

First: Thanks for your help.

The idea is to create a shiny app to track some kind of "Sports". Maybe more like tracking the results of a drinking game... Its called Funkyball where you throw a ball at a target, run and drink. My goal is to collect and analyze data of how well people throw, drink and run.

The first tabPanel is to create some Player.

library(shiny)
ui <- fluidPage(

    mainPanel(tabsetPanel(
        tabPanel("All Player",
                 textInput("str_new_Player","New Player", ""),
                 actionButton("update", "add Player"),
                 tableOutput("tb_player")))))

    server <- function(input, output) {
        values <- reactiveValues()

        values$df <- data.frame("Player" = numeric(0))

        newEntry<- observe({
            if(input$update > 0){
                newLine <- isolate(input$str_new_Player)
                isolate(values$df[nrow(values$df) + 1,] <- c(input$str_new_Player))
            }})
        output$tb_player <- renderTable({values$df})}

    shinyApp(ui = ui, server = server)

So far so good. In the second tabPanel you would choose the first and second Player depending on your created Players. Thats where I struggle. ui:

tabPanel("New Game",
   selectInput("Player_one", "Player 1", choices = "", selected = "")

server:

updateSelectInput(session, "Player_one",
                      label = paste("Player 1"),
                      choices = values$df,
                      selected = "")

I don't know how to insert the Choices from the updated table.

Next step is adding some actionbuttons that fill a table with data if you click on them. Like hit or miss and a stopclock for measuring the time that people need per game.

Upvotes: 0

Views: 52

Answers (1)

starja
starja

Reputation: 10365

There were a few issues with the code: a missing bracket, and most importantly server also needs session as an input because updateSelectInput uses this. I've made a few changes. I use observeEvent instead of observe. I think you can also use observe, but you really want to observe an event, that the button is pushed, so I think it's a bit clearer. Also, you only need to state input$update and it is triggered when something changes there. Upon this event, you not only update the data.frame, but also update the "Player_one" Input. Here was an issue that you used the complete data.frame, but you only need the column Player:

library(shiny)
ui <- fluidPage(

  mainPanel(tabsetPanel(
    tabPanel("All Player",
             textInput("str_new_Player","New Player", ""),
             actionButton("update", "add Player"),
             tableOutput("tb_player")),
    tabPanel("New Game",
             selectInput("Player_one", "Player 1", choices = "", selected = ""))

    ))
)

  server <- function(input, output, session) {
    values <- reactiveValues()

    values$df <- data.frame("Player" = numeric(0))

    observeEvent(input$update, {
      values$df <- rbind(values$df, data.frame(Player = input$str_new_Player))
      updateSelectInput(session, "Player_one",
                        label = paste("Player 1"),
                        choices = values$df$Player,
                        selected = "")
    })

    output$tb_player <- renderTable({values$df})

    }

  shinyApp(ui = ui, server = server)

I'm not super familiar with the use of reactiveValues, maybe someone else knows if they are really needed here or if other solutions are better.

Upvotes: 1

Related Questions