如何通过URL将参数传递给闪亮的应用程序

use*_*875 28 r shiny shiny-server shinydashboard

在Web浏览器中,您将参数传递给网站

www.mysite.com/?parameter=1

我有一个闪亮的应用程序,我想在计算中使用传入网站的参数作为输入.那么可以像www.mysite.com/?parameter=1这样做,然后使用input!参数吗?

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

谢谢

Dea*_*ali 39

当应用根据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']])
      }
    })
  }
)
Run Code Online (Sandbox Code Playgroud)


小智 10

建立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)

))
Run Code Online (Sandbox Code Playgroud)

server.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))
    }
  }

 }

 })
})
Run Code Online (Sandbox Code Playgroud)

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