我有一个想要在闪亮selectInput
下拉列表中使用的键/值对数据。
我希望用户从玩家名称列表中进行选择,但是随后我想使用相应的playerID
来过滤其他数据集。
有没有办法做到这一点?我当前的代码如下:
selectInput("player","Select a player", Players$fullName),
我希望玩家根据“团队”下拉列表做出反应。我的完整代码是:
library(shiny)
library(dplyr)
setwd("C:/Users/Michael/Documents/Baseball/Retrosheet/Shiny")
Teams <- read.csv("TeamID.csv") # List of team names
Players <- read.csv("TeamBatter.csv") # TeamName, fullName, retroID
PlayerData <- read.csv("playerExpectedRBI.csv") # Data keyed on PlayerID
todrop <- Players$retroID
names(todrop) <- Players$fullName
ui <- fluidPage(
selectInput("team","Select a team:",Teams$teamID),
selectInput("player", "Select a player", todrop),
tableOutput("playerSummary")
)
server <- function(input,output,session){
observe({
PlayerList <- reactive({Players %>% filter(teamID == input$team) %>% select(fullName,retroID)})
updateSelectInput(session,"player", choices=PlayerList()$fullName)
SelectedPlayerSum <- reactive({PlayerData %>% filter(resbatter == input$player)})
output$playerSummary <- renderTable({SelectedPlayerSum()})
})
}
shinyApp(ui = ui, server = server)
我遇到了错误: 错误(功能(选择,名称): 必须选择“选择”中的所有子列表。
答案 0 :(得分:0)
基本上,您需要创建命名列表并将其传递给下拉列表。它将返回playerID
而不是玩家fullname
。
library(shiny)
Players <- data.frame(fullname = c('John Doe', 'James Smith', 'John Smith'), playerID = c(1:3))
# create named list and pass it to dropdown
todrop <- Players$playerID
names(todrop) <- Players$fullname
ui <- fluidPage(
selectInput("player","Select a player", todrop),
uiOutput("out")
)
server <- function(input, output) {
output$out <- renderUI({
print(paste0("Players ID is: ", input$player)) # here you select player ID from dropdown (not fullnames) and can use input$player for your queries
})
}
shinyApp(ui = ui, server = server)