R 闪亮:基于搜索栏的选取输出选项

R 闪亮:基于搜索栏的选取输出选项,r,shiny,spotify,R,Shiny,Spotify,嗨,我正在尝试让我的pickerInput中的“选择”取决于用户在上面的搜索栏中键入的内容。我使用的是spotify r软件包,如果你搜索某个艺术家,api会返回一个类似名称的艺术家列表,你需要选择你想要的艺术家。不管怎么说,我想让那张桌子放进pickerInput,但我似乎无法让它工作 在观察事件中使用更新方法,如下所示: observeEvent(input$search, { #Rspotify possibleArtists <- searchArtist(input$se

嗨,我正在尝试让我的pickerInput中的“选择”取决于用户在上面的搜索栏中键入的内容。我使用的是spotify r软件包,如果你搜索某个艺术家,api会返回一个类似名称的艺术家列表,你需要选择你想要的艺术家。不管怎么说,我想让那张桌子放进pickerInput,但我似乎无法让它工作

在观察事件中使用更新方法,如下所示:

observeEvent(input$search, {
  #Rspotify
  possibleArtists <- searchArtist(input$search,token=my_oauth)
  possibleArtists <-  as_tibble(possibleArtists)
  myCols <- c("display_name","id")
  colNums <- match(myCols,names(possibleArtists))
  possibleArtists <- possibleArtists %>%
    select(colNums)
  updatePickerInput(
    session = session,
    inputId = "picker",
    choices = possibleArtists
  )
}, ignoreInit = TRUE)
完整示例:

library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(
  skin = "green",

  dashboardHeader(title = "Lyric Prediction"),

  dashboardSidebar(
    sidebarMenu(
      menuItem("Overview", tabName = "Overview", icon = icon("search")),
      menuItem("Analysis", tabName = "Analysis", icon = icon("bar-chart-o"))
    )
  ),

  dashboardBody(
    tags$head( 
      tags$style(HTML(".fa { font-size: 18px; }"))
    ),
    tabItems(
      # First tab content
      tabItem(
        tabName = "Overview",
        fluidRow(
          column(12,
                 searchInput(
                   inputId = "search", label = "Search Artist on Spotify",
                   placeholder = "Search",
                   btnSearch = icon("search"),
                   btnReset = icon("remove"),
                   width = "500px"
                 )
          ), align = "center"  
        ),

        pickerInput(inputId = "picker", label = "Choose an artist:", choices = NULL)
      )
    )
  )
)


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

  observeEvent(input$search, {
    updatePickerInput(
      session = session,
      inputId = "picker",
      choices = c("The Beatles", 
                  "The Beatles Recovered Band", 
                  "Yesterday - A Tribute To The Beatles",
                  "The Beatles Revival Band & Orchestra")
    )
  }, ignoreInit = TRUE)

}

shinyApp(ui, server)

太棒了,谢谢你!不知道观察功能
library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(
  skin = "green",

  dashboardHeader(title = "Lyric Prediction"),

  dashboardSidebar(
    sidebarMenu(
      menuItem("Overview", tabName = "Overview", icon = icon("search")),
      menuItem("Analysis", tabName = "Analysis", icon = icon("bar-chart-o"))
    )
  ),

  dashboardBody(
    tags$head( 
      tags$style(HTML(".fa { font-size: 18px; }"))
    ),
    tabItems(
      # First tab content
      tabItem(
        tabName = "Overview",
        fluidRow(
          column(12,
                 searchInput(
                   inputId = "search", label = "Search Artist on Spotify",
                   placeholder = "Search",
                   btnSearch = icon("search"),
                   btnReset = icon("remove"),
                   width = "500px"
                 )
          ), align = "center"  
        ),

        pickerInput(inputId = "picker", label = "Choose an artist:", choices = NULL)
      )
    )
  )
)


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

  observeEvent(input$search, {
    updatePickerInput(
      session = session,
      inputId = "picker",
      choices = c("The Beatles", 
                  "The Beatles Recovered Band", 
                  "Yesterday - A Tribute To The Beatles",
                  "The Beatles Revival Band & Orchestra")
    )
  }, ignoreInit = TRUE)

}

shinyApp(ui, server)