【问题标题】:Vlookup based on selectizeInput基于 selectizeInput 的 Vlookup
【发布时间】:2022-10-23 23:03:04
【问题描述】:

我目前正在使用 R Shiny 创建一个 Web 应用程序。我有一个现有的数据框(从 excel 导入)并且想要创建一个较小的表格以在 Web 应用程序中显示。

原始数据框(导入)如下所示(但更大):

ID      date        colour
1231    1972/02/28  red
1314    1966/01/18  blue
1476    1980/11/18  green
2356    1995/03/09  orange
1332    1991/09/10  white

我创建了一个侧边栏面板,用户可以在其中选择他们想要显示信息的 ID:

sidebarPanel(selectizeInput("ID","Select ID",choices = DF$ID, selected = "1332"))

我想创建一个较小的表格,它将根据侧边栏面板中选择的 ID 显示日期和颜色,例如如果选择了 ID = 2356,我希望表格显示以下内容:

ID      2356
date    1995/03/09
colour  orange

有没有办法可以根据 ID 查找日期和颜色?我尝试过使用以下内容:

webapp_table = data.frame(matrix(ncol = 1,nrow = 2))
x2 = c("Date","Colour")
row.names(df2) = x2

webapp_table[1,1] = reactive({req(input$policynumber)
                     dt = as.Date(DF$date[which(DF$ID== input$ID)])
                     return(dt)})

但我收到以下错误: x[[jj]][iseq] <- vjj 中的错误: 子赋值类型修复中的不兼容类型(从闭包到逻辑)

【问题讨论】:

标签: r dataframe shiny vlookup selectinput


【解决方案1】:

正如代码的 cmets 中所讨论的,我会一步一步来

library(shiny)
library(tidyverse)

# Get the data
data <- tribble(
  ~ID,     ~date,         ~colour,
  1231L,    "1972-02-28",  "red",
  1314L,    "1966-01-18",  "blue",
  1476L,    "1980-11-18",  "green",
  2356L,    "1995-03-09",  "orange",
  1332L,    "1991-09-10",  "white"
  ) 

# Find the unique choices to lookup 
values_ID <- data |> 
  distinct(ID) |> 
  arrange(ID) |> 
  pull(ID)

# Define UI
ui <- fluidPage(
  
  # Application title
  titlePanel("Lookup App"),

  # Add a dropdown for ID 
  sidebarLayout(
      sidebarPanel(
          selectInput(
            inputId = "id", 
            label = "Select an ID", 
            choices = values_ID
            )
          ),

      # Show the output
      mainPanel(
         tableOutput("table")
         )
      )
  )

# Define server logic
server <- function(input, output) {
  
  # Filter the data based upon the lookup
  data_lookup <- reactive({
    data |> 
      filter(ID == input$id)
  })

  # Display the output
  output$table <- renderTable({
    data_lookup()
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多