pat28dh
pat28dh

Reputation: 87

Creating a data frame from inputs in shiny

I'm trying to create a shiny app where I can enter input from a sidebar panel to create a data frame on my second tab panel called table. I am able to have one row of data, but when I press my submit button it refreshes these instead of adding another row. Thank you very much for your help.

Here is my code:

library(shiny)
library(ggplot2)


ui <- fluidPage(
  titlePanel("Team"),
  tags$img(height = 100, width = 100,
           src = "Logo"),
    sidebarPanel(
    textInput(inputId = "date",
            label = "Date", 
            value = "yyyy/mm/dd"),
  textInput(inputId = "team",
            label = "Team Name", 
            value = "Team Name"),
  textInput(inputId = "pnumber",
            label = "Player Number", 
            value = "#"),
  selectInput("shot", "shot type:",
              list(`Shot Type` = list("wrist shot", "slap shot", "snap shot"))),
  selectInput("situation", "scoring opportunity:",
              list(`Green` = list("Double cross", "dot line pass"),
                   `Yellow` = list("2vs1", "same side quick"),
                   `Red` = list("clear", "wrap"))),
  submitButton("Add")),
  
  mainPanel(tabsetPanel(
            tabPanel("Track", plotOutput(outputId = "hockeyplot")),
            tabPanel("Data", tableOutput(outputId = "table")),
            tabPanel("Chart", plotOutput(outputId = "chart")))),)
  

server <- function(input, output){
  Reactive_Var<-reactive({
    results=data.frame(Date = input$date, 
                       Team = input$team,
                       Player = input$pnumber,
                       ShotType = input$shot,
                       Situation = input$situation)})
  observeEvent(input$Add, {df<- data.frame(Date = input$date,
                                           Team = input$team,
                                           Player = input$pnumber,
                                           ShotType = input$shot,
                                           Situation = input$situation)})
  output$table<-renderTable({
    Reactive_Var()
  })
  
}

shinyApp(ui = ui, server = server)

Upvotes: 3

Views: 1435

Answers (1)

Ben
Ben

Reputation: 30474

Here is one approach:

First, would use an actionButton instead of a submitButton (this is generally preferable). See here for more information using action buttons.

Second, you can create an empty dataframe (as default) stored in reactiveValues. When the "Add" button is selected, a new row of data is added to this data frame with rbind.

Then, renderTable will show the updated reactiveValues data frame.

library(shiny)
library(ggplot2)

ui <- fluidPage(
  titlePanel("Team"),
  tags$img(height = 100, width = 100,
           src = "Logo"),
  sidebarPanel(
    textInput(inputId = "date",
              label = "Date", 
              value = "yyyy/mm/dd"),
    textInput(inputId = "team",
              label = "Team Name", 
              value = "Team Name"),
    textInput(inputId = "pnumber",
              label = "Player Number", 
              value = "#"),
    selectInput("shot", "shot type:",
                list(`Shot Type` = list("wrist shot", "slap shot", "snap shot"))),
    selectInput("situation", "scoring opportunity:",
                list(`Green` = list("Double cross", "dot line pass"),
                     `Yellow` = list("2vs1", "same side quick"),
                     `Red` = list("clear", "wrap"))),
    actionButton("Add", "Add")),
  
  mainPanel(tabsetPanel(
    tabPanel("Track", plotOutput(outputId = "hockeyplot")),
    tabPanel("Data", tableOutput(outputId = "table")),
    tabPanel("Chart", plotOutput(outputId = "chart")))))


server <- function(input, output){
  
  rv <- reactiveValues(
    df = data.frame(
      Date = as.Date(character()),
      Team = character(),
      Player = character(),
      ShotType = character(),
      Situation = character()
    )
  )
  
  observeEvent(input$Add, {
    rv$df <- rbind(rv$df, data.frame(Date = input$date, 
                                     Team = input$team, 
                                     Player = input$pnumber, 
                                     ShotType = input$shot, 
                                     Situation = input$situation))
  })
  
  output$table<-renderTable({
    rv$df
  })
  
}

shinyApp(ui = ui, server = server)

Upvotes: 2

Related Questions