checkBoxGroupInput和if语句(包括数据)

时间:2017-04-15 20:06:10

标签: r checkbox shiny

我在(Errors in if statements in R Shiny checkBoxGroupInput

之前发布了类似的问题

但有人指出我没有提供相关数据。这是我的代码:

library(shiny)
library(leaflet)
library(DT)
library(ggplot2)
library(dplyr)

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

plotdata <- read.csv("C:/Users/Anatoly/Documents/Collatz/RenameLater/RenameLater/MapsAndSuch/RShinyCoral.csv")
colnames(plotdata) <- c("Year1", "RLIMona", "Year2", "RLICatalina", "Year3", "RLILaParguera1998", "Year4", "RLILAPARGUERA2004")
parguera <- read.csv("C:/Users/Anatoly/Documents/Collatz/RenameLater/RenameLater/MapsAndSuch/RShinyCoral.csv")
parguera <- select(parguera, 5:8)
colnames(parguera) <- c("Year", "1998 Expedition", "Year", "2004 Expedition")
monaisland <- read.csv("C:/Users/Anatoly/Documents/Collatz/RenameLater/RenameLater/MapsAndSuch/RShinyCoral.csv")
monaisland <- select(monaisland, 1:2)
colnames(monaisland) <- c("Year", "Mona Island RLI")
islacatalina <- read.csv("C:/Users/Anatoly/Documents/Collatz/RenameLater/RenameLater/MapsAndSuch/RShinyCoral.csv")
islacatalina <- select(islacatalina, 3:4)
colnames(islacatalina) <- c("Year", "Isla Catalina RLI")



ui <- fluidPage(
    titlePanel("NOAA Coral Luminescence Data (RLI, 5-year Running Average)"),
  leafletOutput("mymap"),
  p(),
  fluidRow(
  column(3, actionButton("laparguera", "La Parguera Data"),
  actionButton("mona", "Mona Island Data"),
  actionButton("isla", "Isla Catalina Data"))),
  fluidRow(
  column(3, offset = 5, actionButton("visualize", "Visualize Data"))),
  fluidRow(
  column(7, offset = 5, checkboxGroupInput("checkbox", "Add to plot", 
  c("La Parguera" = "La Parguera", "Mona Island" = "Mona Island", "Isla Catalina" = "Isla Catalina"))),
  fluidRow(
  DT::dataTableOutput('tbl'), 
  plotOutput("plot1")
)
)
)
server <- function(input, output, session) {

  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addMarkers(lat = 17.95, lng = - 67.05, popup = "La Parguera ") %>%
      addMarkers(lat = 18.00, lng = -67.50, popup = "Mona Island") %>%
      addMarkers(lat = 18.2, lng = -69.00, popup = "Isla Catalina")
  })
  observeEvent(input$laparguera, {
    output$tbl <- DT::renderDataTable(DT::datatable(parguera, options = list(pagelength = 25)))
  })
  observeEvent(input$mona, {
    output$tbl <- DT::renderDataTable(DT::datatable(monaisland, options = list(pagelength = 25)))
  })
  observeEvent(input$isla, {
    output$tbl <- DT::renderDataTable(DT::datatable(islacatalina, options = list(pagelength = 25)))
  })
  observeEvent(input$visualize, {
    output$plot1 <- renderPlot( 
  {
    if (input$checkbox == c("Mona Island"))
    {
    gplot <- ggplot(data = plotdata) +
    geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
      ylab("Candelas (5-year Running Average)" )
    print(gplot)
    }
    else if(input$checkbox == c("La Parguera", "Mona Island"))
    {
      gplot2 <- ggplot(data = plotdata) +
        geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
        ylab("Candelas (5-year Running Average)" )
      print(gplot2)
    }
    else if (input$checkbox == c("La Parguera", "Mona Island", "Isla Catalina")) 
    {
      gplot3 <- ggplot(data = plotdata) +
        geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
        ylab("Candelas (5-year Running Average)" )
      print(gplot3)
    }
    else if(input$checkbox == c("Mona Island", "Isla Catalina")) 
    {
      gplot4 <- ggplot(data = plotdata) +
        geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
        ylab("Candelas (5-year Running Average)" )
      print(gplot4)
    }
  else if(input$checkbox == c("La Parguera", "Isla Catalina")) 
  {
    gplot5 <- ggplot(data = plotdata) +
      geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
      ylab("Candelas (5-year Running Average)" )
    print(gplot5)
  }
  else if(input$checkbox == c("Isla Catalina")) 
  {
    gplot6 <- ggplot(data = plotdata) +
      geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
      ylab("Candelas (5-year Running Average)" )
    print(gplot6)
  }
  else if(input$checkbox == c("La Parguera")) 
  {
    gplot7 <- ggplot(data = plotdata) +
      geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
      ylab("Candelas (5-year Running Average)" )
    print(gplot7)
  }
  else if(is.null(input$checkbox)) 
  {
   print("Check some damn boxes")
  }

      })

})
}

shinyApp(ui, server)

这是我的数据:filedropper.com/rshinycoral

我在使用if语句确定哪个绘图将出现在输出中时出现问题,具体取决于选中的框。请暂时忽略我的情节相同的事实,我稍后会更改。

我知道使用if语句不是最佳,但我在使用Enzo的建议时遇到了麻烦。

谢谢!

1 个答案:

答案 0 :(得分:1)

input $复选框的返回值是一个字符向量。我认为你的问题与闪亮和复选框无关,而是与if语句和向量无关。

当R在if语句中计算谓词时,它只使用向量中的第一个项(这与ifelse的行为不同)。例如,尝试运行以下代码并观察结果和警告消息:

a <- c("a", "b", "c")

if (a == c("a", "d")) {
   TRUE
} else {
  FALSE
}

因此,要修复您的应用,您需要考虑如何查找匹配的向量。一种方法是将all%in%一起使用,例如:

if (all(c("a", "d") %in% a)) {
  TRUE
} else {
  FALSE 
}

if (all(c("a", "b") %in% a)) {
  TRUE
} else {
  FALSE
}