在 Shiny 应用中构建 URL 搜索参数?
Constructing URL search parameters in Shiny app?
我有一个处理大型 csv 文件的 Shiny 应用程序。目前,用户可以从下拉菜单中 select facility_id
数字来获取特定的绘图,请参阅 https://r.timetochange.today/shiny/Annual_Emissions2/。我想用 URL 参数传递这个 id,比如 /?selected_facilities=1010040
这样我就可以将这些图嵌入另一个网站。
我已经从 中获取代码并尝试使用它来更新我在 Shiny 应用程序的服务器部分中的 selectInput()
值,但我真的不明白 UI 部分是构建的,所以我没有做对。任何帮助将不胜感激!这是相关代码:
#### shiny UI ####
facilities <- unique(ghg_emissions$facility_id)
ui <- fluidPage(
titlePanel("Annual Greenhouse Gas Emissions"),
sidebarLayout(
sidebarPanel(
selectInput("selected_facility",
"Select facility",
choices = facilities) # select input gives the drop down menu to select facilities
),
mainPanel(
plotlyOutput("facility_plot")
)
)
)
#### shiny server ####
server <- function(input, output, session) {
# Here you read the URL parameter from session$clientData$url_search
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['selected_facility']])) {
updateSelectInput(session, "selected_facility", value = query[['selected_facility']])
}
})
你的 UI 很好,updateSelectInput
的问题,使用 selected
而不是 value
并包括 choices
.
最小工作示例:
library(shiny)
facilities <- seq(1:5)
ui <- fluidPage(
selectInput("selected_facility", "Select facility", choices = facilities)
)
server <- function(input, output, session) {
observe({
#Get URL query
query <- parseQueryString(session$clientData$url_search)
#Ignore if the URL query is null
if (!is.null(query[['selected_facility']])) {
#Update the select input
updateSelectInput(session, "selected_facility", selected = query[['selected_facility']], choices = facilities)
}
})
}
shinyApp(ui, server)
要测试 运行 您闪亮的应用,请单击 'Open in Browser' 并将您的查询附加到 URL,例如
127.0.0.1:6054/?selected_facility=4
我有一个处理大型 csv 文件的 Shiny 应用程序。目前,用户可以从下拉菜单中 select facility_id
数字来获取特定的绘图,请参阅 https://r.timetochange.today/shiny/Annual_Emissions2/。我想用 URL 参数传递这个 id,比如 /?selected_facilities=1010040
这样我就可以将这些图嵌入另一个网站。
我已经从 selectInput()
值,但我真的不明白 UI 部分是构建的,所以我没有做对。任何帮助将不胜感激!这是相关代码:
#### shiny UI ####
facilities <- unique(ghg_emissions$facility_id)
ui <- fluidPage(
titlePanel("Annual Greenhouse Gas Emissions"),
sidebarLayout(
sidebarPanel(
selectInput("selected_facility",
"Select facility",
choices = facilities) # select input gives the drop down menu to select facilities
),
mainPanel(
plotlyOutput("facility_plot")
)
)
)
#### shiny server ####
server <- function(input, output, session) {
# Here you read the URL parameter from session$clientData$url_search
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['selected_facility']])) {
updateSelectInput(session, "selected_facility", value = query[['selected_facility']])
}
})
你的 UI 很好,updateSelectInput
的问题,使用 selected
而不是 value
并包括 choices
.
最小工作示例:
library(shiny)
facilities <- seq(1:5)
ui <- fluidPage(
selectInput("selected_facility", "Select facility", choices = facilities)
)
server <- function(input, output, session) {
observe({
#Get URL query
query <- parseQueryString(session$clientData$url_search)
#Ignore if the URL query is null
if (!is.null(query[['selected_facility']])) {
#Update the select input
updateSelectInput(session, "selected_facility", selected = query[['selected_facility']], choices = facilities)
}
})
}
shinyApp(ui, server)
要测试 运行 您闪亮的应用,请单击 'Open in Browser' 并将您的查询附加到 URL,例如
127.0.0.1:6054/?selected_facility=4