R 每次按下按钮后,闪亮的初始文本区域输入值和反应 描述

R 每次按下按钮后,闪亮的初始文本区域输入值和反应 描述,r,shiny,R,Shiny,我有一个textAreaInput框,希望以默认值开头。用户可以单击2个操作按钮(提交和随机评论)。提交更新textAreaInput中的注释以供进一步处理(绘图等),而随机注释向textAreaInput发送一个新的随机值(用户也可以在textAreaInput框中键入)。我几乎拥有了它,但在按下提交按钮之前,无法让应用程序更新textAreaInput的值 问题: 我想它被更新时,随机评论被按下,但仍然允许用户删除文本框,并键入自己的文本。我如何让应用程序做到这一点 MWE 库(闪亮) 图

我有一个
textAreaInput
框,希望以默认值开头。用户可以单击2个操作按钮(提交和随机评论)。提交更新
textAreaInput
中的注释以供进一步处理(绘图等),而随机注释向
textAreaInput
发送一个新的随机值(用户也可以在
textAreaInput
框中键入)。我几乎拥有了它,但在按下提交按钮之前,无法让应用程序更新
textAreaInput

问题: 我想它被更新时,随机评论被按下,但仍然允许用户删除文本框,并键入自己的文本。我如何让应用程序做到这一点

MWE
库(闪亮)
图书馆(shinyjs)
图书馆(stringi)
shinyApp(
ui=fluidPage(
第(2)栏,
uiOutput(“randcomment”),
br(),
div(
actionButton(“随机文本”,“随机评论”,图标=图标(“右引号”),
div(actionButton(“提交”、“提交”,icon=icon(“刷新”)),style=“float:right”)
)
),
列(4,div(逐字逐句输出(“commenttext”),样式为“页边距顶部:2cm;”)
),
服务器=功能(输入、输出){

输出$randcomment我的方法有点不同。我将使用
reactiveValues
填充这两个字段,然后使用两个
observeEvents
控制
reactiveValues
的内容

library(shiny)
library(shinyjs)
library(stringi)

shinyApp( 
  ui = fluidPage(
    column(2,
           uiOutput("randcomment"),
           br(),
           div(
             actionButton("randtext", "Random Comment", icon = icon("quote-right")),
             div(actionButton("submit", "Submit", icon = icon("refresh")), style="float:right")
           )

    ),
    column(4, div(verbatimTextOutput("commenttext"), style = 'margin-top: 2cm;'))
  ),
  server = function(input, output) {

    # Reactive lists -------------------------------------------------------
    # setting the initial value of each to the same value.
    initial_string <- stri_rand_lipsum(1)
    comment_value <- reactiveValues(comment = initial_string,
                                    submit = initial_string)

    # Event observers ----------------------------------------------------
    observeEvent(input$randtext,
      {
        comment_value$comment <- stri_rand_lipsum(1)
      }
    )

    # This prevents the comment_value$submit from changing until the 
    # Submit button is clicked. It changes to the value of the input
    # box, which is updated to a random value when the Random Comment
    # button is clicked.
    observeEvent(input$submit,
      {
        comment_value$submit <- input$comment
      }
    )

    # Output Components -------------------------------------------------
    # Generate the textAreaInput
    output$randcomment <- renderUI({
      textAreaInput("comment", 
                    label = h3("Enter Course Comment"), 
                    value = comment_value$comment, 
                    height = '300px', 
                    width = '300px')
    })

    # Generate the submitted text display
    output$commenttext <- 
      renderText({ 
        comment_value$submit 
      })
  }
)
我认为在这种情况下,您根本不需要
反应式
反应式
在需要立即处理时很好。如果您想控制处理值的时间,请使用
反应式值

library(shiny)
library(shinyjs)
library(stringi)

shinyApp( 
  ui = fluidPage(
    column(2,
           uiOutput("randcomment"),
           br(),
           div(
             actionButton("randtext", "Random Comment", icon = icon("quote-right")),
             div(actionButton("submit", "Submit", icon = icon("refresh")), style="float:right")
           )

    ),
    column(4, div(verbatimTextOutput("commenttext"), style = 'margin-top: 2cm;'))
  ),
  server = function(input, output) {

    # Reactive lists -------------------------------------------------------
    # setting the initial value of each to the same value.
    initial_string <- stri_rand_lipsum(1)
    comment_value <- reactiveValues(comment = initial_string,
                                    submit = initial_string)

    # Event observers ----------------------------------------------------
    observeEvent(input$randtext,
      {
        comment_value$comment <- stri_rand_lipsum(1)
      }
    )

    # This prevents the comment_value$submit from changing until the 
    # Submit button is clicked. It changes to the value of the input
    # box, which is updated to a random value when the Random Comment
    # button is clicked.
    observeEvent(input$submit,
      {
        comment_value$submit <- input$comment
      }
    )

    # Output Components -------------------------------------------------
    # Generate the textAreaInput
    output$randcomment <- renderUI({
      textAreaInput("comment", 
                    label = h3("Enter Course Comment"), 
                    value = comment_value$comment, 
                    height = '300px', 
                    width = '300px')
    })

    # Generate the submitted text display
    output$commenttext <- 
      renderText({ 
        comment_value$submit 
      })
  }
)
库(闪亮)
图书馆(shinyjs)
图书馆(stringi)
新亚普(
ui=fluidPage(
第(2)栏,
uiOutput(“randcomment”),
br(),
div(
actionButton(“随机文本”,“随机评论”,图标=图标(“右引号”),
div(actionButton(“提交”、“提交”,icon=icon(“刷新”)),style=“float:right”)
)
),
列(4,div(逐字逐句输出(“commenttext”),样式为“页边距顶部:2cm;”)
),
服务器=功能(输入、输出){
#反应列表-------------------------------------------------------
#将每个的初始值设置为相同的值。

这是我收到的最好的答案之一。谢谢你花时间写它。建设性的反馈真的很有帮助。谢谢你花时间发表。我很高兴你建设性地接受了它。在看了你的声誉之后,我几乎没有包括批评。