【发布时间】:2018-04-30 14:25:05
【问题描述】:
我正在尝试构建一个闪亮的应用程序,我可以在其中上传 csv 文件并基于列名,来自 selectInput - 应用程序显示基本统计数据(图表、描述性统计平均值、sd、var .... 和一些统计测试)。
应用程序中已有的数据集没有问题。统计也没有问题。
问题是如何使 selectInput 使用新上传的 csv 文件中的列名。
这是我的精简版代码:
用户界面:
library(shiny)
shinyUI(fluidPage(
titlePanel("xxx"),
sidebarLayout(
sidebarPanel(
fileInput("csvFile", "Drag cars.csv over here!"),
selectInput("var", "choose variable:", choices= names(data()))
),
mainPanel(
textOutput("average"),
plotOutput("plot")
)
)
))
服务器:
shinyServer(function(input, output, session) {
data <- reactive({
file1 <- input$csvFile
if (is.null(file1)) {
return()
}
data = read.csv(file=file1$datapath)
data
})
output$average <- renderText({
paste("average is: ", mean(data()[,input$var]))
})
output$plot <- renderPlot({
boxplot(data()[,input$var])
})
})
【问题讨论】:
-
看看
updateSelectInput()。此外,避免将任何名称命名为"data",您可能会受益。尽管它可以工作,但由于名为data()的内置函数,它可能会令人困惑