如何在R Shiny应用程序中从谷歌表单中随机生成输出



我正在尝试构建一个应用程序,从中提取谷歌表单上的歌曲数据库及其艺术家和流派,以在Shiny上创建输出。我想使用某种过滤来从他们流派中的歌曲列表和其他选择的输入中生成随机选择。我是Shiny的新手,在这方面遇到了困难。任何提示都将不胜感激!这是我到目前为止的代码以及Google Sheets链接。

https://docs.google.com/spreadsheets/d/1Wzt5w-yyHYvkYuZIj4gHPcZS6deFNBc-H_hM_vJKPDk/edit?usp=sharing

read_rds("data-processed/01-cleaned_data.rds")
ui <- fluidPage(
theme = shinytheme("superhero"),
headerPanel("KVRX Hot 100: The Mobile Application"),
sidebarPanel(
selectInput("no1", "Genre:",
c("Blues" = "blues",
"Rock" = "rock",
"Pop" = "pop",
"Disco" = "disco",
"Folk" = "folk",
"Country" = "country",
"Indie" = "indie",
"House Music/EDM" = "edm",
"Reggae" = "reggae",
"Classical" = "classical",
"Korean" = "korean",
"Punk" = "punk",
"Afro-pop" = "afro-pop",
"Metal" = "metal",
"Funk/Soul" = "soul",
"Brazillian" = "brazil",
"Korean" = "korean",
"Indie rock" = "indie rock",
"Electronic" = "electronic",
"Metal" = "metal",
"Alternative" = "alt",
"Hip-hop/Rap" = "hh/rap",
"Jazz" = "jazz",
"Worldwide" = "ww",
"Ambient" = "ambient",
"Indian" = "indian",
"Alt folk" = "alt folk",
"Electronic" = "electronic",
"Indie rock" = "indie rock",
"Europe" = "europe",
"Psychedelic" = "psych",
"Hard bop" = "hard bop",
"Cajun" = "cajun",
"Christian" = "christian")),
# checkboxInput("outliers", "Show outliers", TRUE),
selectInput("no2", "Explicit:",
c("Explicit" = "explicit",
"Clean" = "clean")),
selectInput("no3", "Texan Artist",
c("Texan" = "texan",
"Not Texan" = "not-texan")),
actionButton("btn", "Generate"),
),
mainPanel(
textOutput("Song")
),
mainPanel(
textOutput("Song")
)
)
server <- function(input, output) {
calculate <- eventReactive(input$btn, {
input$no1 + input$no2
})
output$total <- renderText({
calculate()
})
}

shinyApp(ui, server)

这里有一种可能的方法(我无法访问您的.rds对象,但这个闪亮的应用程序(下面(,直接从谷歌表单链接中提取。请注意,我不使用no3,它是选择是Texan还是not Texan的输入,因为我无法访问它。此外,我的get_songs()功能目前随机抽取5首歌曲。您可能想要提取所有歌曲,或者您可能想要添加一个UI元素,允许用户设置所需的歌曲数量。

library(googlesheets4)
library(shiny)
library(shinythemes)
library(tidyverse)
# set to read only
gs4_deauth()
# get data
data = read_sheet(
"https://docs.google.com/spreadsheets/d/1Wzt5w-yyHYvkYuZIj4gHPcZS6deFNBc-H_hM_vJKPDk/edit?usp=sharing",
col_types="c"
)
# get genre choices
genres = unique(data %>% filter(!is.na(`Genres:`)) %>% pull(`Genres:`))
# drop extra column ("Genres:") and rename
data <- data %>% 
select(-`Genres:`) %>% 
rename_all(~c("Explicit", "Genre_code", "TRT", "Song", "Artist", "Genre_str", "song_id"))
get_songs <- function(g,e,n=5) {
result = data %>% 
filter(Genre_str %in% g, Explicit == e) %>% 
select(Song, Artist, Genre = Genre_str)

# could be less than n songs returned
if(nrow(result)==0) return(NULL)

if(nrow(result)<n) return(result) 
else return(sample_n(result,n))
}
ui <- fluidPage(
theme = shinytheme("superhero"),
headerPanel("KVRX Hot 100: The Mobile Application"),
sidebarLayout(
sidebarPanel(
selectInput("no1", "Genre:",choices=genres),
selectInput("no2", "Explicit:",choices=c("Clean", "Explicit")),
selectInput("no3", "Texan Artist",choices= c("Texan" = "texan","Not Texan" = "not-texan")),
actionButton("btn", "Generate")
),
mainPanel(
tableOutput("Songs")
)
)
)

server <- function(input, output, session) {
songs = eventReactive(input$btn, {
get_songs(input$no1, input$no2)
})

output$Songs <- renderTable(
songs()
)
}
shinyApp(ui, server)

最新更新