2017-01-17 20 views
1

我想在我閃亮的應用顯示一個僅積:如果我動態選擇,閃亮的應用程序必須顯示plot(c(1:3)),否則閃亮的應用程序必須顯示一個grViz類對象。renderPlot到renderUI - 類型的錯誤對象「關閉」不subsettable

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) 

錯誤:類型的對象 '閉合' 不是subsettable

一種可能的解決方案將在ui.R plotOutput("plot")在投入(用於繪製繪圖(C(1:3)))和grVizOutput("plot2") (用於繪製grviz對象),但我不希望它,因爲如果我不選擇選項「A」(或其他),我的閃亮應用程序中會有一個空白區域。

回答

1

這應該現在工作。你想在reactive()中輸入$ options調用。由於該值在加載之後才被初始化,因此如果爲空,我也將其設置爲'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) 

編輯

有趣的編輯。即使我找不到在reactivePlot()中使grViz工作的方法,下面的代碼應該適用於您。爲了解決這個問題,我創建了僅當'condition'參數中的項爲真(選擇A或B)時才顯示的條件面板項目。您可以通過將條目添加到「條件」參數而不創建任何新的條件面板來修改這個條件。然後,例如,您只需要在reactivePlot()方法中添加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

它的工作原理,謝謝!一個問題,如果我有興趣把!= A在條件範圍內,我應該如何修改代碼'conditionalPanel(condition =「['A'] .indexOf(input.options)> = 0」,plotOutput(outputId = 'plot1'))'? –

+0

很高興幫助。 '.indexOf()'是一個JavaScript方法。如果'inputs.options'不在列表'['A']'中,它將返回-1。所以你可以說'conditionalPanel(condition =「['A']。indexOf(input.options)== - 1」,plotOutput(outputId ='plot1'))'。如果你有一個列表(不只是A),那麼使用這種方法是有用的。你可以說'input.options!='A'' – danielson

相關問題