【问题标题】:How do you pass parameters to a shiny app via URL如何通过 URL 将参数传递给闪亮的应用程序
【发布时间】:2015-12-28 14:25:40
【问题描述】:

在网络浏览器中,您将参数传递给类似的网站

www.mysite.com/?parameter=1

我有一个闪亮的应用程序,我想在计算中使用传递给站点的参数作为输入。那么是否可以执行类似 www.mysite.com/?parameter=1 的操作,然后使用 input!parameter?

你能提供任何示例代码或链接吗?

谢谢

【问题讨论】:

标签: r shiny shiny-server shinydashboard


【解决方案1】:

当应用基于 URL 初始化时,您必须自己更新输入。您将使用session$clientData$url_search 变量来获取查询参数。这是一个示例,您可以轻松地将其扩展为您的需求

library(shiny)

shinyApp(
  ui = fluidPage(
    textInput("text", "Text", "")
  ),
  server = function(input, output, session) {
    observe({
      query <- parseQueryString(session$clientData$url_search)
      if (!is.null(query[['text']])) {
        updateTextInput(session, "text", value = query[['text']])
      }
    })
  }
)

【讨论】:

  • 是否可以直接在应用程序上更新输入?我正在尝试,但它会自动更改为 url 值。例如在 url 我选择 text='abc' 然后在我想定义 text='def' 的应用程序上。
  • 你将如何做到这一点而不必在 UI 中输入?人们可能不希望用户弄乱传递的值。
  • 输入只是演示您可以使用该参数做什么。绝对不需要
【解决方案2】:

在 daattali 的基础上,它接受任意数量的输入,并为您为几种不同类型的输入分配值:

ui.R:

library(shiny)

shinyUI(fluidPage(
textInput("symbol", "Symbol Entry", ""),

dateInput("date_start", h4("Start Date"), value = "2005-01-01" ,startview = "year"),

selectInput("period_select", label = h4("Frequency of Updates"),
            c("Monthly" = 1,
              "Quarterly" = 2,
              "Weekly" = 3,
              "Daily" = 4)),

sliderInput("smaLen", label = "SMA Len",min = 1, max = 200, value = 115),br(),

checkboxInput("usema", "Use MA", FALSE)

))

服务器.R:

shinyServer(function(input, output,session) {
observe({
 query <- parseQueryString(session$clientData$url_search)

 for (i in 1:(length(reactiveValuesToList(input)))) {
  nameval = names(reactiveValuesToList(input)[i])
  valuetoupdate = query[[nameval]]

  if (!is.null(query[[nameval]])) {
    if (is.na(as.numeric(valuetoupdate))) {
      updateTextInput(session, nameval, value = valuetoupdate)
    }
    else {
      updateTextInput(session, nameval, value = as.numeric(valuetoupdate))
    }
  }

 }

 })
})

要测试的示例网址:127.0.0.1:5767/?symbol=BBB,AAA,CCC,DDD&date_start=2005-01-02&period_select=2&smaLen=153&usema=1

【讨论】:

【解决方案3】:

Shiny App:如何通过 URL 传递多个令牌/参数

通过 url 传递给闪亮应用的令牌的标准分隔符是 &amp; 符号。

闪亮的应用代码示例:

server <- function(input, output, session) {
  observe({
    query <- parseQueryString(session$clientData$url_search)
    if (!is.null(query[['paramA']])) {
        updateTextInput(session, "InputLabel_A", value = query[['paramA']])
    }
    if (!is.null(query[['paramB']])) {
        updateTextInput(session, "InputLabel_A", value = query[['paramB']])
    }
  })
  # ... R code that makes your app produce output ..
}

对应的网址示例: http://localhost.com/?paramA=hello&?paramB=world

参考:parseQueryString Docs

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2019-08-26
    • 2017-02-21
    • 2018-08-05
    相关资源
    最近更新 更多