【问题标题】:Capturing actionButton value and updating textInput accordingly in Shiny App在 Shiny App 中捕获动作按钮值并相应地更新文本输入
【发布时间】:2017-07-25 11:47:52
【问题描述】:

希望标题是不言自明的。下面是重现问题的简单示例。我想要的是用于标记按钮的字母 (a-z),一旦单击按钮,就将其添加到应用程序的 textInput 框中。

请注意,以某种方式从字母向量 (p1) 中提取字母值是行不通的,因为我的真实应用程序有几乎无限数量的单词可以标记按钮。我需要获取传递给按钮标签 (value1) 的实际值。

现在,我得到的是(HTML?)代码。

请注意,我已经尝试过:

Capture label of actionButton in Shiny app

Capture the label of an actionButton once it is clicked

虽然他们帮助我靠近,但并没有解决当前的问题。

library(shiny)
shinyUI(fluidPage(
# Application title
    titlePanel("Letter Predictor"),
# Sidebar with action button
    sidebarLayout(
        sidebarPanel(
            actionButton("word1", label = textOutput("value1"))
        ),
# Text entry box
        mainPanel(
            textInput(inputId = "text", label = h3("Text input"), value = "Enter a number (1-26)"),
        hr()
            )
        )
    )
)

shinyServer(function(input, output, session) {
# Letter prediction function to be used in Shiny Application
    predictalg <- function(userinput, predictor, predicted){
            pred <- predicted[which(predictor == userinput)]
            return(pred)
    }
# Vectors for making predictions
    p1 <- letters
    p2 <- seq_along(letters)
# Prediction function applied
    value1 <- reactive({
            predictalg(userinput = { trimws(input$text) },
                       predictor = p2,
                       predicted = p1)
    })
    output$value1 <- value1
# Register button click and (ideally) pass label (value1) to updateTextInput
    observeEvent(input$word1, {
        name <- paste(input$text, textOutput("value1"))#PROBLEM WITH value1
            updateTextInput(session, "text", value = name)
    })
})

我是一个有光泽的新手,我被困住了。任何帮助将不胜感激!

【问题讨论】:

  • 您会得到 HTML 代码,因为这就是 textOutput("value1") 的含义。如果您进行以下更改:name &lt;- paste(input$text, value1() ) 您将(可能)得到您想要的。
  • 这似乎行得通。谢谢!

标签: r shiny


【解决方案1】:

每米哈尔·马伊卡:

你会得到 HTML 代码,因为这就是

textOutput("value1")

是。如果您进行以下更改:

name <- paste(input$text, value1() ) 

你会(可能)得到你想要的。

value1 后面的括号“()”是键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 2016-08-14
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2011-09-20
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多