Я хотел бы создать такое приложение, как показано ниже, только в начале нужно выбрать количество цилиндров. В следующем примере выбирается база данных, я бы хотел отказаться и сразу перейти к выбору цилиндров. Могу ли я использовать здесь опцию фильтра?

Моя идея:

 df_mtcars <- reactive({
    cylinder_selected <- as.numeric(input$si_cylinders[1])        
    df <- mtcars %>% filter(cyl == cylinder_selected)        
    return(df)
  })

Мой код:

ui:

library(radarchart)
shinyUI(pageWithSidebar(      
  headerPanel("Car Comparison Radar"),      
  sidebarPanel(
    uiOutput("choose_dataset"),
    uiOutput("choose_car"),
    uiOutput("choose_columns")
  ),          
  mainPanel(
    chartJSRadarOutput('radar', height = '350px')    
  )
))

сервер:

library(shiny)
library(radarchart)

shinyServer(function(input, output) {

  # choose dataset but I want choose cyl
  output$choose_dataset <- renderUI({
    data_sets <- "mtcars"
    selectInput("dataset", "Data set", data_sets)
  })

  # select a car
  output$choose_car <- renderUI({
    selectInput("car","car",as.list(rownames(get(input$dataset))))
  })

  # Check boxes
  output$choose_columns <- renderUI({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()

    # Get the data set with the appropriate name
    dat <- get(input$dataset)
    colnames <- names(dat)

    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  })            
  output$radar <- renderChartJSRadar({

    # Get the data set
    dat <- get(input$dataset)

    # Make sure columns are correct for data set (when data set changes, the
    # columns will initially be for the previous data set)
    if (is.null(input$columns) || !(input$columns %in% names(dat)))
      return()

    # Keep the selected columns
    dat <- dat[, input$columns, drop = FALSE]

    #reform data for plot
    dat <- as.data.frame(t(dat), stringsAsFactors = FALSE)
    dat$labs <- row.names(dat)
    dat <- dat[, c('labs', input$car)]
    chartJSRadar(dat)
  })
 })
0
Elia 27 Апр 2018 в 17:44

1 ответ

Лучший ответ

Поскольку это uiOutput("choose_car") дает вам все возможности автомобиля. Если вы добавите selectInput для выбора цилиндров, у вас возникнет проблема, потому что у вас есть определенные автомобили для каждого номера цилиндра.

Таким образом, у вас может быть selectInput для автомобилей с условием selectInput для цилиндров.

Вы можете удалить uiOutput("choose_dataset") самостоятельно, поэтому на своем примере вы можете попробовать следующее:

ui = pageWithSidebar(      
  headerPanel("Car Comparison Radar"),      
  sidebarPanel(
    uiOutput("choose_dataset"),
    uiOutput("choose_cyclinder"),
    uiOutput("choose_car"),
    uiOutput("choose_columns")
  ),          
  mainPanel(
    chartJSRadarOutput('radar', height = '350px')    
  )
)



server = function(input, output) {


  output$choose_cyclinder <- renderUI({

    temp <-  mtcars %>% group_by(cyl) %>% summarise(Counts = n()) 
    cyl <- levels(as.factor(temp$cyl))
    selectInput("select_cyl", "Choose a cylinder", as.list(cyl), selected=TRUE, multiple = FALSE)
  })



  # choose dataset but I want choose cyl
  output$choose_dataset <- renderUI({
    data_sets <- "mtcars"
    selectInput("dataset", "Data set", data_sets)
  })

  # select a car
  output$choose_car <- renderUI({

    dat <- get(input$dataset)
    dat <- dat %>% tibble::rownames_to_column('carnames') %>%
    filter(cyl %in% c(input$select_cyl)) %>%
      tibble::column_to_rownames('carnames')
    selectInput("car","car",as.list(rownames(dat)))
  })



  # Check boxes
  output$choose_columns <- renderUI({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()

    # Get the data set with the appropriate name
    dat <- get(input$dataset)
    colnames <- names(dat)

    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  })            
  output$radar <- renderChartJSRadar({

    # Get the data set
    dat <- get(input$dataset)
   # dat <- mtcars

    # Make sure columns are correct for data set (when data set changes, the
    # columns will initially be for the previous data set)
    if (is.null(input$columns) || !(input$columns %in% names(dat)))
      return()

    # Keep the selected columns
    dat <- dat[, input$columns, drop = FALSE]

   # dat <- dat %>% filter(cyl %in% c(input$select_cyl))

    #reform data for plot
    dat <- as.data.frame(t(dat), stringsAsFactors = FALSE)
    dat$labs <- row.names(dat)
    dat <- dat[, c('labs', input$car)]
    chartJSRadar(dat)
  })
}

shinyApp(ui, server)

РЕДАКТИРОВАТЬ:

ui = pageWithSidebar(      
  headerPanel("Car Comparison Radar"),      
  sidebarPanel(
    #uiOutput("choose_dataset"),
    uiOutput("choose_cyclinder"),
    uiOutput("choose_car"),
    uiOutput("choose_columns")
  ),          
  mainPanel(
    chartJSRadarOutput('radar', height = '350px')    
  )
)



server = function(input, output) {


  output$choose_cyclinder <- renderUI({

    temp <-  mtcars %>% group_by(cyl) %>% summarise(Counts = n()) 
    cyl <- levels(as.factor(temp$cyl))
    selectInput("select_cyl", "Choose a cylinder", as.list(cyl), selected=TRUE, multiple = FALSE)
  })



  # choose dataset but I want choose cyl
#  output$choose_dataset <- renderUI({
#    data_sets <- "mtcars"
#    selectInput("dataset", "Data set", data_sets)
#  })

  # select a car
  output$choose_car <- renderUI({

   # dat <- get(mtcars)
    dat <- mtcars
    dat <- dat %>% tibble::rownames_to_column('carnames') %>%
      filter(cyl %in% c(input$select_cyl)) %>%
      tibble::column_to_rownames('carnames')
    selectInput("car","car",as.list(rownames(dat)))
  })



  # Check boxes
  output$choose_columns <- renderUI({
    # If missing input, return to avoid error later in function
  #  if(is.null(input$dataset))
   #   return()

    # Get the data set with the appropriate name
   # dat <- get(input$dataset)
    dat <- mtcars
    colnames <- names(dat)

    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  })            
  output$radar <- renderChartJSRadar({

    # Get the data set
    #dat <- get(input$dataset)
     dat <- mtcars

    # Make sure columns are correct for data set (when data set changes, the
    # columns will initially be for the previous data set)
    if (is.null(input$columns) || !(input$columns %in% names(dat)))
      return()

    # Keep the selected columns
    dat <- dat[, input$columns, drop = FALSE]

    # dat <- dat %>% filter(cyl %in% c(input$select_cyl))

    #reform data for plot
    dat <- as.data.frame(t(dat), stringsAsFactors = FALSE)
    dat$labs <- row.names(dat)
    dat <- dat[, c('labs', input$car)]
    chartJSRadar(dat)
  })
}

shinyApp(ui, server)
1
MLavoie 20 Июн 2018 в 22:03