我正在尝试使用R Shiny创建一个APP。我想上传数据(.csv文件)。然后我想在下拉菜单中填充CSV文件中的列名。我无法做到这一点。
请参阅以下代码:
---- server.r -----
library(shiny)
options(shiny.maxRequestSize = 32*1024^2)
shinyServer(
function(input, output){
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath,head=TRUE,sep=",")
})
output$sum <- renderTable({
if(is.null(data())){return ()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return ()}
data()
})
# the following renderUI is used to dynamically generate the tabsets when the file is loaded. Until the file is loaded, app will not show the tabset.
output$tb <- renderUI({
if(is.null(data()))
h5("no file loaded")
else
tabsetPanel(tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
output$col <- renderUI({
selectInput("phenomena", "Select the Phenomena", names(data))
})
})
----- ui.R -----
library(shiny)
shinyUI(fluidPage(
titlePanel("Hotspot Analysis of EnviroCar Data"),
sidebarLayout(
sidebarPanel(
# uploading the file
fileInput("file","Upload *.csv file"), # fileinput() function is used to get the file upload contorl option
uiOutput("col")
),
mainPanel( uiOutput("tb") )
)
))
答案 0 :(得分:4)
我想问题出在server.R
:
selectInput("phenomena", "Select the Phenomena", names(data))
在这里,您使用data
而没有括号,因此您实际获得的是函数data
的源代码,而names(data)
是NULL
。我认为您只需要names(data)
替换names(data())
。