闪亮的初始textAreaInput值,每按一次按钮即可响应

Tyl*_*ker 1 r shiny

描述

我有一个textAreaInput要从默认值开始的框。用户可以单击2 actionButtonsSubmitRandom Comment)。 Submit更新来自的注释以textAreaInput进行进一步处理(绘图等),同时Random Comment将新的随机值发送给textAreaInput(用户也可以在textAreaInput框中键入内容)。我几乎有,但不能得到应用更新textAreaInputvalue,直到Submit按下按钮。

我希望在Random Comment按下该按钮时进行更新,但仍允许用户删除文本框并键入自己的文本。如何使该应用程序执行此操作?

在此处输入图片说明

微机

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) {

        output$randcomment <- renderUI({
            commentUi()
        })

        comment_value <- reactiveValues(default = 0)

        observeEvent(input$submit,{
            comment_value$default <- input$randtext
        })

        renderText(input$randtext)

        commentUi <- reactive({

            if (comment_value$default == 0) {
                com <- stri_rand_lipsum(1)
            } else {
                com <- stri_rand_lipsum(1)
            }

            textAreaInput("comment", label = h3("Enter Course Comment"), 
                value = com, height = '300px', width = '300px')
        })

        output$commenttext <- renderText({ input$comment })


    }
  )
Run Code Online (Sandbox Code Playgroud)

Ben*_*min 5

我会对此有所不同。我将使用reactiveValues填充两个字段,然后使用两个observeEvents来控制的内容reactiveValues

reactive在这种情况下,我认为您根本不需要。 reactive需要立即处理时很好。如果要控制何时处理该值,请使用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 
      })
  }
)
Run Code Online (Sandbox Code Playgroud)

对您的代码的一些注释

我在确定您的代码在做什么方面有些挣扎。部分原因是您的服务器功能有些混乱。您的组件是

  • 输出
  • 反应式清单
  • 观察者
  • 输出(但未分配给插槽...多余)
  • 反应物体
  • 输出

我建议将您的反应堆,观察员和输出一起分组。如果您有真正分开的系统,则可以将系统分为不同的代码部分,但要使它们遵循类似的模式(我会宣称这两个方框是同一系统的一部分)

你的commentUi反应堆有一个奇怪的if-else结构。它始终设置com为随机字符串。而且,if-else构造实际上不是必需的,因为您永远不会在代码中更新任何位置,comment_value$default它始终为0。看起来您可能曾经在某个时候以动作按钮为基础,然后得出结论。 (正确)那不是一个好选择。

另外,我建议不要在反应对象中构建UI组件。如果反应式返回值然后构建render函数族中的任何UI组件,您会发现它们更加灵活和有用。