带数字输入和反应性列的R闪亮数据表

带数字输入和反应性列的R闪亮数据表

本文介绍了带数字输入和反应性列的R闪亮数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个闪亮的数据表,其中一列用于数字输入,另一列对输入有反应。这类似于在单元格中嵌入方程式时可以在excel中执行的操作。我在下面以虹膜为例,并尝试计算花瓣面积。因此,果皮面积等于花瓣长度×花瓣宽度/调整。用户将输入Adjust参数来调整区域。在这里提出了类似的问题,但没有直接回答。

I am building a shiny datatable with one column for numericinput and the other reactive to the input. It is similar to what you can do in excel with equation embedded in cells. I am giving iris as example below and trying to calculate petal area. So peal area equals Petal.Length*Petal.Width/Adjust. The adjust parameter will be typed in by user to adjust area. A similar question was asked here but with no direct answer.Insert a numeric input for each row - R Shiny

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

ui <- basicPage(

  DT::dataTableOutput('x1')
)

server <- function(input, output, session) {
  # create a character vector of shiny inputs
  shinyInput = function(FUN, len, id, ...) {
    inputs = character(len)
    for (i in seq_len(len)) {
      inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
    }
    inputs
  }

  # obtain the values of inputs
  shinyValue = function(id, len) {
    unlist(lapply(seq_len(len), function(i) {
      value = input[[paste0(id, i)]]
      if (is.null(value)) 1 else value
    }))
  }

  # a sample data frame
  DF <- reactive(
    iris <- iris %>%
      mutate(
        adjust = shinyInput(numericInput, nrow(iris), 'adj', value = 1),
        Petal_Area = Petal.Length*Petal.Width/shinyValue('adj', nrow(iris))
      )
  )

  # render the table containing shiny inputs
  output$x1 = DT::renderDataTable(

    # use slider to control number of rows shown in table
    DF(), server = FALSE, escape = FALSE, options = list(
      preDrawCallback = JS('function() {
                           Shiny.unbindAll(this.api().table().node()); }'),
      drawCallback = JS('function() {
                        Shiny.bindAll(this.api().table().node()); } ')
      )
      )
}

shinyApp(ui, server)


推荐答案

这是不完整的答案。希望其他用户可以对此进行改进。如果没有,那么这是最好的答案。

This is an incomplete answer. In the hope that another user may be able to improve upon it. If not then this is the best possible answer.

我现在尝试了大约半小时,但是由于某种原因(也许是因为 DT 不能使用像Excel),我无法在 DT 表中获得计算结果。但是如果我不将结果显示在 DT 表中,那么反应性和计算就会起作用!

I tried now for about half an hour but for some reason (maybe cause DT is not meant to be used like Excel), I can't get the result of the calculation in the DT-table. However reactivity and calculation work if I do not display the result in the DT-table!

我只做了两个更改:


  1. 我添加了调试表

  2. 我替换了您的 dplyr -代码并删除了计算

  1. I added a debugging table
  2. I replaced your dplyr-code and removed the calculation

代码如下:

library(shiny)
library(DT)

ui <- basicPage(
  DT::dataTableOutput('x1'),
  tableOutput('debug')
)

server <- function(input, output, session) {
  # create a character vector of shiny inputs
  shinyInput = function(FUN, len, id, ...) {
    inputs = character(len)
    for (i in seq_len(len)) {
      inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
    }
    inputs
  }

  # obtain the values of inputs
  shinyValue = function(id, len) {
    unlist(lapply(seq_len(len), function(i) {
      value = input[[paste0(id, i)]]
      if (is.null(value)) 1 else value
    }))
  }

  # a sample data frame
  DF <- reactive({
    iris_adj <- iris
    iris_adj$adjust<-shinyInput(numericInput, nrow(iris), 'adj', value = 1)
    # iris_adj$Petal_area<-iris$Petal.Length*iris$Petal.Width/shinyValue('adj',nrow(iris))
    return(iris_adj)
  })


  # render the table containing shiny inputs
  output$x1 <- DT::renderDataTable(

    # use slider to control number of rows shown in table
    DF(), server = FALSE, escape = FALSE, options = list(
      preDrawCallback = JS('function() {
                           Shiny.unbindAll(this.api().table().node()); }'),
      drawCallback = JS('function() {
                        Shiny.bindAll(this.api().table().node()); } ')
      )
      )
  output$debug<-renderTable({
    cbind(DF()[,-6],shinyValue('adj',nrow(iris)),Petal_Area=iris$Petal.Length*iris$Petal.Width/shinyValue('adj',nrow(iris)))
  })
      }

shinyApp(ui, server)

我一添加计算,就没有找到 DT 的输出它进入调试表的方式。

As soon as I add the calculation, output from the DT does not find it's way into the debugging table.

我个人记得:我使用过 shinyInput shinyValue 把戏大约半年前。我还记得那是非常非常不稳定的。 我不会推荐给任何想要开发稳定应用程序的人

On a personal note: I remember using this shinyInput and shinyValue trick about half a year ago. I also remember that it was very very unstable. I would not recommend it to anyone who wants to develop a stable app

我还记得还有另一个R-package用于完整的交互式表格。它称为 rhandsontables 。也许尝试:

I also remember that there was another R-package for complete interactive tables. It is called rhandsontables. Maybe try that: https://jrowen.github.io/rhandsontable/

这篇关于带数字输入和反应性列的R闪亮数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:16