renderPlot到renderUI - 类型为' closure'的错误对象不是子集

时间:2017-01-17 14:31:46

标签: r shiny

我希望在我的闪亮应用中显示仅限一个情节:如果我动态选择A,闪亮的应用必须显示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) ,否则闪亮的应用必须显示 grViz 类对象。

plotOutput("plot")

错误:类型&#39;关闭的对象&#39;不是子集表

一个可能的解决方案是放入ui.R grVizOutput("plot2")(用于绘制情节(c(1:3)))和private void writeNewPost(String userId, String username, String title, String body) { // Create new post at /user-posts/$userid/$postid and at // /posts/$postid simultaneously String key = mDatabase.child("posts").push().getKey(); Post post = new Post(userId, username, title, body); Map<String, Object> postValues = post.toMap(); Map<String, Object> childUpdates = new HashMap<>(); childUpdates.put("/posts/" + key, postValues); childUpdates.put("/user-posts/" + userId + "/" + key, postValues); mDatabase.updateChildren(childUpdates); } (用于绘制grviz对象),但我没有&#39我想要它,因为如果我不选择选项&#34; A&#34; (或其他方式),我闪亮的应用程序中会有一个空白区域。

1 个答案:

答案 0 :(得分:1)

现在应该可以了。你想在reactive()中调用input $ options。由于该值在加载之后才初始化,因此如果为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)

修改

有趣的编辑。下面的代码应该适合你,即使我找不到一种方法让grViz在reactivePlot()中工作。为了解决这个问题,我创建了仅在'condition'参数中的项为true(选择了A或B)时才显示的conditionalPanel项。您可以通过在“条件”参数中添加项目而不创建任何新的conditionalPanel来修改此条件。然后,您只需要在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)