这个想法是开发一个 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")
转换后,我想检查缺失值,并在框中动态生成结果,并使用 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/