闪亮的 R -Select 输入无效

Shiny R -Select input is Not working

我正在尝试使用 Shiny(第一次使用)在 R 中构建应用程序。

第 1 步:读取 Csv 数据 第 2 步:每个变量的直方图 第 3 步:进一步分析。

我可以读取 csv 数据,但无法在 selectinput 中显示数据的列名。

代码是运行但是变量名的显示不工作。 我的代码是:

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)

在此先感谢您的帮助。

我在这里添加了一个示例,您可以如何继续。我用我的虚拟 .csv 文件测试了它,所以它应该可以工作。我还为直方图添加了一些测试用例,这样您就不会打印错误。

我添加了以下更改

  1. 我使用 renderUI 创建了名为 headers
  2. selectInput
  3. table输出最好直接使用tableOutput("table")
  4. 渲染
  5. 我添加了你想要的直方图

示例:

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(tableOutput("table")))),
               tabPanel("Histogram",sidebarLayout(sidebarPanel(
                 uiOutput("ui1")),
                 mainPanel(plotOutput("myhist")))),
               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$ui1 <- renderUI({
    selectInput("headers","Select variable to view Histogram",choices =as.list(names(data())),multiple = FALSE)
  })

  output$myhist <- renderPlot({
    histdata <- suppressWarnings(as.numeric((data()[,names(data()) %in% input$headers])))
    histdata <- histdata[!is.na(histdata)]
    if(length(histdata) == 0){
      return()
    }
    hist(histdata,breaks=10)
    box();grid() 
  })
}
shinyApp(ui=ui,server=server)