【问题标题】:renderPlot into renderUI - error object of type 'closure' is not subsettablerenderPlot 到 renderUI - 'closure' 类型的错误对象不是子集
【发布时间】:2017-06-01 16:05:58
【问题描述】:

我想在我的闪亮应用中显示只有一个情节:如果我动态选择 A,闪亮的应用必须显示 plot(c(1:3)),否则闪亮的应用必须显示 grViz 类对象。

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)

错误:“闭包”类型的对象不是子集

一种可能的解决方案是放入 ui.R plotOutput("plot")(用于绘制 plot(c(1:3)))和 grVizOutput("plot2")(用于绘制 grviz 对象),但我不想要它,因为如果我不选择选项“A”(或其他),我闪亮的应用程序中会有一个空白区域。

【问题讨论】:

    标签: r shiny


    【解决方案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() 中工作的方法,下面的代码也应该适合你。为了解决这个问题,我创建了仅在“条件”参数中的项目为真(选择 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)
    

    【讨论】:

    • 是的,这非常适合上一个问题。我刚刚修改了问题,并更好地解释它。对不起,谢谢
    • 它有效,谢谢!一个问题,如果我有兴趣将 !=A 放在条件面板中,我应该如何修改代码 conditionalPanel(condition = "['A'].indexOf(input.options)&gt;=0",plotOutput(outputId='plot1'))
    • 很高兴它有帮助。 .indexOf() 是一种 JavaScript 方法。如果inputs.options 不在列表['A'] 中,它将返回-1。所以你可以说conditionalPanel(condition = "['A'].indexOf(input.options)==-1",plotOutput(outputId='plot1‌​'))。仅当您有一个列表时才使用此方法(不仅仅是 A)。你可以说input.options!='A'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2014-08-01
    • 1970-01-01
    • 2018-11-01
    相关资源
    最近更新 更多