首页 > 解决方案 > 为 DT 输出渲染文本输入时出现 R 闪亮错误

问题描述

在我闪亮的应用程序中为 DT 渲染文本输入时,我遇到了一些非常奇怪的问题。textinput 存储一些值,用作应用程序的设置之一。这些设置存储在一个反应​​列表中,一个列表成员是一个 data.table - 存储所有渲染设置。

我做了一个可重现的例子,而不是代码片段。不漂亮,但它说明了。

library(shiny)

rv = reactiveValues()


ui <- shinyUI(fluidPage(

      textInput(inputId = "textinput", label = h3("Numeric input"), value = "some value"),
      tags$hr(),
      fluidRow(column(3, verbatimTextOutput("textinput"))),
      tags$hr(),
      tabPanel("Settings table for viewing", dataTableOutput('settings_table')),

))

server <- function(input, output, session) {
  observe({

    # lst_names = list()
    # lst_values = list()


    rv$textinput <-  renderText( input$textinput )
    output$textinput <- renderText({ input$textinput }) # this is displayed nicely

    # lst_names = c(lst_names, "rv$textinput") 
    # lst_values = c(lst_values, rv$textinput)        


    rv$settings = data.table(Var_names = "rv$textinput", Var_values = rv$textinput)

    })

  output$settings_table = DT::renderDataTable(options = list(pageLength = 50), {
    rv$settings
  })
}

shinyApp(ui, server)

我在 DT 的 github 中找到了这段文本,但我无法找到让它工作的方法。numericinput、rendertext、renderprint、将 rendertext/print 移出“观察”块也有同样的问题。

有什么建议么?

标签: rshinydttextinput

解决方案


你不使用好的dataTableOutput。您必须使用DT::dataTableOutput或,等效地,DT::DTOutput.

library(shiny)
library(DT)

ui <- fluidPage(

  textInput(inputId = "textinput", label = h3("Numeric input"), 
            value = "some value"),
  tags$hr(),
  fluidRow(column(3, verbatimTextOutput("textinput"))),
  tags$hr(),
  tabPanel("Settings table for viewing", DTOutput('settings_table')),

)

server <- function(input, output, session) {

  output$textinput <- renderText({ input$textinput }) 

  rv <- reactiveValues()

  observe({

    rv$textinput <- input$textinput

    rv$settings <- data.frame(
      Var_names = "rv$textinput", 
      Var_values = rv$textinput
    )

  })

  output$settings_table <- renderDT(options = list(pageLength = 50), {
    rv$settings
  })

}

shinyApp(ui, server)

推荐阅读