I get input data from a file uploading widget ( reactive
) like:
Step 1 getData <- reactive(...) #psuedo code
.
And I need to do some work on the data and somehow store the result:
Step 2 result #SOMEHOW# = Clean1(getData()) #psuedo code
.
In the end I'll render the result like:
Step 3 output$DataAfter = renderTable({ result... }) #psuedo code
.
How should I do Step 2 ? I tried result = reactive({ Clean1(getData()) })
, but there's an error: the input is class NULL
before I upload the file.
The following code works , where I put the Clean1()
function inside Step 1 getData()
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr()
),
mainPanel(
tableOutput('DataBefore'),
tableOutput('DataAfter'),
tableOutput('Aggregation')
)
)
))
server.R
library(shiny)
library(dplyr)
library(stringr)
library(stringdist)
library(qdapRegex)
source('Clean1.R')
shinyServer(function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
Clean1( read.csv(inFile$datapath) )
})
output$DataAfter = renderTable({
as.data.frame( getData()[[1]] )
})
output$Aggregation = renderTable({
as.data.frame( getData()[[2]] )
})
})
Clean1.R
Clean1 = function(data){
data = data %>% sample_frac(1)
data = data %>%
mutate_each(funs(toupper)) %>%
mutate_each(funs(gsub("[[:punct:]]", " ", .))) %>%
mutate_each(funs(str_trim)) %>%
mutate_each(funs(rm_white))
by.town = data %>%
group_by(State, Town) %>%
summarise( Count = n() )
return(list(data, by.town))
}
Now I **store the result like: result = reactive({ Clean1(getData()) })
, and it shows error until I upload the file (still works in the end).
The error is
Don't know how to sample from objects of class NULL
This error should happen at the beginning of Clean1()
. It seems to think getData()
is NULL
before I upload it.
ui. R and Clean1.R doesn't change.
server.R
library(shiny)
library(dplyr)
library(stringr)
library(stringdist)
library(qdapRegex)
source('Clean1.R')
shinyServer(function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath)
})
result = reactive({
Clean1(getData())
})
output$DataAfter = renderTable({
as.data.frame( result()[[1]] )
})
output$Aggregation = renderTable({
as.data.frame( result()[[2]] )
})
})
So what's standard way to do Step 2 ? Let me know please. Thanks a lot!
The getData()
is NULL
when the code first evaluates, so that error makes sense. You could make the result
a reactive value that is monitored by an observer, but I think that would be more work than necessary. If you want it to not display an error, try just wrapping the Clean1
in a tryCatch
block that returns NULL
on error.
result = reactive({
tryCatch({
Clean1(getData())
}, error=function(e) NULL)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.