是否可以通过读取data.frame来为selectInput列出列表?
据我了解,一个使用selectInput的选项在server.R中子集化一个data.frame,而不是相反。
我无法列出预制清单,因为我不知道会有多少选择。所以我的应用应该:
读取data.frame,获取对广告系列列的独特观察结果,并将其放入列表中,并在selectInput
中使用该列表
代码:
library(shiny)
library(tidyverse)
shinyApp(
ui = fluidPage(
selectInput("state", "Choose a state:",
list(`East Coast` = c("NY", "NJ", "CT"),
`West Coast` = c("WA", "OR", "CA"),
`Midwest` = c("MN", "WI", "IA"))
),
textOutput("result")
),
server = function(input, output) {
fb <- reactive({
fb_data <- read_csv("https://www.dropbox.com/s/ry6gfksljbpg1d6/curacao-fb-julago.csv?dl=1", skip = 0)
})
lst_campaigns <- unique(fb()$campaign)
lst_campaigns
}
)
selectInput
信息的链接:
https://shiny.rstudio.com/reference/shiny/latest/selectInput.html
答案 0 :(得分:1)
是的,[0, 4]
可以完成这项工作。
在下面的示例中,单击“更新”按钮将调用读取您的保管箱文件并更新广告系列列表。 请注意,我只将前十个广告系列传递给选项,因为第11个条目会导致错误(也许有一个闪亮字符无法处理的非法字符)。
updateSelectInput
library(shiny)
library(tidyverse)
shinyApp(
ui = fluidPage(
selectInput("state", "Choose a state:",
list(`East Coast` = c("NY", "NJ", "CT"),
`West Coast` = c("WA", "OR", "CA"),
`Midwest` = c("MN", "WI", "IA"))),
actionButton("update", "Update"),
textOutput("result")
),
server = function(input, output, session) {
fb <- reactive({
fb_data <- read_csv("https://www.dropbox.com/s/ry6gfksljbpg1d6/curacao-fb-julago.csv?dl=1", skip = 0)
fb_data
})
observeEvent(input$update, {
dat = fb()
lst_campaigns <- unique(dat$campaign)
updateSelectInput(session, "state",
choices=head(lst_campaigns, 10),
selected=lst_campaigns[1])
})
}
)
用于自动启动。
server
答案 1 :(得分:0)
您可以通过同时使用updateSelectInput
和observe
函数来完成此操作,
library(shiny)
ui <- fluidPage(
selectInput("species", "Select a specie:", choices = NULL)
)
server <- function(input, output, session) {
data <- reactive({
iris
})
observe({
# Dont run unless a data have been imported
req(data())
unique_obs <- levels(data()[, 'Species'])
updateSelectInput(session = session, "species", choices = unique_obs)
})
}
shinyApp(ui, server)