简体   繁体   English

防止闪亮的应用渲染图中的错误

[英]prevent error in shiny app render plot

Hallo I have a problem inside my shiny app. 你好我的闪亮应用程序里面有问题。 The app works fine but in the beginning there is the error: 该应用程序工作正常,但在一开始就有错误:

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

I made an example code which reproduces the error without the other stuff of the app for simplicity: 我做了一个示例代码,为了简单起见,在没有应用程序的其他内容的情况下重现错误:

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)

Does anybody know how to prevent this error or if not to hide the output of this special error? 有人知道如何防止此错误或是否隐藏此特殊错误的输出? Many thanks! 非常感谢!

Use shiny::validate to delay the evaluation of the plot until the renderUI has finished and passed a value back to the server: 使用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)

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

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