2017-02-16 125 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)