【问题标题】:How to change data.frame values that depend on a numericInput如何更改依赖于 numericInput 的 data.frame 值
【发布时间】:2017-07-14 01:56:51
【问题描述】:

我不是闪亮的专业人士,当numericInput 用于结果进入matrix/data.frame 的函数时,我正在努力使data.frame 中的数据发生变化。 这是我的问题的一个玩具示例:

ui.R
library(DT)
library(shiny)
shinyUI(fluidPage(
  fluidRow(
           column(2,numericInput(inputId = "precentil",label = "percentile", 
                                 value = 0.9, min = 0.01, max=1, step = 0.01)),
           column(6,dataTableOutput("matResult")))
))


server.R
library(DT)
library(shiny)
shinyServer(function(input, output) {

  data =  rnorm(1000)
  percentile = reactive({input$percentil})
  quant = reactive({quantile(data,percentile())})
  result = as.data.frame(c("quantile", quant))
  output$matResult =  DT::renderDataTable(DT::datatable(result,
                            options = list(paging = FALSE),rownames=F))

})

我得到的错误是:Warning: Error in as.data.frame.default: cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame 我明白,但我搜索了很多,但没有找到解决这个问题的方法。

【问题讨论】:

    标签: r datatable shiny


    【解决方案1】:

    如果您希望您的 data.frame 更新,那么您需要使其成为响应式并使其保持响应式。并且输入值已经是反应性的。好像你不是在摸索反应式编程。我建议您查看 Shiny 开发者大会上的 Effective Reactive Programming videos。此外,这应该有效。

    data <- rnorm(1000)
    ui <- fluidPage(
      fluidRow(
      column(2,numericInput(inputId = "percentile",label = "percentile", 
                                 value = 0.9, min = 0.01, max=1, step = 0.01)),
      column(6,dataTableOutput("matResult")))
    )
    server <- function(input, output) {
      result <- reactive({
        data.frame("quantile" = quantile(data, as.numeric(input$percentile) ))
      })
      output$matResult =  DT::renderDataTable(DT::datatable(result(),
                                options = list(paging = FALSE), rownames=F))
    }
    shinyApp(ui=ui, server=server)
    

    【讨论】:

    • 感谢它的工作,并感谢教程的链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    相关资源
    最近更新 更多