繁体   English   中英

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

[英]renderPlot into renderUI - error object of type 'closure' is not subsettable

我想在我的闪亮应用程序中显示一个图:如果我动态选择 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”(或其他),我闪亮的应用程序中就会有一个空白区域。

这现在应该可以工作了。 您想在reactive() 内部调用input$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 工作的方法,下面的代码应该对你有用。 为了解决这个问题,我创建了 conditionalPanel 项目,这些项目仅在“条件”参数中的项目为真(选择了 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM