2017-03-16 39 views
1

我試圖創建一個閃亮的應用程序,允許用戶輸入值。數據中的缺失值將被用戶提供的值或默認值取代。用戶輸入該值後,新文件將生成名稱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 
+0

你沒有在任何地方使用'data.table'對象。也許你正在考慮'dt'包和它們的DataTables(或者它們是指它)。 – lmo

+0

這是一個相當複雜的事情,看起來似乎是第一個閃亮的程序。它的結構並不是真正需要被動方案的結構。想想如何幫助你。 –

回答

1

這是一個相當複雜的工作,而且那裏是有一些很酷的代碼,你還沒有真正構建它這樣你就可以在Shiny中取得很大的進步。對於第一次或第二次閃亮的事業來說,這可能太複雜了。

如果我有時間,我會爲你重寫它,但我現在沒有,我認爲你可以自己做,並學到很多東西。這是我認爲必須完成的:

第一個我會將submitButton更改爲actionButton。在使用submitButton的Shiny中,幾乎總是走錯路 - 它只會導致死衚衕(就像你現在發現的那樣)。你需要的東西是這樣的:

actionButton("applyChanges","Apply Changes"), 

,你需要做的data_newreactiveEvent功能。您現在在初始化過程中執行的計算必須移動或複製到反應代碼塊中。事情是這樣的:

data_new <- eventReactive(applyChanges,{ 

    # code to change NAs in data_set1 to something specified in the input goes here 
    ############################################################################### 

    ind <- sapply(data_set1, is.numeric) 
    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" 
    d_new <- stats_d 
    d_new["User_input"] <- d_new$Max 
    d_new["OutlierCutoff"] <- 1 

    d_new["Drop_Variable"] <- "No" 
    return(d_new) 
}) 

我將你的所有輸入控件轉換成renderUI部件,並讓它們在服務器以及使用data_new反應您剛纔創建計算。像這樣在ui功能:

uiOutput("select") 

而且這樣的server功能:

output$select <- renderUI({ 
     selectInput("select", label = h3("Select Variable"), 
        choices = unique(data_new()$variable), 
        selected = unique(data_new()$variable)[1]), 
    ) 
    }) 

注意函數括號()在data_new()。這是因爲它現在是被動的。對輸入控件select,num,select1,num1,select2,select3這樣做。

第四個(實際上這可能是首先要做的),觀看Joe Cheng用Shiny找到的所有視頻 - 觀看它們多次,以瞭解它。反應式編程與其他形式的編程不同。需要一段時間才能得到它。

希望我沒有犯任何混淆的語法錯誤。重組的好運氣。我不認爲用Shiny實際上有另一種方法,但我可能是錯的。

+0

非常感謝您的詳細評論。我仍然試圖實施以上並提出解決方案。只是一個小問題,我們如何在返回(d_new)後檢查/查看文件d_new。我不想在ui中使用這個文件,但想在全局R環境中檢查它。有什麼辦法可以做到嗎? –

+0

Upvote和驗收會很好。嘗試「utils :: View(mtcars)」以獲得一個彈出窗口,其中包含數據幀的當前內容。例如,使用ui'actionButton(「b1」,「View Mtcars」)'和在服務器'observeEvent(輸入$ b1,{utils :: View(mtcars)})''中的 –

+0

會得到一個包含mtcars數據幀的彈出窗口。 –