2017-09-13 6 views
0

私の仕事ではいくつかの問題が発生しています。問題を明確にするために、私はダイヤモンドデータを例として使用しています。は、光沢のあるアプリケーションのselectinputウィジェットにオプションNULLを含む

私は関数sca.plotを作成しました。引数color.by = NULLの場合は、散布図を作成し、すべての点を赤色にします。 color.byが因子変数の場合は、変数で散布図とカラーポイントを作成します。

私のインタラクティブな散布図を光沢のあるものにするのが私の質問です。 selectinputウィジェットにNULLオプションを含めるにはどうしたらいいですか?

selectinputの選択肢にNULLを含めると、エラーが発生します。それを理解できませんでした....

ありがとうございます。

以下

私のコードです:ここでは

library(ggplot2) 

sca.plot <- function (color.by=NULL) { 
    if (is.null(color.by)) { 
     diamonds %>% ggplot(aes(x, y))+ 
      geom_point (color='red') 
    } else { 
     color.by = sym(color.by) 
     diamonds %>% ggplot(aes_(~x, ~y, color=color.by))+ 
      geom_point() 
    } 
} 

sca.plot(color.by='clarity') 

ui <- fluidPage(
    sidebarLayout(
     sidebarPanel(
      selectInput('colorby', label = h5(strong('Color By')), 
         choices = list('clarity', 'cut'), 
         selected = NULL) 
     ), 
     mainPanel(
      plotOutput('plot') 
      ) 
     ) 
    ) 


server <- function(input, output) { 

    output$plot <- renderPlot ({ 
     sca.plot(color.by=input$colorby) 
    }) 
} 

runApp(shinyApp(ui, server)) 
+0

[This](https://github.com/rstudio/shiny/issues/559)のリンクが役立ちます。 – SBista

+0

@SBistaこのリンクは私の質問に答えます。ありがとう! – zesla

答えて

0

はあなたのためのソリューションです:

library(ggplot2) 
library(shiny) 

ui <- fluidPage(
    sidebarLayout(
    sidebarPanel(
     selectInput('colorby', label = h5(strong('Color By')), 
        choices = list('NULL','clarity', 'cut'), 
        selected = NULL) 
    ), 
    mainPanel(
     plotOutput('plot') 
    ) 
) 
) 


server <- function(input, output) { 

    output$plot <- renderPlot ({ 
    if(!input$colorby == 'NULL'){ 
    ggplot(diamonds, aes_string(x="x",y="y", color=input$colorby)) + 
     geom_point() 
     }else{ 
     ggplot(diamonds, aes(x=x,y=y)) + 
     geom_point(color='red') 
    } 
    }) 
} 
shinyApp(ui = ui, server = server) 

あなたはselectInputで引数としてではなく文字列としてNULLを使用することができます - >'NULL'

あなたは実際にあなたが光沢のあるアプリの初めに書いたような機能を必要としません、あなたは簡単にif...else...ステートメントを直接使用することができますrenderPlot()で、geom_point()の所望の色を得る。

+0

ありがとうございます。私は実際の仕事で機能が必要です。リンクSBista提供私の問題を解決しました。 – zesla

関連する問題