在selectInput 中,变量应该不带引号,如下所示:
selectInput("artists","pick 3 artists out of the top 10",
c(h[1],h[2],h[3],h[4],h[5],h[6],
h[7],h[8],h[9],h[10]),multiple = TRUE)
以下是一个展示其工作原理的应用:
library(shiny)
spotifydata<-spotifycharts::chart_top200_weekly()
s<-spotifydata$artist
h<-head(s,20)
ui <- fluidPage(
selectInput("artists","pick 3 artists out of the top 10",
c(h[1],h[2],h[3],h[4],h[5],h[6],
h[7],h[8],h[9],h[10]),multiple = TRUE)
)
server <- function(input, output)
{}
shinyApp(ui, server)
输出如下:
请注意,通过这种方法,变量h在不同的用户会话之间共享。
如果您不希望变量 h 在不同的用户会话之间共享,您可以使用以下方法,我们在服务器函数中获取 h 值并使用函数 updateSelectInput 更新选择输入的选择
ui <- fluidPage(
selectInput("artists","pick 3 artists out of the top 10",
choices = c(), multiple = TRUE)
)
server <- function(input, output, session)
{
observe({
spotifydata<-spotifycharts::chart_top200_weekly()
s<-spotifydata$artist
h<-head(s,20)
updateSelectInput(session, inputId = "artists", choices = c(h[1],h[2],h[3],h[4],h[5],h[6],
h[7],h[8],h[9],h[10]))
})
}
shinyApp(ui, server)