2017-01-17 3 views
1

プロット:動的にAを選択した場合、光沢のあるアプリケーションはplot(c(1:3))を表示する必要があります。 grVizクラスオブジェクト。レンダリングUIへのrenderPlot - サブタイプのタイプのエラーオブジェクト

library(shiny) 

runApp(list(
    ui = fluidPage(
    uiOutput("optionsplots"), 
    uiOutput("plot")), 
    server = function(input, output) { 


    output$optionsplots <- renderUI({ 
     selectInput("options", "Options to plot:",choices=c("A","B")) 
    }) 


    output$plot <- renderUI({ 


     if(input$options == 'A'){renderPlot({plot(c(1:3))})} 
     else{renderGrViz({grViz("digraph boxes_and_circles { 

          # a 'graph' statement 
          graph [overlap = true, fontsize = 10] 

          # several 'node' statements 
          node [shape = box, 
          fontname = Helvetica] 
          A; B; C; D; E; F 

          node [shape = circle, 
          fixedsize = true, 
          width = 0.9] // sets as circles 
          1; 2; 3; 4; 5; 6; 7; 8 

          # several 'edge' statements 
          A->1 B->2 B->3 B->4 C->A 
          1->D E->A 2->4 1->5 1->F 
          E->6 4->6 5->7 6->7 3->8 
     }")})} 
    }) 
    } 
),launch.browser = T) 

エラー:とgrVizOutput("plot2"):一つの可能​​な解決策はui.R plotOutput("plot")(3))のプロットをプロットするための(C(1)を入れられる

タイプのオブジェクトの閉鎖が "subsettableではありません(grvizオブジェクトをプロットするため)しかし、私はオプション "A"(またはそうでなければ)を選択しないと、私の光るアプリに空白のスペースがあるので、私はそれが欲しいわけではありません。

答えて

1

これで動作するはずです。あなたは入力$オプションをreact()の中で呼びたいと思っています。値はロード後まで初期化されていないので、nullの場合は 'A'に設定します。

library(shiny) 

runApp(list(
    ui = fluidPage(
    uiOutput("optionsplots"), 
    plotOutput("plot")), 
    server = function(input, output) { 

    getOption = reactive({ifelse(is.null(input$options),'A',input$options)}) 

    output$optionsplots <- renderUI({ 
     selectInput("options", "Options to plot:",choices=c("A","B")) 
    }) 
    output$plot <- renderPlot({ 
     if(getOption() == 'A'){plot(c(1:3))} 
    else{plot(c(1:6))} 
    }) 
    } 
),launch.browser = T) 

編集

興味深い編集。私がreactPlot()内でgrVizを動作させる方法を見つけることができなかったとしても、以下のコードはあなたのために働くはずです。この問題を回避するために、私はconditional引数の項目が真(AまたはBが選択された)のときにのみ表示されるconditionalPanel項目を作成しました。新しいconditionalPanelsを作成せずに項目を 'condition'引数に追加するだけで、多くの条件でこれを変更できます。それでは、例えばreactPlot()メソッド内にif(getOption()== 'C')plot()...を追加するだけです。

runApp(list(
    ui = fluidPage(
    uiOutput("optionsplots"), 
    conditionalPanel(condition = "['A'].indexOf(input.options)>=0",plotOutput(outputId='plot1')), 
    conditionalPanel(condition = "['B'].indexOf(input.options)>=0",grVizOutput(outputId='plot2')), 
    h2('next item')), 
    server = function(input, output) { 

    getOption = reactive({ifelse(is.null(input$options),'_',input$options)}) 

    output$plot1 = reactivePlot(function(){ 
     plot(c(1:3)) 
    }) 

    output$plot2 = renderGrViz({grViz("digraph boxes_and_circles { 

       # a 'graph' statement 
       graph [overlap = true, fontsize = 10] 

       # several 'node' statements 
       node [shape = box, 
       fontname = Helvetica] 
       A; B; C; D; E; F 

       node [shape = circle, 
       fixedsize = true, 
       width = 0.9] // sets as circles 
       1; 2; 3; 4; 5; 6; 7; 8 

       # several 'edge' statements 
       A->1 B->2 B->3 B->4 C->A 
       1->D E->A 2->4 1->5 1->F 
       E->6 4->6 5->7 6->7 3->8 
     }")}) 

    output$optionsplots <- renderUI({ 
     selectInput("options", "Options to plot:",choices=c('_','A','B')) 
    }) 

    } 
     ),launch.browser = T) 
+0

はい、これは前の質問に対して完全に機能します。 私はちょうど質問を修正し、それをよりよく説明します。申し訳ありませんが、ありがとうございます –

+0

1つの質問、if!= Aを条件付きのpannelに入れることに興味があれば、どのようにコードを修正するべきですか?conditionalPanel(condition = "['A']。indexOf(input.options)> = 0"、plotIutput(outputId = 'plot1')) '? –

+0

うれしいです。 '.indexOf()'はJavaScriptメソッドです。 '['A']'のリストに 'inputs.options'が含まれていなければ-1を返します。したがって、あなたは 'conditionalPanel(condition =" ['A']。indexOf(input.options)== - 1 "、plotOutput(outputId = 'plot1'))'と言うことができます。リストを持っている場合(単にA以上)、このメソッドを使うと便利です。あなたは 'input.options!= 'A''と言うことができます – danielson

関連する問題