2017-02-08 12 views
1

私のShinyアプリケーションでは、動的ベクトルをテキスト出力として表示できます。私はまた、textOutputrenderTextthis suggestionの代わりにhtmlOutputrenderUIを使用していますので、HTML(太字、フォントの色など)を使用したいと思います。ここでShinyのrenderUIを使用してベクトルを表示する

は、いくつかのサンプルコードです:

library(shiny) 

shinyApp(

    ui <- htmlOutput("example"), 

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

    # vector (in the real app, this is not a static vector--it will be updated with other inputs) 
    states <- c("Alabama", "Alaska", "Arizona", "Arkansas") 

    # text output 
    output$example <- renderUI({ 

     x <- paste0("<strong>Here are your states</strong>: ", states) 
     HTML(x) 

    }) #END RENDERUI 
    } #END SERVER 
) #END SHINYAPP 

このコードの結果は次のとおりです。ここで

は、あなたの状態は次のとおりです。ここではアラスカ 次のとおりです。ここでアラバマ州では、あなたの状態は、あなたの州:アリゾナあなたの州はここにあります:アーカンソー

私が欲しいもの

がある:ここでは

は、あなたの状態は次のとおりです。アラバマアラスカアリゾナアーカンソー

私は条件文を使用して解決策が出ているが、それはかなり不格好です。ここでも、上記の解決策は動作しますが、それはむしろ不格好だと、より大きなベクトルに対してひどく非効率的になります

x <- paste0("<strong>Here are your states: </strong>", 
      if(!is.na(states[1])){states[1]}, 
      if(!is.na(states[2])){states[2]}, 
      if(!is.na(states[3])){states[3]}, 
      if(!is.na(states[4])){states[4]}) 
HTML(x) 

(で、のは10+、要素を言わせて):ここには、私は上記の所望の出力のためにrenderUIに入れるものです。 HTMLを利用しながらこれらのベクトルを表示する簡単な方法はありますか?

答えて

1

あなたはpaste(..., collapse = " ")を探しています。

library(shiny) 

shinyApp(

    ui <- htmlOutput("example"), 

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

    # vector (in the real app, this is not a static vector--it will be updated with other inputs) 
    states <- c("Alabama", "Alaska", "Arizona", "Arkansas") 

    # text output 
    output$example <- renderUI({ 

     x <- paste0("<strong>Here are your states</strong>: ", paste(states, collapse = " ")) 
     HTML(x) 

    }) #END RENDERUI 
    } #END SERVER 
) #END SHINYAPP 
+1

はい、これはトリックです!あなたが自習しているときに、どれだけ多くの単純なものがミックスで失われているのは驚くべきことです。ありがとう! – Lauren

関連する問題