【问题标题】:How to clean a text inside textAreaInput() before some operation?如何在某些操作之前清除 textAreaInput() 中的文本?
【发布时间】:2021-05-19 11:25:47
【问题描述】:

我想在 writeLines 操作后使用函数 updateTextInput 清除 textAreaInput() 内的文本写入,但未成功。在我的例子中:

# Packages
require(shiny)
require(shinythemes)


# Create the shiny dash
ui <- fluidPage(
  theme = shinytheme("cosmo"),
  titlePanel(title="My Map Dashboard"),  
  sidebarLayout(
    sidebarPanel(
      textAreaInput("text_input","Write something"),
      actionButton("erefunction", "Erase")
    ),
    mainPanel(
      textOutput("idSaida")
    )
  )
)
server <- function(input, output){
  
  observeEvent(input$erefunction, {
    
    filePath <- tempfile(fileext = ".txt")
    writeLines(c(input$text_input), filePath)
    updateTextInput(value="")
  })
  
}
shinyApp(ui, server)
##

请提供任何提示或解决方案。提前致谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    updateTextAreaInput代替updateTextInput;您还需要将session 添加到闪亮的server 函数并将其传递给updateTextAreaInput

    这里是修改后的server函数:

    server <- function(input, output, session){
        
        observeEvent(input$erefunction, {
            
            filePath <- tempfile(fileext = ".txt")
            writeLines(c(input$text_input), filePath)
            updateTextAreaInput(session, "text_input", value="")
        })
        
    }
    

    【讨论】:

      【解决方案2】:

      我添加了会话参数和 id,因此 updateSelectInput 知道正在更新什么输入。

      require(shiny)
      require(shinythemes)
      
      
      # Create the shiny dash
      ui <- fluidPage(
          theme = shinytheme("cosmo"),
          titlePanel(title="My Map Dashboard"),  
          sidebarLayout(
              sidebarPanel(
                  textAreaInput("text_input","Write something"),
                  actionButton("erefunction", "Erase")
              ),
              mainPanel(
                  textOutput("idSaida")
              )
          )
      )
      server <- function(input, output, session){
          
          observeEvent(input$erefunction, {
              
              filePath <- tempfile(fileext = ".txt")
              writeLines(c(input$text_input), filePath)
              ##added session and the name of the input to update
              updateTextInput(session, "text_input",  value="")
          })
          
      }
      shinyApp(ui, server)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-17
        • 1970-01-01
        • 2011-06-21
        • 2012-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多