Reputation: 425
I am creating a survey using R shiny and have the following function at the beginning of my Shiny App:
install.packages("devtools")
spotifydata<-spotifycharts::chart_top200_weekly()
s<-spotifydata$artist
h<-head(s,20)
I want to know if there is anywhere to display the output of variable "h"??
I had the idea of using "selectInput" in the following manner to display each result in a drop down menu fashion.
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)
I know this produces an error But I want to know if there is a way to emulate this
Upvotes: 1
Views: 58
Reputation: 7694
In the selectInput
the variables should be written without quotes like this:
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)
Following is an app showing the working of the same:
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)
The output is as follows:
Please note that with this approach the variable h
is shared between different user sessions.
If you don't want the variable h
to be shared between different user sessions you can use the following approach, where we get h value within the server function and update the choices of select input using the function 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)
Upvotes: 1