【问题标题】:Append / remove input text (words) in Shiny without overwriting在 Shiny 中添加/删除输入文本(单词)而不覆盖
【发布时间】:2019-12-22 21:32:52
【问题描述】:

我希望能够在每次单击“添加单词”/“删除单词”按钮时添加或删除新单词。但我希望应用程序保存以前添加/删除的单词。例如,如果我添加 'hello' 作为我的第一个单词,然后添加 'bye',我希望我的单词向量是 ['hello', 'bye']。如果我然后删除'hello',我的向量应该是['bye']

这是我迄今为止能够实现的目标

# Define UI ----------
ui <- fluidPage(

  # Sidebar panel
  sidebarLayout(
    sidebarPanel(

      selectInput('addrm',
                  label = h3('Remove or add words'),
                  choices = list('Remove words' = 1,
                                 'Add words' = 2)),
      textInput('words',
                label = 'You can introduce multiple words separated by comma',
                placeholder = 'Enter words...'),

      uiOutput('button')

    ),

    # Main panel
    mainPanel(

      textOutput('removeWords')
    )
  )
)

# Define server logic required ------------
server <- function(input, output){

  output$button <- renderUI({

    if (input$addrm == 1){
      actionButton('button', label = 'Remove words')
    } else{
      actionButton('button', label = 'Add words')
    }
  })

  stopwords <- c()

  output$removeWords <- renderText({

    input$button
    isolate({ # Only runs when the button is clicked
      stopwords <- unique(c(stopwords, unlist(strsplit(gsub(' ', '', input$words), ',')))) 
    })
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    没关系,我可以在下面的post 中找到我想要的东西

    以防万一有人想要答案:

    
    # Define UI ----------
    ui <- fluidPage(
    
      # Sidebar panel
      sidebarLayout(
        sidebarPanel(
    
          selectInput('addrm',
                      label = h3('Remove or add words'),
                      choices = list('Remove words' = 1,
                                     'Add words' = 2)),
          textInput('words',
                    label = 'You can introduce multiple words separated by comma',
                    placeholder = 'Enter words...'),
          uiOutput('button')
    
        ),
    
        # Main panel
        mainPanel(
    
          textOutput('removeWords')
        )
      )
    )
    
    # Define server logic required ------------
    server <- function(input, output){
    
      output$button <- renderUI({
    
        if (input$addrm == 1){
          actionButton('button', label = 'Remove words')
        } else{
          actionButton('button', label = 'Add words')
        }
      })
    
      values <- reactiveValues()
      values$stopwords <- c()
    
      output$removeWords <- renderText({
    
        input$button
        isolate({ # Only runs when the button is clicked
          values$stopwords <- unique(c(values$stopwords, unlist(strsplit(gsub(' ', '', input$words), ',')))) 
        })
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2020-02-14
      • 1970-01-01
      • 2011-05-08
      • 2019-07-22
      • 2016-08-02
      相关资源
      最近更新 更多