2017-02-16 108 views
7

我想在我的闪亮应用程序中显示一个链接,该链接指向基于用户输入生成的URL。我不想显示网址的全文。我知道如果事先知道URL,可以使用a(href =“”,label =“”)函数,但在这种情况下,URL取决于用户的输入。以下不起作用:嵌入反应生成的URL闪亮

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) 

我希望我可以点击“在Google地图中显示”并定向到动态生成的网址。请帮助我,谢谢。

回答

5

您需要同时使用renderUIuiOutput反应性地更新UI:

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) 
+0

网址非常感谢你一个HTML按钮!这正是我需要的。 –

+0

@YuZhang高兴我的回答有帮助,请接受它 – HubertL

0

如果这个问题其实是关于建立反应的URL链接,然后HubertL的答案是要走的路。

如果你想保持在地图和搜索功能可按所有自包含的光泽,而不必打开新的链接,谷歌地图,你可以用我的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

+0

这真的很酷!我从github安装'devtools :: install_github(“SymbolixAU/googleway”)''。 –

+0

@YuZhang - 很高兴你喜欢它。请注意,它仍处于开发阶段,因此可能会略有变化,但应该对于一般用途稳定 – SymbolixAU

+0

@YuZhang - 注意地图小部件现在位于CRAN上,不再需要开发版本 – SymbolixAU

0

我用它可以递归产生

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)