我正在创建一个闪亮的应用程序,用户可以在其中单击“ renderDT”表的一个单元格以选择文本,然后将此选定的文本添加到文本输入框中,该功能类似于您在手机上输入文字,然后手机会提供几个候选字词供您选择。然后,您可以点击其中一个单词,该单词将自动转到您的文本框,以完成输入。
我能够找到有关如何将点击的单元格的值获取到here的输出的示例。在“ 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))
})
}
)
这是'ui.R'
shinyUI(fluidPage(
mainPanel(
textInput("inputtext", label="", value=" "),
p("Click to select from the top candidate words:"),
DT::dataTableOutput("candidates")
)
))
答案 0 :(得分:1)
尝试一下。 if条件的编码错误,因此从未调用过updateTextinput。另外,如果要单击单元格值来inputtext
,则input$inputtext
的名称应为paste0
。我已在进行编辑的地方插入了注释。另外,我使用的是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)