如何从多个文本框输入中读取字符串并将它们存储在 Shiny 中的向量中?

Posted

技术标签:

【中文标题】如何从多个文本框输入中读取字符串并将它们存储在 Shiny 中的向量中?【英文标题】:How can I read strings from a multiple textboxinputs and store them in a vector in Shiny? 【发布时间】:2021-08-11 23:09:35 【问题描述】:

I have a few text boxes with single characters in them which will be inputted by the user。我一直在试图弄清楚如何将这些值存储在向量中。例如,如果用户在框中输入值 B A C D A,那么我的向量应该看起来像 x

编辑:我已经更新了下面的代码。我没有先更新它,因为它有点长。该代码基本上获得了与数据集中的列一样多的复选框。如果你勾选一个复选框,那么你会得到 2 个文本框输入和下拉菜单

用户界面:

 library(shiny)
library(DT)
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",
                      
                    
                              fillCol(uiOutput('choose_columns')),
                             
                        ## 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)
    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) 
    
  )
  
  #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(
    n <- length(names(data()))
    colnames <- names(data())
    items <- strsplit(input$domains,',')[[1]]
    tagList(
      lapply(1:n, function(i)
       
          
          div(
            div(style="display: inline-block; vertical-align:top; width: 150px ;",checkboxInput(paste0('Columns', i),"", value = TRUE )),
            div(style="display: inline-block; vertical-align:top; width: 150px ;",textInput(paste0('answer_key', i),"",placeholder = 'e.g. A')),
            div(style="display: inline-block; vertical-align:top; width: 150px ;",selectInput(inputId = "domains", label = "", choices =  items)),
            div(style="display: inline-block; vertical-align:top; width: 150px ;",textInput(paste0('valid_options',i),"",placeholder = 'e.g. A,B,C,D'))
          )
          
          
        
    
  )
  

  
      )
    
  )
  
)

【问题讨论】:

我已经更新了 抱歉迟到了。我是堆栈交换的新手,我仍在弄清楚这个网站。感谢您的回复! 【参考方案1】:

要将输入作为向量,您可以在server 代码中使用以下代码。

vec <- sapply(gtools::mixedsort(grep('answer_key',names(input), value = TRUE)), 
                  function(x) input[[x]])

完整的应用代码 -

library(shiny)
library(DT)

ui <- 
  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",    
                      
                      fillCol(uiOutput('choose_columns')),
                      textOutput('text')
                      
                      ## 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


server <- 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) 
    
  )
  
  #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(
    n <- length(names(data()))
    colnames <- names(data())
    items <- strsplit(input$domains,',')[[1]]
    tagList(
      lapply(1:n, function(i)
        
        
        div(
          div(style="display: inline-block; vertical-align:top; width: 150px ;",checkboxInput(paste0('Columns', i),"", value = TRUE )),
          div(style="display: inline-block; vertical-align:top; width: 150px ;",textInput(paste0('answer_key', i),"",placeholder = 'e.g. A')),
          div(style="display: inline-block; vertical-align:top; width: 150px ;",selectInput(inputId = "domains", label = "", choices =  items)),
          div(style="display: inline-block; vertical-align:top; width: 150px ;",textInput(paste0('valid_options',i),"",placeholder = 'e.g. A,B,C,D'))
        )
      )
      
      
      
    )
    
  )
  
  output$text <- renderText(
    vec <- sapply(gtools::mixedsort(grep('answer_key', names(input), value = TRUE)), function(x) input[[x]])
    vec
  )
  


shinyApp(ui, server)

【讨论】:

以上是关于如何从多个文本框输入中读取字符串并将它们存储在 Shiny 中的向量中?的主要内容,如果未能解决你的问题,请参考以下文章

WinAPI GetWindowText 作为字符串

在java中用键盘输入多个字符串,并将它们打印出来

读取单词并将它们存储到数组中

如何从文本框中读取文本并将其中的一些链接?

用JAVA编写一程序:从键盘输入多个字符串到程序中,并将它们按逆序输出在屏幕上。

用JAVA编写一程序:从键盘输入多个字符串到程序中,并将它们按逆序输出在屏幕上。