R 在空TIBLE中,列类型根据第一行的添加方式而变化

R 在空TIBLE中,列类型根据第一行的添加方式而变化,r,dplyr,shiny,R,Dplyr,Shiny,我正在开发一个闪亮的应用程序,其中一部分需要编辑(更改现有值,添加新行)到各种TIBLES。需要编辑许多TIBLE,每个TIBLE都有不同的列名和数据类型。我想使用通用编辑器。每个tibble开始时都是空的 我用零行和键入的列创建TIBLE。这样行 makeTibble <- function() { tibble(W=character(), X=numeric(), Y=logical(), Z=structure(NA_real_, class = "Date")) } v &l

我正在开发一个闪亮的应用程序,其中一部分需要编辑(更改现有值,添加新行)到各种TIBLES。需要编辑许多TIBLE,每个TIBLE都有不同的列名和数据类型。我想使用通用编辑器。每个tibble开始时都是空的

我用零行和键入的列创建TIBLE。这样行

makeTibble <- function() {
  tibble(W=character(), X=numeric(), Y=logical(), Z=structure(NA_real_, class = "Date"))
}
v <- reactiveValues(data=makeTibble())

提前感谢您的帮助。

在这里,您可以连接到原子向量中:

newValues <- c("W"="Testing...", "X"=pi, "Y"=TRUE, "Z"=as.Date("2020-04-29"))
改为使用列表:

newValues <- list("W"="Testing...", "X"=pi, "Y"=TRUE, "Z"=as.Date("2020-04-29"))

newValues Stephane,谢谢!就我而言,这是一个“树为林”的时刻!您的建议解决了我使用
addLoop
选项时的问题[这为我提供了一个可以在应用程序中使用的解决方案],但是addTypedLoop仍然将最后一列从
Date
转换为
double
。那里发生了什么事?
asTypedValue <- function(value, type) {
  print(paste0("type: ", type, "; value: ", value))
  switch(type, 
         "character"="Testing...",
         "logical"=TRUE,
         "numeric"=pi,
         "Date"=as.Date("2020-04-29", origin="1970-01-01")
   )
}

observeEvent(input$addTypedLoop, {
  columns <- v$data %>% summarise_all(class) %>% gather(variable, class)
  v$data <- v$data %>% add_row()
  targetRow <- nrow(v$data)
  for (col in columns$variable) {
    colClass <- (columns %>% filter(variable  == col))$class
    v$data <- v$data %>% mutate(!! col := ifelse(row_number() == targetRow, asTypedValue(newValues[col], colClass), !! col)) 
  }
})
library(shiny)
library(tidyverse)

ui <- fluidPage(
  wellPanel(
    actionButton("reset", "Reset"),
    actionButton("addDirect", "Add row (direct manipulation)"),
    actionButton("addLoop", "Add row (apply loop)"),
    actionButton("addTypedLoop", "Add row (apply loop with typing)")
  ),
  wellPanel(
    verbatimTextOutput("tibble")
  )
)

server <- function(input, output) {
  newValues <- c("W"="Testing...", "X"=pi, "Y"=TRUE, "Z"=as.Date("2020-04-29"))

  makeTibble <- function() {
    tibble(W=character(), X=numeric(), Y=logical(), Z=structure(NA_real_, class = "Date"))
  }

  v <- reactiveValues(
    data=makeTibble()
  )

  output$tibble <- renderPrint({
    v$data
  })

  observeEvent(input$reset, {
    v$data <- makeTibble()
  })

  observeEvent(input$addDirect, {
    v$data <- v$data %>% add_row(W="Testing...", X=pi, Y=TRUE, Z=as.Date("2020-04-29", origin="1970-01-01"))
  })

  observeEvent(input$addLoop, {
    columns <- v$data %>% dplyr::summarise_all(class) %>% tidyr::gather(variable, class)
    v$data <- v$data %>% add_row()
    for (f in columns$variable) {
      v$data <- v$data %>% mutate(!!f := newValues[f])
    }
  })

  asTypedValue <- function(value, type) {
    print(paste0("type: ", type, "; value: ", value))
    switch(type, 
           "character"="Testing...",
           "logical"=TRUE,
           "numeric"=pi,
           "Date"=as.Date("2020-04-29", origin="1970-01-01")
    )
  }

  observeEvent(input$addTypedLoop, {
    columns <- v$data %>% summarise_all(class) %>% gather(variable, class)
    v$data <- v$data %>% add_row()
    targetRow <- nrow(v$data)
    for (col in columns$variable) {
      colClass <- (columns %>% filter(variable  == col))$class
      v$data <- v$data %>% mutate(!! col := ifelse(row_number() == targetRow, asTypedValue(newValues[col], colClass), !! col)) 
    }
  })
}

shinyApp(ui, server)
newValues <- c("W"="Testing...", "X"=pi, "Y"=TRUE, "Z"=as.Date("2020-04-29"))
> newValues
                 W                  X                  Y                  Z 
      "Testing..." "3.14159265358979"             "TRUE"            "18381" 
newValues <- list("W"="Testing...", "X"=pi, "Y"=TRUE, "Z"=as.Date("2020-04-29"))