闪亮的应用程序,它上传一个数据文件并为每一列创建一个复选框和文本输入和下拉列表

Shiny app which Uploads a datafile and create a checkbox and textinput and dropdown list for each column

Here is a video which explains what I want.

我想上传一个文件,然后对于每一列,应该会出现一个复选框。

如果选中该复选框,则应为每列显示一个下拉列表和两个文本输入。

如果未选中,则复选框和两个文本输入应该消失。

这个 image 第一列只有两个文本输入和一个下拉菜单,但每个复选框应该有两个文本输入和下拉菜单。

上传数据文件后查看分析选项卡

UI代码:

shinyUI(
  navbarPage(title="Analysis",
             tabPanel(title="Input",
                      sidebarLayout(
                        sidebarPanel(
                          fileInput("file","Upload the file"),
                          checkboxInput('file_has_headers',"Take Column Names from the first row of the file",value= TRUE),
                          checkboxInput('show_head_only',"Display only first 6 rows. Uncheck this to see entire file",value= TRUE),
                          radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ','),
                          textAreaInput("domains", 'Enter the comma separated list of dimensions, for example: verbal ability, numerical ability' ),
                          width = 4
                        ),
                        mainPanel(
                          wellPanel(
                            DT::dataTableOutput("uploaded_table"
                            ),# Displays the uploaded table by using js dataTable from DT package
                          ),
                          width = 8
                        ),
                        position = 'left'
                      )      
             ), #End of Input Tab panel
             
             tabPanel(title="Verification",
                      fluidRow(
                        column(2,
                               "V",
                               uiOutput('choose_columns')
                        ),
                        column(2,
                               "Key",
                               textInput('anser_key',"",placeholder = 'e.g. A')
                        ),
                        column(4,
                               "Dimension",
                               uiOutput("domain_dropdown",inline = FALSE)
                        ),
                        column(3,
                               "Valid Options",
                               textInput('valid_options',"",placeholder = 'e.g. A,B,C,D')
                        ),
                      ) # End Fluid row    
             ), #End of Verification Tab Panel
             navbarMenu(title="Analayis",
                        tabPanel(title="Item Analysis", "content"
                                 
                        ), #End of Item Analysis Tab Panel
                        tabPanel(title="Test Analysis", "content"
                                 
                        ) #End of Test Analysis Tab Panel
             ) #End of navbarMenu
  ) #End of navbarPage
) #end of shinyUI

服务器代码:

library(shiny)
library(DT)
options(shiny.maxRequestSize=300*1024^2)

shinyServer(function(input, output) {
  
  #1: Get the uploaded file in the data variable 
  data <- reactive({
    uploaded <- input$file
    #if(is.null(file1)){return("No file is selected or selected file is not in the right format. Please check the documentation and upload correct file.")} 
    req(uploaded) #req retruns a silence rather than error and is better than using if()
    if(input$show_head_only){
      head(read.csv(file=uploaded$datapath, sep=input$sep,header = input$file_has_headers)) #head() returns only first 6 rows
    } else {
      read.csv(file=uploaded$datapath, sep=input$sep,header = input$file_has_headers) 
    }
  })
  
  #2:set the elemet for domain dropdown list.
  output$domain_dropdown <- renderUI({
    items <- strsplit(input$domains,',')[[1]] #It creates a list and [[1]] retuns the list as c('','') which is needed for select input
    selectInput(inputId = "domains", label = "", choices =  items)
  })
  
  
  #3: set element to show the uploaded csv file as a table
  output$uploaded_table<- DT::renderDataTable(
    data(), # If a variable contains the output of reactive() function, it must be used as a function.
    server=TRUE, #Important to keep this as true so that large datasets do not crash the browser
    options = list(
      scrollX = TRUE
    ),
  ) # End of uploaded table output setting
  
  #4: Set dynamic checkboxes based on the number of columns in the data
  output$choose_columns <- renderUI({
    req(data())
    colnames <- names(data())
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       # selected = colnames
    )
  })
  
})

