Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 嵌入具有混合数字输入的列,并在DT中选择输入_R_Shiny_Dt - Fatal编程技术网

R 嵌入具有混合数字输入的列,并在DT中选择输入

R 嵌入具有混合数字输入的列,并在DT中选择输入,r,shiny,dt,R,Shiny,Dt,我想在DT中添加一列,该列接受selectInput或NumericiInput,具体取决于变量。 例如,给定以下DF: df1 <- tibble( var1 = sample(letters[1:3],10,replace = T), var2 = runif(10, 0, 2), id=paste0("id",seq(1,10,1)) ) DF=gather(df1, "var", "value", -id

我想在DT中添加一列,该列接受selectInput或NumericiInput,具体取决于变量。 例如,给定以下DF:

df1 <- tibble(
 
  var1 = sample(letters[1:3],10,replace = T),
  var2 = runif(10, 0, 2),
  id=paste0("id",seq(1,10,1))
)

DF=gather(df1, "var", "value", -id)


df1这里是的一个改编版本

使用最新版本的
tidyr
中推荐的
pivot\u longer
代替
collect
。此外,在为新的
选择器
列创建输入时,请检查变量
名称
。如果是
var1
使用
选择输入
,否则使用
数值输入

否则,应该以类似的方式工作

library(shiny)
library(DT)
library(tidyverse)

df1 <- tibble(
  var1 = sample(letters[1:3],10,replace = T),
  var2 = runif(10, 0, 2),
  id=paste0("id",seq(1,10,1))
)

# gather is retired, switch to pivot_longer
DF = pivot_longer(df1, cols = -id, names_to = "name", values_to = "value", values_transform = list(value = as.character))

ui <- fluidPage(
  title = 'selectInput or numericInput column in a table',
  DT::dataTableOutput('foo'),
  verbatimTextOutput('sel')
)

server <- function(input, output, session) {
  for (i in 1:nrow(DF)) {
    if (DF$name[i] == "var1") {
      DF$selector[i] <- as.character(selectInput(paste0("sel", i), "", choices = unique(df1$var1), width = "100px"))
    } else {
      DF$selector[i] <- as.character(numericInput(paste0("sel", i), "", NULL, width = "100px"))
    }
  }
  output$foo = DT::renderDataTable(
    DF, escape = FALSE, selection = 'none', server = FALSE,
    options = list(dom = 't', paging = FALSE, ordering = FALSE),
    callback = JS("table.rows().every(function(i, tab, row) {
        var $this = $(this.node());
        $this.attr('id', this.data()[0]);
        $this.addClass('shiny-input-container');
      });
      Shiny.unbindAll(table.table().node());
      Shiny.bindAll(table.table().node());")
  )
  output$sel = renderPrint({
    str(sapply(1:nrow(DF), function(i) input[[paste0("sel", i)]]))
  })
}

shinyApp(ui, server)
库(闪亮)
图书馆(DT)
图书馆(tidyverse)
df1