闪亮的应用程序绑定数据只保存一个名字

Shiny app to bind data only saves one name

我正在尝试构建一个闪亮的应用程序以了解它的工作原理。我遇到了 运行 个问题。我正在构建一个输入表单来跟踪输入数据;在这种情况下特别是高尔夫球手。闪亮的应用程序 运行 但保存的输入不超过 1 个。例如,它演示了播放器 1 和播放器 2,因为它们是在服务器下编码的。当我动态添加 Player 3 时 - 它有效。但是当我添加玩家 4 时,它会覆盖玩家 3。

感谢您的帮助。我已经阅读了大约 100 页,但似乎找不到一个简单的答案。

library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)

############################################# GOLF SHINY APP ####################################################

#building the shiny app
ui <- fluidPage(
  theme = shinythemes::shinytheme("sandstone"),
  #user driven inputs for new golfers
  titlePanel("Stableford App"),
  navbarPage("App Options",
             tabPanel("New Golfer", 
                      br(),
                      br(),
                      textInput("name", "Initial Golfer's name", value = "Add Name Here"),
                      numericInput("hd_cap", "Whats the Golfer's Handicap?", value = 15, min = 0, max = 50),
                      sliderInput("goal_score", "Initial Stableford Goal?", value = 10, min = 0, max = 60),
                      actionButton("add_golfer", ("Add Golfer!"), icon = icon("cocktail"), class="btn btn-danger btn-block")),
             
             tabPanel("View Golfers",
                      actionButton("gen_golfers", "Update Golfer List"),
                      dataTableOutput("initial_golfer_list"),
                      dataTableOutput("golfer_list")
  )

)
)
server <- function(input, output, session){
  
  #building the rbind information for players
  name <- c('Player 1', 'Player 2')
  hd_cap <- c(2,10)
  sgoal <- c(40,22)
  big_golfer_list <- data.frame(name, hd_cap, sgoal) 
  big_golfer_list <- setNames(big_golfer_list, c("Golfer", "Handicap", "Target"))
  
  PlayerData <-  eventReactive(input$gen_golfers,{
    big_golfer_list <- rbind(big_golfer_list,
                              data.frame("Golfer" = input$name, "Handicap" = input$hd_cap, "Target" = input$goal_score))
  })

  #table with all names
  output$golfer_list = renderDataTable(
    FinalData <- PlayerData() %>% filter(Golfer != "Add Name Here"))

}

# Run the application 
shinyApp(ui = ui, server = server)

要保留以前的值,您需要使用 <<-。否则,它只是将一个高尔夫球手的名字添加到列表中现有的两个名字中。请参阅下面的更改以获得您想要的行为。

更新: 我已经稍微修改了您的代码以仅使用第一个操作按钮。此外,选择现在位于侧边栏中。重置按钮允许您重新开始,并且您可以修改显示中的任何条目 table。

############# GOLF SHINY APP ##############

#building the shiny app
ui <- fluidPage(
  theme = shinythemes::shinytheme("sandstone"),
  #user driven inputs for new golfers
  titlePanel("Stableford App"),
  navbarPage("App Options",
              sidebarLayout(
                sidebarPanel(
                  br(),
                  textInput("name", "Initial Golfer's name", value = "Add Name Here"),
                  numericInput("hd_cap", "Whats the Golfer's Handicap?", value = 15, min = 0, max = 50),
                  sliderInput("goal_score", "Initial Stableford Goal?", value = 10, min = 0, max = 60),
                  actionButton("add_golfer", ("Add Golfer!"), icon = icon("cocktail"), class="btn btn-danger btn-block"),
                  actionButton(inputId = "reset", label = "Reset", class="btn btn-primary")

                ),
                mainPanel(
                  tabsetPanel(
                    tabPanel("New Golfer",
                             br(),
                             br(),
                             # textInput("name", "Initial Golfer's name", value = "Add Name Here"),
                             # numericInput("hd_cap", "Whats the Golfer's Handicap?", value = 15, min = 0, max = 50),
                             # sliderInput("goal_score", "Initial Stableford Goal?", value = 10, min = 0, max = 60),
                             # actionButton("add_golfer", ("Add Golfer!"), icon = icon("cocktail"), class="btn btn-danger btn-block"),
                             DTOutput("golfersnames")
                    ),

                    tabPanel("View Golfers",
                             # actionButton("gen_golfers", "Update Golfer List"),
                             # dataTableOutput("initial_golfer_list"),
                             dataTableOutput("golfer_list")
                    )
                  )

                )

             )
  )
)

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

  #building the rbind information for players
  name <- c('Player 1', 'Player 2')
  hd_cap <- c(2,10)
  sgoal <- c(40,22)
  big_golfer_list <- data.frame(name, hd_cap, sgoal)
  big_golfer_list <- setNames(big_golfer_list, c("Golfer", "Handicap", "Target"))

  DF1 <- reactiveValues(data=big_golfer_list)
  orig_d1 <- big_golfer_list

  PlayerData <-  eventReactive(input$add_golfer,{
    big_golfer_list <<- rbind(big_golfer_list,
                             data.frame("Golfer" = input$name, "Handicap" = input$hd_cap, "Target" = input$goal_score))
  })

  observeEvent(input$add_golfer, {
    DF1$data <- PlayerData()
    output$golfersnames <- renderDT(DF1$data, editable = TRUE)
  })

  #table with all names
  output$golfer_list = renderDataTable(
    FinalData <- PlayerData() %>% filter(Golfer != "Add Name Here"))

  observeEvent(input$reset, {
    DF1$data <- orig_d1
  })

}

# Run the application
shinyApp(ui = ui, server = server)