checkBoxGroupInput和if语句(包括数据) [英] checkBoxGroupInput and if statements (data included)

查看:114
本文介绍了checkBoxGroupInput和if语句(包括数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾发布过类似的问题( R Shiny中的if语句checkBoxGroupInput

I posted a similar question before (Errors in if statements in R Shiny checkBoxGroupInput)

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

but as someone pointed out I had not included relevant data. Here is my code:

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

And here is my data: filedropper.com/rshinycoral

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

I am having a problem using if statements to determine which plot will appear in the output, depending on which boxes are checked off. Please ignore for now the fact that my plots are the same, I'll change this later.

我知道使用if语句不是最优的,但是我遇到了麻烦

I am aware using if statements is not optimal, but I am having trouble using Enzo's suggestions.

谢谢!

推荐答案

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

The return value from input$checkbox is a character vector. I think your issue has nothing to do with shiny and checkboxes, rather if statements and vectors.

R在if语句中对谓词求值时,它仅使用向量中的第一项(这与 ifelse )。因此,例如,尝试运行以下代码并观察结果和警告消息:

When R evaluates your predicate in an if statement, it only uses the first item in the vector (this is different behaviour to ifelse). so for example, try running the following code and observe both the result and the warning message:

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

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

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

So to fix your app, you will need to think about how you look for matching vectors. One way is to use all with %in% e.g:

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

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

这篇关于checkBoxGroupInput和if语句(包括数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