如何在SelectInput搜索栏中查看所有可用值?

时间:2017-11-20 13:36:50

标签: r shiny

我正在学习使用Shiny构建仪表板并遇到以下问题,希望有人在这里解释我的错误。首先,我希望看到' Hair'和'物种'当我点击搜索栏时。我认为过滤会完成这项工作,但它不起作用。我还尝试使用choices = hair作为额外的selectInput参数,但它也不起作用。

library(shiny)
library(dplyr)
library(DT)

?starwars


# Step 1 - prepare row data
starwars_data = as_tibble(starwars_data)

# a) add missing info
starwars_data = starwars %>%
  mutate(
    ID = rownames(starwars),
    height = case_when(
      name == 'Finn' ~ as.integer(178),
      name == 'Rey' ~ as.integer(170),
      name == 'Poe Dameron' ~ as.integer(172),
      name == 'BB8' ~ as.integer(67),
      name == 'Captain Phasma' ~ as.integer(200),
      TRUE ~ height
    ),
    mass = case_when(
      name == 'Finn' ~ 73,
      name == 'Rey' ~ 54,
      name == 'Poe Dameron' ~ 80,
      name == 'BB8' ~ 18,
      name == 'Captain Phasma' ~ 76,
      TRUE ~ mass
    ),
    film_counter = lengths(films),
    vehicle_counter = lengths(vehicles),
    starship_counter = lengths(starships)
  )


# 2) Prepare layout

hair = starwars_data %>% 
  select(hair_color) %>% 
  distinct() %>%
  `$`('hair')


spec = starwars_data %>% 
  select(species) %>% 
  distinct() %>% 
  `$`('spec')


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput('hair', 'Hair', hair),
      selectInput('spec', 'Species', spec)
    ),
    mainPanel(
      #plotlyOutput('plot'),
      #dataTableOutput('table'))
    )
  )
)


srv <- function(input, output){

  d = reactive({starwars_data %>%
      filter(hair_color %in% input$hair)})

  c = reactive({starwars_data %>%
      filter(species %in% input$spec)})

  #output$table = renderDataTable({datatable(d)})

}

shinyApp(ui, srv)

感谢您对此有所帮助。

1 个答案:

答案 0 :(得分:0)

在此修改您的代码:

hair = starwars_data %>% 
  select(hair_color) %>% 
  distinct() 

如果您希望表格显示为hair

,则更新代码
   library(shiny)
library(dplyr)
library(DT)
library(tibble)

?starwars


# Step 1 - prepare row data
starwars_data = as_tibble(starwars_data)

# a) add missing info
starwars_data = starwars %>%
  mutate(
    ID = rownames(starwars),
    height = case_when(
      name == 'Finn' ~ as.integer(178),
      name == 'Rey' ~ as.integer(170),
      name == 'Poe Dameron' ~ as.integer(172),
      name == 'BB8' ~ as.integer(67),
      name == 'Captain Phasma' ~ as.integer(200),
      TRUE ~ height
    ),
    mass = case_when(
      name == 'Finn' ~ 73,
      name == 'Rey' ~ 54,
      name == 'Poe Dameron' ~ 80,
      name == 'BB8' ~ 18,
      name == 'Captain Phasma' ~ 76,
      TRUE ~ mass
    ),
    film_counter = lengths(films),
    vehicle_counter = lengths(vehicles),
    starship_counter = lengths(starships)
  )


# 2) Prepare layout

hair = starwars_data %>% 
  select(hair_color) %>% 
  distinct() 


spec = starwars_data %>% 
  select(species) %>% 
  distinct() 


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput('hair', 'Hair', hair),
      selectInput('spec', 'Species', spec)
    ),
    mainPanel(
      #plotlyOutput('plot'),
      dataTableOutput('table'))
    )
  )
)


srv <- function(input, output){

  d = reactive({starwars_data %>%
      filter(hair_color %in% input$hair)})

  c = reactive({starwars_data %>%
      filter(species %in% input$spec)})

  output$table = renderDataTable({datatable(d())})

}

shinyApp(ui, srv)