嵌入反应生成的URL有光泽

时间:2017-02-16 23:47:04

标签: r url shiny

我想在我的闪亮应用中显示一个链接,该链接指向根据用户输入生成的网址。我不想显示网址的全文。我知道如果我事先知道了URL,可以使用a(href ="",label ="")功能,但在这种情况下,URL取决于用户&#39输入。以下不起作用:

ui <- fluidPage(
    titlePanel("Show map of a given state"),
    sidebarLayout(
        sidebarPanel(
            textInput("state", label = "State", value = "CA", placeholder = "California or CA"),
            actionButton("showU","Show map")
        ),
        mainPanel(
            conditionalPanel(
                condition = "input.showU > 0",
                htmlOutput("url"),
                a(href=htmlOutput("url"),"Show in Google Map",target="_blank")
            )
        )
    )
)

server <- function(input, output){
    observeEvent(input$showU,{
    output$url <-renderUI({paste("https://www.google.com/maps/place/", input$state, sep="")})
    })
}

shinyApp(ui,server)

我希望我可以点击&#34;在Google地图中显示&#34;并被定向到动态生成的URL。请帮帮我,谢谢。

3 个答案:

答案 0 :(得分:5)

您需要将renderUIuiOutput一起使用来自动更新用户界面:

library(shiny)
ui <- fluidPage(
  titlePanel("Show map of a given state"),
  sidebarLayout(
    sidebarPanel(
      textInput("state", label = "State", value = "CA", placeholder = "California or CA"),
      actionButton("showU","Show map")
    ),
    mainPanel(
      conditionalPanel(
        condition = "input.showU > 0",
        uiOutput("url")
      )
    )
  )
)

server <- function(input, output){
  observeEvent(input$showU,{
    output$url <-renderUI(a(href=paste0('https://www.google.com/maps/place/', input$state),"Show in Google Map",target="_blank"))
  })
}

shinyApp(ui,server)

答案 1 :(得分:0)

如果这个问题实际上是关于创建反应性URL链接,那么HubertL的答案是要走的路。

如果你想让地图和搜索功能全部保持在闪亮的状态,而不是打开一个新的Google地图链接,你可以使用我的googleway包来完成同样的任务

library(shiny)
library(googleway)


ui <- fluidPage(
  titlePanel("Show map of a given state"),
  sidebarLayout(
     sidebarPanel(
     ),
    mainPanel(
      google_mapOutput(outputId = "myMap", height = "600px")

    )
  )
)

server <- function(input, output){

  ## you need a valid API key from Google Maps
  ## https://developers.google.com/maps/

  map_key <- "your_map_api_key"

  output$myMap <- renderGoogle_map({
    google_map(key = map_key, search_box = T)
  })

}

shinyApp(ui,server)

enter image description here

答案 2 :(得分:0)

我使用了一个HTML按钮,可以递归生成网址

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("HTML button"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot"),
         HTML(paste0(htmlOutput('url_test')))
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })

   output$url_test = renderText({
     paste('<a href=',cultivar_url(),' class="btn btn-default">Go to Google</a>')
   })

   cultivar_url = reactive({
     print('https://www.google.com')
   }) 
}

# Run the application 
shinyApp(ui = ui, server = server)