【问题标题】:How to add names from loaded with fileInput data to selectInput in shiny app?如何将加载了 fileInput 数据的名称添加到闪亮应用程序中的 selectInput?
【发布时间】:2019-07-18 07:49:49
【问题描述】:

我想制作一个应用程序,用户可以在其中加载一些数据,而不是自己探索它,所以,我现在想知道如何将数据集的 colnames 传递给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

任何 idias 我如何将我的 df 的名称传递给 selectInput?

【问题讨论】:

  • 请看?df(函数来自library(stats)):Density, distribution function, quantile function and random generation for the F distribution。请使用例如DF 改为您的响应式名称。
  • 您需要使用动态 UI。看看uiOutput。示例:shiny.rstudio.com/gallery/dynamic-ui.html

标签: r shiny shiny-reactivity


【解决方案1】:

你可以这样做。

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)

【讨论】:

    猜你喜欢
    • 2022-06-19
    • 2015-03-17
    • 2017-09-10
    • 2021-10-28
    • 2017-07-27
    • 2015-06-15
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多