[英]How to clean up CSV data after uploading to Shiny App
Please help! 请帮忙!
I'm trying to build a Shiny App with the intent to classify data loaded from a CSV file. 我正在尝试构建一个Shiny App,目的是对从CSV文件加载的数据进行分类。 How do I successfully create a DataFrame from a CSV file (that is uploaded) so that I can move forward and clean/analyze it. 如何从CSV文件(已上传)成功创建DataFrame,以便可以继续进行和清理/分析它。
Please see code: 请查看代码:
library(shiny)
library(lubridate)
library(utils)
library(dplyr)
library(tidytext)
ui <- (pageWithSidebar(
headerPanel("CSV File Upload Demo"),
sidebarPanel(
#Selector for file upload
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
#These column selectors are dynamically created when the file is loaded
uiOutput("fromCol"),
uiOutput("toCol"),
uiOutput("amountflag"),
#The conditional panel is triggered by the preceding checkbox
conditionalPanel(
condition="input.amountflag==true",
uiOutput("amountCol")
)
),
mainPanel(
tableOutput("filetable")
)
))
Please advise whether to use Reactive 请告知是否使用反应式
server <- (function(input, output) {
#This function is repsonsible for loading in the selected file
filedata <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
dataframe <- reactive({
readr::read_csv(infile()$datapath)
})
# Clean data by whole-case removal of missing cells (either NAs or "nan")
# Remove the rows which have NAs
myDataClean2 = dataframe[complete.cases(dataframe),]
# In order to turn it into a tidy text dataset, we first put the data into a data frame:
text_df <- data_frame(myDataClean2$text,myDataClean2$title,myDataClean2$author,myDataClean2$id,myDataClean2$label)
names(text_df) <- c("text","title","author","id","label")
# Within the tidy text framework, we break both the text into individual tokens and transform
# it to a tidy data structure. To do this, we use tidytextâs unnest_tokens() function.
tidy_text_df <- text_df %>%
unnest_tokens(word, text)
#This previews the CSV data file
output$filetable <- renderText({
tidy_text_df()
})
})
})
# Run the application
shinyApp(ui = ui, server = server)
You are mixing reactive blocks. 您正在混合反应块。 Your filedata
should end with something that outputs your data, likely the output from unnest_tokens(word, text)
. 您的filedata
应以输出数据的结尾,可能是unnest_tokens(word, text)
的输出。 (It should put out all data you are interested in, I think that that line does.) From there, your output$filetable
needs to be outside of filedata
's reactive block, on its own. (它应该列出所有您感兴趣的数据,我认为那行会写的。)从那里,您的output$filetable
必须独立于 filedata
的反应性块之外 。 And it should be using filedata()
, not tidy_text_df
(which isn't available outside of the first reactive block). 它应该使用filedata()
,而不是tidy_text_df
(在第一个反应性块之外不可用)。
Try this: 尝试这个:
server <- (function(input, output) {
#This function is repsonsible for loading in the selected file
filedata <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
dataframe <- reactive({
readr::read_csv(infile()$datapath)
})
# Clean data by whole-case removal of missing cells (either NAs or "nan")
# Remove the rows which have NAs
myDataClean2 = dataframe[complete.cases(dataframe),]
# In order to turn it into a tidy text dataset, we first put the data into a data frame:
text_df <- data_frame(myDataClean2$text,myDataClean2$title,myDataClean2$author,myDataClean2$id,myDataClean2$label)
names(text_df) <- c("text","title","author","id","label")
# Within the tidy text framework, we break both the text into individual tokens and transform
# it to a tidy data structure. To do this, we use tidytextâs unnest_tokens() function.
text_df %>%
unnest_tokens(word, text)
})
#This previews the CSV data file
output$filetable <- renderText({
filedata()
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.