2017-02-16 7 views
7

私は光沢のあるアプリに、ユーザーの入力に基づいて生成されたURLに誘導するリンクを表示したいと思います。私は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マップで表示する」をクリックして、オンザフライで生成されたURLを参照することをお勧めします。助けてください、ありがとう。

答えて

5

あなたは反動UIを更新するためにuiOutputと一緒renderUIを使用する必要があります。

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の答えは移動するための方法です。

マップを維持し、すべての自己完結型の光沢で、むしろGoogleマップに新しいリンクを開くこと以外をfunciton検索したい場合は、同じタスクに

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) 
を達成するために、私の googlewayパッケージを使用することができます

enter image description here

+0

これは本当にクールです!私はgithub 'devtools :: install_github(" SymbolixAU/googleway ")'からインストールしました。 –

+0

@YuZhang - あなたがそれを好きでうれしい。それはまだ開発中ですので、若干変更されるかもしれませんが、一般的な使用のためには安定していなければなりません。 – SymbolixAU

+0

@YuZhang - マップウィジェットがCRAN上にあることに注意してください。開発バージョンはもう必要ありません – SymbolixAU

0

私は、URLを再帰的に

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) 
関連する問題