我试图创建一个闪亮的应用程序,允许用户输入值。数据中的缺失值将被用户提供的值或默认值取代。用户输入该值后,新文件将生成名称data_new。我想使用这个文件来进一步更新我的原始数据集来替换缺失的值。我不知道如何从闪亮的应用程序文件和更新数据表中获取输入。使用单一代码中的闪亮输出更新数据表
代码第1部分:
library(shiny)
library(readr)
library(datasets)
data_set <- structure(list(A = c(1L, 4L, 0L, 1L), B = c("3", "*", "*", "2"
), C = c("4", "5", "2", "*"), D = c("*", "9", "*", "4")), .Names = c("A", "B", "C", "D"), class = "data.frame", row.names = c(NA, -4L))
data_set1 <- data_set
my.summary <- function(x, na.rm=TRUE){
result <- c(Mean=mean(x, na.rm=na.rm),
SD=sd(x, na.rm=na.rm),
Median=median(x, na.rm=na.rm),
Min=min(x, na.rm=na.rm),
Max=max(x, na.rm=na.rm),
N=length(x),
Nmiss = sum(is.na(x)))
}
# identifying numeric columns
ind <- sapply(data_set1, is.numeric)
# applying the function to numeric columns only
stats_d <- data.frame(t(data.frame(sapply(data_set1[, ind], my.summary))))
stats_d <- cbind(Row.Names = rownames(stats_d), stats_d)
colnames(stats_d)[1] <- "variable"
data_new <- stats_d
#rownames(data) <- c()
data_new["User_input"] <- data_new$Max
data_new["OutlierCutoff"] <- 1
data_new["Drop_Variable"] <- "No"
shinyApp(
ui <-
fluidPage(
titlePanel("Univariate Analysis"),
# Create a new row for the table.
sidebarLayout(
sidebarPanel(
selectInput("select", label = h3("Select Variable"),
choices = unique(data_new$variable),
selected = unique(data_new$variable)[1]),
numericInput("num", label = h3("Replace missing value with"), value = unique(data_new$variable)[1]),
selectInput("select1", label = h3("Select Variable"),
choices = unique(data_new$variable),
selected = unique(data_new$variable)[1]),
numericInput("num1", label = h3("Outlier Cutoff"), value = unique(data_new$variable)[1],min = 0, max = 1),
selectInput("select2", label = h3("Select any other Variable to drop"),
choices = unique(data_new$variable),
selected = unique(data_new$variable)[1]),
selectInput("select3", label = h3("Yes/No"),
choices = list("Yes", "No")),
submitButton(text = "Apply Changes", icon = NULL)),
mainPanel(
dataTableOutput(outputId="table")
)) )
,
Server <- function(input, output) {
# Filter data based on selections
output$table <- renderDataTable({
data_new$User_input[data_new$variable==input$select] <<- input$num
data_new$OutlierCutoff[data_new$variable==input$select1] <<- input$num1
data_new$Drop_Variable[data_new$variable==input$select2] <<- input$select3
data_new
})
})
代码2部分:
data_set[as.character(data_new$variable)] <- Map(function(x, y)
replace(x, is.na(x), y), data_set[as.character(data_new$variable)], data_new$User_input)
data_setN <- data_set
你没有在任何地方使用'data.table'对象。也许你正在考虑'dt'包和它们的DataTables(或者它们是指它)。 – lmo
这是一个相当复杂的事情,看起来似乎是第一个闪亮的程序。它的结构并不是真正需要被动方案的结构。想想如何帮助你。 –