简体   繁体   English

如何在Shiny中的输入文本框中显示输出文本?

[英]How to display output text inside of input text box in Shiny?

I am creating a shiny app where users can click on one of the cells of the 'renderDT' table to choose the text, and then this chosen text will be added into the text input box -- a function similar to when you type text on your phone, then the phone gives several candidate words for you to choose from. 我正在创建一个闪亮的应用程序,用户可以在其中单击“ renderDT”表的单元格之一来选择文本,然后将所选的文本添加到文本输入框中,该功能类似于您在您的电话,然后电话会提供几个候选词供您选择。 Then you can tap on one of the words, the word will automatically go to your text box, to finish your typing. 然后,您可以点击其中一个单词,该单词将自动转到您的文本框,以完成输入。

I was able to find the example on how to get the clicked cell's value to the output from here . 我能够找到有关如何将点击的单元格的值从此处获取到输出的示例。 Something like this on 'server.R' 在“ server.R”上是这样的

shinyServer(function(input, output,session) {
  output$inputecho <- reactive({
    input$inputtext
  })

  candidates <- reactive({

    candidates <- data.table(
                 candidate=c("word1","word2","word3") 
                 score=c(0.2,0.13,0.12) 
                  ) ### here candidates is a dummy data with same ###structure of real data.

    candidates<-as.data.frame(candidates)
    candidates

  })

output$candidates<-renderDT({candidates()},server=FALSE, selection = list(mode='single',target="cell"))

observeEvent(input$candidates_cell_clicked,{
  info=input$candidates_cell_clicked
  if (is.null(info$value)||info$col!=0)return()
  updateTextInput(session,'inputtext',value=paste0(inputtext,info$value))
})

 }
)

And this for 'ui.R' 这是“ ui.R”的意思

shinyUI(fluidPage(

    mainPanel(

      textInput("inputtext", label="", value=" "),
      p("Click to select from the top candidate words:"),
      DT::dataTableOutput("candidates")
      )
  ))

When I run the app, it seems that the cell can be shown as selected. 当我运行应用程序时,似乎可以将单元格显示为选中状态。 However, the input text inside of my input textbox is not updated. 但是,我的输入文本框中的输入文本未更新。 选择了单元格的应用程序的屏幕截图

Try this. 尝试这个。 The if condition was wrongly coded, and hence updateTextinput was never being called. if条件的编码错误,因此从未调用过updateTextinput。 Also, inputtext needed to be called as input$inputtext , if you want to paste0 with cell value being clicked.Ive left comments inline the places where I made edits. 此外, inputtext需要被称为input$inputtext ,如果你想paste0与单元格的值是clicked.Ive左意见内嵌在我所做出的编辑的地方。 Also, i used data.frame and not data.table , Not that it matters. 另外,我使用data.frame而不是data.table ,这并不重要。

library(shiny)
library(DT)
library(shiny)

ui <- fluidPage(
  mainPanel(
    textInput("inputtext", label="", value=" "),
    p("Click to select from the top candidate words:"),
    DT::dataTableOutput("candidates")
  )
)

server <- function(input, output, session) {
  output$inputecho <- reactive({
    input$inputtext
  })

  candidates <- reactive({

    candidates <- data.frame( # changed from data.table
      candidate=c("word1","word2","word3") , # comma was missing
      score=c(0.2,0.13,0.12) 
    ) ### here candidates is a dummy data with same ###structure of real data.

    candidates<-as.data.frame(candidates)
    candidates

  })

  output$candidates<-renderDT({candidates()},server=FALSE, selection = list(mode='single',target="cell"))

  observeEvent(input$candidates_cell_clicked,{
    info=input$candidates_cell_clicked
    print(info$value)
    if (!(is.null(info$value)||info$col==0))# this was wrongly coded in your example
    {
    updateTextInput(session,'inputtext',value=paste0(input$inputtext,info$value)) # assuming you want to concatenate the two
    # if you want to just update the cell do value=info$value
      }

  })

}


shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM