简体   繁体   English

复选框输入值 R shiny:如果为真,则

[英]checkboxInput value R shiny: if TRUE then

I have the following code.我有以下代码。 The objective is to make the position of the plot bars reactive to the selectInput value目的是使 plot 条的 position 对 selectInput 值起反应

library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)
library(shinythemes)
library(plotly)
library(ggthemes)
library(lubridate)



data <- data.frame(mitarbeiter = c("AA", "BB", "CC", "DD", "EE", "FF"), 
         art = c("hr", "GG", "TT", "RR", "OO", "OO"),
         creadate = as_date(c("2018-01-03", "2018-01-03", "2018-01-03", "2018-01-03", "2018-01-03", "2018-01-03")))

mitarbeiter1 <- sort(unique(data$mitarbeiter))
art1 <- sort(unique(data$art))

year_month <- function(dates) {
  paste(lubridate::year(dates),
        str_pad(lubridate::month(dates), width = 2, pad = 0),
        sep="-")
}

year_week <- function(dates) {
  paste(lubridate::year(dates),
        str_pad(lubridate::week(dates), width = 2, pad = 0),
        sep="-")
}

year_day <- function(dates) {
  paste(lubridate::year(dates),
        str_pad(lubridate::month(dates), width = 2, pad = 0),
        str_pad(lubridate::day(dates), width = 2, pad = 0),
        sep="-")
}


ui <- fluidPage(
  fluidRow(
    column(4,
           pickerInput("mitarbeiterName", "Name des Mitarbeiters", mitarbeiter1, 
                       options = list(`actions-box` = TRUE), multiple = TRUE),
           pickerInput("artName", "Art", art1, 
                       options = list(`actions-box` = TRUE), multiple = TRUE),
           pickerInput("period", "Zeitraum", c("day", "week", "month", "year"), 
                       options = list(`actions-box` = TRUE)),
           dateRangeInput("date", "Datum auswahlen", start  = "2020-01-01"),
           checkboxInput("kumulativ", "Kumulativ"),
           downloadButton("download", "Download")
    ),
    column(8,
           plotlyOutput("policyPlot")
    )
  )
)

server <- function(input, output, session) {
  
  #create a reactive object with a NULL starting value
  listofrows <- reactiveValues(data = NULL)
  
  #observe the changes in inputs and update the reactive object 
  observeEvent(c(input$mitarbeiterName, input$artName, input$date, input$period), {
    req(input$mitarbeiterName)
    req(input$artName)
    req(input$period)
    req(input$date)

    listofrows$data <- subset(data, mitarbeiter %in% input$mitarbeiterName &
                                art %in% input$artName & 
                                creadate >= input$date[1] & creadate <= input$date[2]) 
  }, ignoreInit = T, ignoreNULL = TRUE)
  
  output$policyPlot <- renderPlotly({
    req(listofrows$data)
    req(input$kumulativ)
    
    fn <- switch(
      input$period,
      day = year_day,
      week = year_week,
      month = year_month,
      year = year
    )
    
    pos <- if (input$kumulativ) "dodge" else "identity"
    
    ggplot(listofrows$data) +
      geom_bar(aes(x = fn(creadate), fill = mitarbeiter), 
               stat = "count", 
               position = pos,
               show.legend = T) +
      ggtitle("Anzahl erstellte Policen (pro Mitarbeiter)") +
      xlab("Zeitraum") + ylab("Anzahl der Policen")
  })
  
  output$download <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".png", sep = "")
    },
    content = function(file) {
      ggsave(file, plot = output$policyPlot)
    })
}

shinyApp(ui, server)

Now, I want:现在,我想要:

  • the position to be "dodge" if checkboxInput = TRUE, and如果 checkboxInput = TRUE,则 position 为“闪避”,并且
  • the position to be "identity" if checkboxInput = FALSE.如果 checkboxInput = FALSE,则 position 为“身份”。

does someone have any suggestion how to do that?有人有什么建议吗? How can we do the if condition with the checkbox value?我们如何使用复选框值执行 if 条件?

In your case, req(input$kumulativ) doesn't work.在您的情况下, req(input$kumulativ)不起作用。 It's because req checks if a value is "truthy", and FALSE is not considered truthy.这是因为req检查一个值是否“真实”,而FALSE不被认为是真实的。 Therefore, you can change it to:因此,您可以将其更改为:

req(!is.null(input$kumulativ))

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

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