r - 我们如何对 Shiny 的变量进行预处理步骤

标签 r shiny

这个想法是开发一个 Shiny 的应用程序,帮助用户在用户界面中上传文件(基本上是测试数据),然后用一个按钮检查预测并显示图表。

目前,我已经开发了UI,可以使用文件输入命令上传测试文件。

我现在很惊讶如何包含服务器命令,该命令会触发 更改数据类型并检查缺失值。我不确定如何将这些代码集成到我的服务器中。

任何帮助都会很棒。或任何可以指导我的链接都会有帮助。我花了一整天的时间寻找相关帖子,但未能找到并整合它。

下面是我的服务器代码和我想要集成的通用 R 代码。

用户界面代码:

shinyUI(fluidPage(
  titlePanel("File Input"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file","Upload the file"), # fileinput() function is used to get the file upload contorl option
      helpText("Default max. file size is 5MB"),
      tags$hr(),
      h5(helpText("Select the read.table parameters below")),
      checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
      checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
      br(),
      radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
      ),
    mainPanel(
      uiOutput("tb")
      )

    )
  ))

shinyServer(function(input,output){


  # file$datapath -> gives the path of the file
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

  })

  output$filedf <- renderTable({
    if(is.null(data())){return ()}
    input$file
  })

  output$sum <- renderTable({
    if(is.null(data())){return ()}
    summary(data())

     })

  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })

  output$tb <- renderUI({
    if(is.null(data()))
      h5("Powered by", tags$img(src='RStudio-Ball.png', heigth=200, width=200))
    else
      tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
  })
})

这是我想与我的服务器代码集成的预处理步骤。

转换数据类型

claim[,c(2:12,16:22,25,30,31,33,32,34)] <- lapply(claim[,c(2:12,16:22,25,30,31,33,32,34)], as.numeric)
claim[,c(1, 13:15)] <- lapply(claim[,c(1, 13:15)], as.Date, format = "%d.%m.%Y")

缺失值插补

mice_impute = mice(New,m=5, method='rf')
Imputed_data <- mice::complete(mice_impute, 3)

编辑

我在 R 环境中的数据集包含 4000 个观察值和 34 个变量。

例如,我考虑了 5 个变量。

变量的数据类型为因子。

声明 <- c(V1,V2,V3,V4,V5) 第一步是我想将它们转换为数字。这是一般的 R 代码。

claim[,c(2:12,16:22,25,30,31,33,32,34)] <- lapply(claim[,c(2:12,16:22,25,30,31,33,32,34)], as.numeric)
    claim[,c(1, 13:15)] <- lapply(claim[,c(1, 13:15)], as.Date, format = "%d.%m.%Y")

我想要一些用户友好的东西,如下所示。 By converting so, I should be able to create them as a new test data frame

转换后,我想检查缺失值,并在框中动态生成结果,并使用 mouse 包对它们进行插补。在我的 R 环境中,我已经有了小鼠的代码。

基本上我的训练集将在R环境本身中,上传的新测试数据应该在shiny中处理并根据训练集预测结果。

最佳答案

这是我解决此问题的方法,但我无法对此进行测试,请参阅我对您的问题的评论。

基本上,我们将数据存储在 reactiveVal 中,并使用 observeEvent 监听 actionButton 上的点击 (输入$convert_button,注意需要将这个添加到UI中),然后修改数据。

首先是一个使用 mtcars 数据集的工作示例。按“上传”以假上传您的数据,然后按“转换”以填充 NA。

my_mtcars <- mtcars
my_mtcars$cyl[1:10]=NA
my_mtcars$wt[1:10]=NA

ui <- fluidPage(
  actionButton('upload_button','Upload'),
  actionButton('convert_button','Convert'),
  tableOutput('table')
)

server <- shinyServer(function(input,output){

  data <- reactiveVal()

  # file$datapath -> gives the path of the file
  observeEvent(input$upload_button, ignoreNULL=T, ignoreInit=T, {
    data(my_mtcars)
  })

  observeEvent(input$convert_button, {
    claim <- data() # read the data from the reactiveVal
    claim[is.na(claim)] <- 0
    data(claim) # write back the modified data to the reactiveVal
  })

  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })

})

shinyApp(ui,server)

这是一个关于如何使用您的数据执行此操作的示例,但正如我所说,我无法对此进行测试:

shinyServer(function(input,output){

  data <- reactiveVal()

  # file$datapath -> gives the path of the file
  observeEvent(input$file, ignoreNULL=T, ignoreInit=T, {
    file1 <- input$file
    if(is.null(file1)){return()} 
    data(read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors))
  })

  observeEvent(input$convert_button, {
    claim <- data() # read the data from the reactiveVal
    claim[,c(2:12,16:22,25,30,31,33,32,34)] <- lapply(claim[,c(2:12,16:22,25,30,31,33,32,34)], as.numeric)
    claim[,c(1, 13:15)] <- lapply(claim[,c(1, 13:15)], as.Date, format = "%d.%m.%Y")
    data(claim) # write back the modified data to the reactiveVal
  })

  output$filedf <- renderTable({
    if(is.null(data())){return ()}
    input$file
  })

  output$sum <- renderTable({
    if(is.null(data())){return ()}
    summary(data())

  })

  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })

  output$tb <- renderUI({
    if(is.null(data()))
      h5("Powered by", tags$img(src='RStudio-Ball.png', heigth=200, width=200))
    else
      tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
  })
})

关于r - 我们如何对 Shiny 的变量进行预处理步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49687172/

相关文章:

r - R 中 match 和 lapply 的组合

r - R 中的标注文本

r - 如何创建一个弹出窗口供用户在 R shiny 中输入信息?

r - ggplot : How to make the x/time-axis of a time-series plot only the time-component, 不是日期?

r - 根据前 5 个值汇总 R 中的多列

r - 在 Shiny 应用程序中使用调查包会给出 "Warning: Error in table: all arguments must have the same length"

r - 使用 rCharts、shiny、nPlot 在 nvd3 中关闭散点图上的放大

r - 如何在 R 中的 DT::Datatable 中更改这些按钮的标签并更改行的颜色?

r - 检查连续日期内的两个值是否相同

r - 如何替换数据表中值的单个组件?