【问题标题】:Action button in Shiny app updates query in url with input from userShiny 应用程序中的操作按钮使用来自用户的输入更新 url 中的查询
【发布时间】:2019-08-06 20:05:28
【问题描述】:

我有这个应用程序:

library(shiny)

ui <- fluidPage(
  textInput("query_text","Type something:"),
  actionButton(inputId='query_button',
               label="Search", 
               icon = icon("th"), 
               onclick = paste("location.href='http://www.example.com?lookfor=",
                               input$query_text, "'", sep=""))
)

server <- function(input, output, session) {

}

shinyApp(ui, server)

我想用操作按钮更新 url,所以当用户输入一些东西(例如:纸)时,它会像这样更新 url:

http://www.example.com/?lookfor=paper

任何想法如何做到这一点?也许将它包装在一个观察事件上?

【问题讨论】:

  • 您是否要简单地将lookfor=&lt;input&gt; 添加到当前页面的 URL 中?您上面的示例代码是否假设闪亮的应用程序托管在example.com?或者您是否真的要完全更改 URL 栏中的 URL,包括域名?
  • 是的,我只是想将 '?lookfor=' 添加到当前页面! @DeanAttali
  • 我现在正在阅读您的“用于改进应用程序和解决常见问题的闪亮提示和技巧”(顺便说一句,做得很好!)。我已经看过“闪亮应用程序中的导航(历史中的前进/后退)”,但我不需要所有导航历史,只需添加 '?lookfor='!

标签: r shiny


【解决方案1】:

根据您对我的评论的回复,您正在寻找的是updateQueryString 函数。

library(shiny)

ui <- fluidPage(
  textInput("query_text", "Type something:"),
  actionButton(inputId = 'query_button', label = "Search")
)

server <- function(input, output, session) {
  observeEvent(input$query_button, {
    updateQueryString(paste0("?lookfor=", input$query_text))
  })
}

shinyApp(ui, server)

【讨论】:

  • 这正是我想要的!非常感谢与 Shiny 的出色(和鼓舞人心的)工作 =)
猜你喜欢
  • 2017-04-10
  • 2023-03-29
  • 2018-04-04
  • 1970-01-01
  • 2020-11-26
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
相关资源
最近更新 更多