簡體   English   中英

如何從加載有fileInput數據的名稱添加名稱到閃亮應用程序中的selectInput?

[英]How to add names from loaded with fileInput data to selectInput in shiny app?

我想制作一個應用程序,用戶可以在其中加載一些數據,而不是自己瀏覽,因此,我現在想將數據集的名稱傳遞給selectInput

為此,我有一個簡單的應用程序,如下所示:

library("shiny")
library("readxl")

# Define UI 

ui <- fluidPage(

    titlePanel("TKM"),

    sidebarLayout(
        sidebarPanel(
            fileInput(inputId = "input",label = "input xlsx: ", multiple = FALSE, accept = c(".xlsx"))
        ),

        mainPanel(
           tableOutput("contents")
        )
    )
)


# Define server logic
server <- function(input, output) {

    df <- reactive({ read_xlsx(input$input$datapath) })


    output$contents <- renderTable({ head(df()) })
}

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

因此,如您所見,我的數據是反應性輸入,因此如果我添加一個簡單的數據:

selectInput(inputId = "x", label = "X: ", choices = colnames(df()))

我將無法使用以下應用程序運行: Error in df() : argument "x" is missing, with no default

任何習慣我如何將我的df的名稱傳遞給selectInput?

您可以執行以下操作。

library("shiny")
library("readxl")

# Define UI 

ui <- fluidPage(

  titlePanel("TKM"),

  sidebarLayout(
    sidebarPanel(
      fileInput(inputId = "input", label = "input xlsx: ", multiple = FALSE, accept = c(".xlsx"))
    ),

    mainPanel(
      uiOutput("columns"),
      tableOutput("contents")
    )
  )
)


# Define server logic
server <- function(input, output) {

  df <- eventReactive(input$input, { 
    read_xlsx(input$input$datapath) 
  })

  output$columns <- renderUI({
    req(df())
    selectInput("x", "Choose a column", choices = colnames(df()))
  })

  output$contents <- renderTable({
    req(df())
    head(df()) 
  })
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM