【问题标题】:How to allow selectizeInput to allow user to enter new entry as well as load some old ones如何允许 selectizeInput 允许用户输入新条目以及加载一些旧条目
【发布时间】:2020-01-11 17:37:18
【问题描述】:

我正在尝试使用持久数据存储来允许用户将先前输入的文本数据位加载到文本框中。我还希望用户能够输入一些新数据并进行存储。

该应用程序可以运行,但它不允许用户在文本框中输入新数据。我尝试使用 selectizeInput 和选项 create=TRUE 但没有成功。这是我的代码:

library(shiny)
library(here)


outputDir <- here("responses")

saveData <- function(data) {
  data <- t(data)
  # Create a unique file name
  fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))
  # Write the file to the local system
  write.csv(
    x = data,
    file = file.path(outputDir, fileName), 
    row.names = FALSE, quote = TRUE
  )
}

loadData <- function() {
  # Read all the files into a list
  files <- list.files(outputDir, full.names = TRUE)
  data <- lapply(files, read.csv, stringsAsFactors = FALSE) 
  # Concatenate all data together into one data.frame
  data <- do.call(rbind, data)
  data
}

# Define the fields we want to save from the form
fields <- c("name", "used_shiny", "r_num_years")

# Shiny app with 3 fields that the user can submit data for
shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("responses", width = 300), tags$hr(),
    selectizeInput("MyChoices","Select from ui delimiters", choices=
                     loadData()[,1],options = list(create = TRUE)),
    checkboxInput("used_shiny", "I've built a Shiny app in R before", FALSE),
    sliderInput("r_num_years", "Number of years using R",
                0, 25, 2, ticks = FALSE),
    actionButton("submit", "Submit")
  ),
  server = function(input, output, session) {

    # Whenever a field is filled, aggregate all form data
    formData <- reactive({
      data <- sapply(fields, function(x) input[[x]])
      data
    })

    # When the Submit button is clicked, save the form data
    observeEvent(input$submit, {
      saveData(formData())
    })

    # Show the previous responses
    # (update with current response when Submit is clicked)
    output$responses <- DT::renderDataTable({
      input$submit
      loadData()
    }) 


  }
)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    啊哈,这是一个简单的答案。如果要加载存储的条目并允许输入新条目,则必须允许多个 =TRUE。需要在 ui 中添加以下内容:

    selectizeInput("name", "Name", "",multiple = TRUE,options = list(create = TRUE),choices=loadData()[,1])
    

    【讨论】:

    • 不幸的是只有两天时间@Elin
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2011-04-21
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多