我想制作一个应用程序,用户可以在其中加载一些数据,而不是自己浏览,因此,我现在想将数据集的名称传递给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?
答案 0 :(得分: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)