如何在Shiny中使用if语句选择多个输入大小写?

时间:2018-12-20 12:36:59

标签: r shiny selectinput

我正在开发一个闪亮的应用程序,以更好地可视化地震。我有两个属性,分别是深度和幅度,并希望根据深度,幅度和两者的选择绘制3个不同的图。但是,我无法使用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

1 个答案:

答案 0 :(得分:0)

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")) {...}