如何在 Shiny 中使用 if 语句 select 多个输入案例?

How to select multiple input case using if statement in Shiny?

我正在开发一个闪亮的应用程序来更好地可视化地震。我有两个属性,分别是深度和震级,我想根据两者的深度、震级和 selection 绘制 3 个不同的图。但是,我不能 select 使用 if 语句来处理多个输入的情况。我怎样才能做到这一点?提前致谢

这是数据和代码。

 structure(list(latitude = c(39.597, 39.2815, 40.6823, 40.427, 
 39.0462, 40.854), longitude = c(34.3363, 40.2353, 36.6918, 26.2398, 
 41.4552, 27.9235), depth = c(8.6, 5, 13.7, 7.2, 12.9, 14.9), 
  magnitude = c(5.2, 5.5, 5.1, 5.1, 5.1, 5.1), time = structure(c(-7304, 
-7271, -7067, -7059, -6974, -6716), class = "Date")), .Names = 
 c("latitude", 
 "longitude", "depth", "magnitude", "time"), row.names = c(NA, 
  6L), class = "data.frame")
library(shiny)
library(sp)
library(aspace)

 ui <- fluidPage(headerPanel("Earthquakes"),
  dateRangeInput(inputId = "time","Date range:",format = "yyyy-mm-dd",min=min(turkat$time),max = max(turkat$time),start=min(turkat$time),end=max(turkat$time),separator="-" ),
            sidebarPanel(
              selectInput("att", "Select an attribute", choices=c("depth","magnitude"),selected="magnitude",multiple=TRUE,selectize=TRUE)),
            mainPanel(plotOutput("map1"))


 )
 server <- function(input, output) {


 myturkat <- reactive({
  turkat[turkat$time>=input$time[1]&turkat$time<=input$time[2],]
 })
   clr<-reactive({as.factor(cut(myturkat()$depth,breaks=c(0,20,40,60,Inf)))        })
  mag<-reactive({as.factor(cut(myturkat()$magnitude,breaks= seq(floor(min(myturkat()$magnitude)),ceiling(max(myturkat()$magnitude)))),include.lowest=TRUE)
  })



 output$map1 <- 
    renderPlot({ if (is.null(input$att)){ warning("select at least one attribute"); return(NULL)}


     else if(input$att=="magnitude"){
    req(myturkat())
    par(mar=c(5.1, 4.1, 3.1, 2.1))
     plot(turkiye, axes=TRUE,ylim=c(38,42),xlim=c(26,42))  
     points(myturkat()$longitude,myturkat()$latitude,pch=1,cex=myturkat()[,input$att]-(min(myturkat()[,input$att])-1),col="blue",lwd=1.2)
     }
      else if(input$att=="depth"){
       req(myturkat())
      par(mar=c(5.1, 4.1, 3.1, 2.1))
      plot(turkiye, axes=TRUE,ylim=c(38,42),xlim=c(26,42))  
      points(myturkat()$longitude,myturkat()$latitude,pch=16,cex=1.2,col=clr())
      lev<-levels(clr())
       legend("topright",lev,col=palette(),pch=16,title="depth (km)",pt.cex=1.2)}

     else if(input$att=="depth" && input$att=="magnitude") { 
     {req(myturkat())
       grup<-split(myturkat(),mag)
       par(mar=c(5.1, 4.1, 3.1, 2.1))
       plot(turkiye, axes=TRUE,ylim=c(38,42),xlim=c(26,42))
       for (i in 1: length(levels(mag)))
       {
         points(grup[[i]]$longitude,grup[[i]]$latitude,pch=i,cex=1,col=black)}
     }}}
 )
 }

      shinyApp(ui = ui, server = server)

程序给出了这个错误。

  Listening on http://127.0.0.1:3066
  Warning in renderPlot(...) : select at least one attribute
  Warning in if (input$att == "magnitude") { :
  the condition has length > 1 and only the first element will be used
  Warning in if (input$att == "depth") { :
  the condition has length > 1 and only the first element will be used

input$att 是一个可能包含两个元素的向量,但您要问它是否等于一个元素的向量。将 == 替换为 %in%(并切换顺序):

if ("magnitude" %in% input$att & "depth" %in% input$app) ...

另一种可能更好的解决方案是用 identical 替换等号,这不是向量化的:

if(identical(input$att,"magnitude")) {...}
else if(identical(input$att,"depth")) {...}
else if (identical(input$att, c("magnitude", "depth") | identical(input$att, c("depth","magnitude")) {...}