我試圖創建一個閃亮的應用程序,允許用戶輸入值。數據中的缺失值將被用戶提供的值或默認值取代。用戶輸入該值後,新文件將生成名稱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
這是一個相當複雜的事情,看起來似乎是第一個閃亮的程序。它的結構並不是真正需要被動方案的結構。想想如何幫助你。 –