R 我们如何对一个变量执行预处理步骤
其想法是开发一个闪亮的应用程序,帮助用户在用户界面上传一个文件,基本上是测试数据,然后用一个按钮检查预测和显示图形 目前,我已经开发了UI,可以使用文件输入命令上传测试文件 我现在很惊讶,我怎么能包括服务器命令,它会触发 更改数据类型并检查是否缺少值。我不确定如何将这些代码集成到我的服务器中 任何帮助都会很好。或者任何可以指导我的链接都是有帮助的。我花了一整天的时间寻找相关的帖子,但我没能找到并整合它 下面是我想要集成的服务器代码和通用R代码 用户界面代码:R 我们如何对一个变量执行预处理步骤,r,shiny,R,Shiny,其想法是开发一个闪亮的应用程序,帮助用户在用户界面上传一个文件,基本上是测试数据,然后用一个按钮检查预测和显示图形 目前,我已经开发了UI,可以使用文件输入命令上传测试文件 我现在很惊讶,我怎么能包括服务器命令,它会触发 更改数据类型并检查是否缺少值。我不确定如何将这些代码集成到我的服务器中 任何帮助都会很好。或者任何可以指导我的链接都是有帮助的。我花了一整天的时间寻找相关的帖子,但我没能找到并整合它 下面是我想要集成的服务器代码和通用R代码 用户界面代码: shinyUI(fluidPage(
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个变量
变量的数据类型为factor
声明以下是我将如何处理这一问题,但我无法测试这一点,请参阅我对您的问题的评论 基本上,我们将数据存储在一个reactiveVal中,并使用一个observeEvent来监听对actionButton输入$convert_按钮的点击,注意您需要将其添加到UI中,然后修改数据 首先,一个使用mtcars数据集的工作示例。按upload可上传数据,按convert可填写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")))
})
})
嗨@Jenny,请看一些关于如何创建可复制示例的提示。此外,我建议限制你的问题,以便更容易回答;例如,仅从缺失值插补开始,忽略其余部分。根据你得到的答案,看看你是否能自己完成下一步,如果不能的话;下一个问题的时间到了@Florian是的当然,我会编辑它。。希望你能在第一部分帮助我如何更改数据类型和插补缺失值;当然,更新准备好后请告诉我。@Florian如果您清楚更新内容,请告诉我。还不是100%,请参阅我提供的链接。您缺少UI和数据。在这种情况下,您可以使用dput包含一些数据,或者使用mtcars数据集。基本上,您是从哪里读取数据集的?那是R环境公司的吗?你能帮我拿一下这个吗。我没有任何线索来解决延尼问题,请考虑是否解决你的问题。谢谢是的,mtcar数据集就是这样。对不起,这就是我不能接受的原因。虽然它解决了mtcars,但我接受它作为样品
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")))
})
})