【问题标题】:How can I take an Input in Shiny and fill a table with it?如何在 Shiny 中输入输入并用它填充表格?
【发布时间】:2016-05-17 10:23:51
【问题描述】:

我正在尝试从 Shiny 应用程序中的各种滑块中获取输入并将它们合并到一个表格中,但是,我不断收到以下错误:

Error in UseMethod("xtable") : 
no applicable method for 'xtable' applied to an object of class "c('double', numeric')"


Error in UseMethod("xtable") : 
no applicable method for 'xtable' applied to an object of class "reactive"


Error in as.vector(x, mode) : 
cannot coerce type 'closure' to vector of type 'any'

我尝试了很多想法,但仍然无法实现。我也没有找到任何好的例子或解释作为指导。

我只需要一些简单的东西,用滑块的结果填充表格,然后能够通过renderTable() 打印表格。

我拥有的从滑块捕获输入的代码如下:

sliderInput(inputId = "a.w1.a", label = "Investment on advocacy", min = 0, max = 1000000, step = 50000, value = 0),

sliderInput(inputId = "a.w1.b", label = "Investment on B", min = 0, max = 1000000, step = 50000, value = 0)

由此,我能够创建一个反应性对象,然后让我能够成功绘制:

output$fund.a.w1.alloc <- reactive(input$a.w1.a + input$a.w1.b + input$a.w1.c + input$a.w1.d)

output$fund.a.w1.p <- renderPlot(barplot(d.funds.a.w1(), main = "Expenditure strategy (1000's USD)", col = "blue"))

但是,当我尝试创建具有相同内容的表格或矩阵时,我似乎没有得到正确的答案。我尝试过类似的东西:

fund.w1 <- matrix(c(reactive(input$a.w1.a/1000, input$a.w1.b/1000, input$a.w1.c/1000, input$a.w1.d/1000)), nrow = 2, ncol = 2)

或者这个:

fund.w1 <- reactive(matrix(c(input$a.w1.a/1000, input$a.w1.b/1000, input$a.w1.c/1000, input$a.w1.d/1000), nrow = 2, ncol = 2))

或者这个:

fund.w1 <- matrix(reactive(c(input$a.w1.a/1000, input$a.w1.b/1000, input$a.w1.c/1000, input$a.w1.d/1000)), nrow = 2, ncol = 2)

我什至试着看看数据对象是否可以自己组成一个表,但它不起作用。

事实上,它甚至没有像这样简单的事情:

output$fund.w1.f <- renderTable({input$a.w1.a/1000})

或者作为(之前创建过矩阵):

fund.w1.f.test[1,1] <- renderText(input$a.w1.a)

非常感谢您的帮助。

【问题讨论】:

  • 你必须在你的帖子中包含你的代码,否则真的很难提供帮助!
  • 这就是问题所在。我没有太多关于那部分的代码,因为我找不到任何有用的东西。我会将我以前的一些尝试添加到原始问题中,并查看它可以帮助您帮助我。非常感谢您的处置!我真的很感激。
  • 嗨,@MLavoie,刚刚进行了更改以试图澄清问题。谢谢!

标签: r user-input shiny-server shiny


【解决方案1】:

我想我找到了如何让它工作(抱歉,如果这太基本了,伙计们,我对 Shiny 还很陌生,但仍然掌握了一些东西)。

我发现了一个示例,在构建数据reactive 对象后,我推断出这一点:

d.funds.c.w1 <- reactive(c(input$c.w1.c/1000, input$c.w1.a/1000, input$c.w1.b/1000)

然后,要将其放入表中,您需要使用renderTable(),如下所示:

output$fund.w1.f <- renderTable({
  d <- data.frame(x = rbind(t(o.funds.a.w1()), t(o.funds.b.w1()), t(o.funds.c.w1()), t(o.funds.d.w1())))
  d
})

另一种解决方法是 an answer I just got 来自 Google Shiny 论坛组中一个名叫 Ben Newberg 的人,我在那里发布了同样的问题:

Turn the slider input values into a data frame first.

sliderInput(inputId = "a.w1.a", label = "Investment on advocacy", min = 0, max = 1000000, step = 50000, value = 0),
sliderInput(inputId = "a.w1.b", label = "Investment on B", min = 0, max = 1000000, step = 50000, value = 0),
tableOutput("tbl")


output$tbl <- renderTable({
   df1 <- data.frame(a = c(input$a.w1.a), b = c(input$a.w1.b))
   df1
})

我希望这对可能需要同样指导的任何人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 2018-03-08
    • 2015-01-26
    • 1970-01-01
    • 2020-05-14
    相关资源
    最近更新 更多