无法在 R shiny 中动态填充下拉菜单
Cannot populate drop down menu dynamically in R shiny
我正在尝试使用 R Shiny 创建一个应用程序。我想上传数据(.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") )
)
))
我猜问题出在server.R
:
selectInput("phenomena", "Select the Phenomena", names(data))
在这里,你使用的是没有括号的data
,所以你实际得到的是函数data
的源代码,而names(data)
是NULL
。我认为您只需要将 names(data)
替换为 names(data())
.
我正在尝试使用 R Shiny 创建一个应用程序。我想上传数据(.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") )
)
))
我猜问题出在server.R
:
selectInput("phenomena", "Select the Phenomena", names(data))
在这里,你使用的是没有括号的data
,所以你实际得到的是函数data
的源代码,而names(data)
是NULL
。我认为您只需要将 names(data)
替换为 names(data())
.