【问题标题】:prevent error in shiny app render plot防止闪亮的应用程序渲染图中出现错误
【发布时间】:2017-03-14 15:16:23
【问题描述】:

您好,我的闪亮应用程序有问题。该应用程序运行良好,但一开始有错误:

Warning: Error in xy.coords: 'x' and 'y' lengths differ
Stack trace (innermost first):
    102: xy.coords
    101: plot.default
    100: plot
     99: renderPlot [C:/Users/Stuhlian/Desktop/ghj.R#29]
     89: <reactive:plotObj>
     78: plotObj
     77: origRenderFunc
     76: output$plot1
      1: runApp

为了简单起见,我制作了一个示例代码,它在没有应用其他内容的情况下重现错误:

ui <- fluidPage(
    mainPanel(uiOutput("samplematches"),
               plotOutput("plot1")

      )
    )

server <- function(input, output) {
re1<-data.frame(x=c(1:10),y=c(1:10))
re2 <-data.frame(x=c(1:10),y=c(1:10))
re3<-data.frame(x=c(1:10),no.matches.Sample1=c(1:10),Sample2=c(11,9,8,4,5,4,3,2,6,2))
result <- reactive({list(re,re2,re3)})


output$samplematches <- renderUI({if(length(grep("Sample", colnames(result()[[3]])))>0){sliderInput("samplematches", "Select Sample for matched masses",min=1, max=length(grep("Sample", colnames(result()[[3]]))), value=1,step=1)} else{return()}})
output$plot1 <- renderPlot({plot(result()[[3]][,1], result()[[3]][,grep("Sample", colnames(result()[[3]]))[input$samplematches]]) })

}

shinyApp(ui, server)

有人知道如何防止这个错误,或者如果不隐藏这个特殊错误的输出吗?非常感谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    使用shiny::validate 延迟绘图的评估,直到renderUI 完成并将值传回服务器:

    ui <- fluidPage(
      mainPanel(uiOutput("samplematches"),
                plotOutput("plot1")
    
      )
    )
    
    server <- function(input, output) {
      re1<-data.frame(x=c(1:10),y=c(1:10))
      re2 <-data.frame(x=c(1:10),y=c(1:10))
      re3<-data.frame(x=c(1:10),no.matches.Sample1=c(1:10),Sample2=c(11,9,8,4,5,4,3,2,6,2))
      result <- reactive({list(re1,re2,re3)})
    
    
      output$samplematches <- renderUI({
        shiny::validate(
          need(length(grep("Sample", colnames(result()[[3]])))>0, "A message!")
        )
          sliderInput("samplematches", "Select Sample for matched masses",min=1, max=length(grep("Sample", colnames(result()[[3]]))), value=1,step=1)
        })
    
       output$plot1 <- renderPlot({
         shiny::validate(
           need(input$samplematches, "Input a value!")
         )
         plot(result()[[3]][,1], result()[[3]][,grep("Sample", colnames(result()[[3]]))[input$samplematches]]) 
      })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2019-11-09
      • 2016-01-11
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2020-09-21
      • 2019-07-02
      • 1970-01-01
      相关资源
      最近更新 更多