也许您正在寻找这个。

ui <- shinyUI(
  navbarPage(title="Analysis",
             tabPanel(title="Input",
                      sidebarLayout(
                        sidebarPanel(
                          fileInput("file","Upload the file"),
                          checkboxInput('file_has_headers',"Take Column Names from the first row of the file",value= TRUE),
                          checkboxInput('show_head_only',"Display only first 6 rows. Uncheck this to see entire file",value= TRUE),
                          radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ','),
                          textAreaInput("domains", 'Enter the comma seperated list of dimensions, for example: verbal ability, numerical ability' ),
                          width = 4
                        ),
                        mainPanel(
                          wellPanel(
                            DT::dataTableOutput("uploaded_table"
                            ),# Displays the uploaded table by using js dataTable from DT package
                          ),
                          width = 8
                        ),
                        position = 'left'
                      )      
             ), #End of Input Tab panel
             
             tabPanel(title="Verification",
                      
                      fillRow(flex = c(1,4),
                              fillCol(uiOutput('choose_columns')),
                              fillCol(fluidRow(column(8,uiOutput('kdv'))))
                              )  ## end of fillRow
                      
             ), #End of Verification Tab Panel
             navbarMenu(title="Analayis",
                        tabPanel(title="Item Analysis", "content"
                                 
                        ), #End of Item Analysis Tab Panel
                        tabPanel(title="Test Analysis", "content"
                                 
                        ) #End of Test Analysis Tab Panel
             ) #End of navbarMenu
  ) #End of navbarPage
) #end of shinyUI

library(shiny)
library(DT)
options(shiny.maxRequestSize=300*1024^2)

server <- shinyServer(function(input, output) {
  
  #1: Get the uploaded file in the data variable 
  data <- reactive({
    uploaded <- input$file
    #if(is.null(file1)){return("No file is selected or selected file is not in the right format. Please check the documentation and upload correct file.")} 
    req(uploaded) #req retruns a silence rather than error and is better than using if()
    if(input$show_head_only){
      head(read.csv(file=uploaded$datapath, sep=input$sep,header = input$file_has_headers)) #head() returns only first 6 rows
    } else {
      read.csv(file=uploaded$datapath, sep=input$sep,header = input$file_has_headers) 
    }
  })
  
  #2:set the element for domain dropdown list.
  output$domain_dropdown <- renderUI({
    req(input$columns)
    items <- strsplit(input$columns,',') # [[1]] #It creates a list and [[1]] returns the list as c('','') which is needed for select input
    selectInput(inputId = "domains", label = "", choices =  items)
  })
  
  
  #3: set element to show the uploaded csv file as a table
  output$uploaded_table<- DT::renderDataTable(
    data(), # If a variable contains the output of reactive() function, it must be used as a function.
    server=TRUE, #Important to keep this as true so that large datasets do not crash the browser
    options = list(
      scrollX = TRUE
    ),
  ) # End of uploaded table output setting
  
  #4: Set dynamic checkboxes based on the number of columns in the data
  output$choose_columns <- renderUI({
    req(data())
    colnames <- names(data())
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       # selected = colnames
    )
  })
  
  output$kdv <- renderUI({
    n <- length(names(data()))
    colnames <- names(data())
    if (is.null(input$columns)){return(NULL)
    }else{
      tagList(
        lapply(1:n, function(i){
          lapply(input$columns , function(par){
            if (colnames[i]==par){
              div(
                div(style="display: inline-block; vertical-align:top; width: 145px ;",textInput(paste0('answer_key',i),"",placeholder = 'e.g. A')),
                div(style="display: inline-block; vertical-align:top; width: 155px ;",selectInput(paste0('sel_var',i), "", choices=data()[[par]])),
                div(style="display: inline-block; vertical-align:top; width: 145px ;",textInput(paste0('valid_options',i),"",placeholder = 'e.g. A,B,C,D'))
              )
            }
          })
        })
      )
    }
  })
  
})

shinyApp(ui, server)