我正在尝试用R(第一次使用)在R中构建应用程序。闪亮的R选择输入不起作用
步骤1:读取Csv数据 步骤2:每个变量的直方图 步骤3:进一步分析。
我能够读取csv数据,但无法在selectinput中显示数据的colname。
代码正在运行,但变量名称的显示不起作用。 我的代码是:
library(shiny)
ui<-navbarPage("Model Developement by Subhasish",
tabPanel("Data Import",sidebarLayout(sidebarPanel(fileInput("file","Upload your CSV",multiple = FALSE),
tags$hr(),
h5(helpText("Select the read.table parameters below")),
checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
),
mainPanel(uiOutput("tb1"))
)),
tabPanel("Histogram",sidebarLayout(sidebarPanel(
selectInput("headers","Select variable to view Histogram",choices =as.list(names(data)),multiple = FALSE)),mainPanel("mainpanel"))),
tabPanel("More")
)
server<-function(input,output) { data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
})
output$table <- renderTable({
if(is.null(data())){return()}
data()
})
output$tb1 <- renderUI({
tableOutput("table")
})
}
shinyApp(ui=ui,server=server)
感谢您的帮助。
基本上问题是,你正试图从'ui'访问'data'文件的列名,但你不能这样做,除非你从'服务器上通过名字'。你有两个选择:1)在ui中创建'selectInput',然后在服务器中使用'updateSeletctInput',2)在服务器上创建'selectInput',然后使用'renderUI'和'uiOutput'传递它。 –
感谢@JhonPaul的指导。我会记住这一点,将来会使用它。在这种情况下,看起来第二种方式更好,因为它更具动态性。 – Subhasish1315