I want to write an app and input different csv file when using the app in different time. There should be many row inside my file and I want to select specific row for comparison, eg to get the mean, standard deviation of row. I name the row as 'Numerical Variable' in the code. I want to put the specific row in to a data frame for comparison. However, when I run the code, error message 'Warning: Error in as.data.frame.default: cannot coerce class ‘c("reactive.event", "reactiveExpr", "reactive", "function")’ to a data.frame' would be seen.
library(shiny)
library(data.table)
library(dplyr)
library(matrixStats)
# Define UI for application that draws a histogram
not_sel <- 'Not Selected'
main_page <- tabPanel(
title = 'Analysis',
titlePanel('Analysis'),
sidebarLayout(
sidebarPanel(
title = 'Inputs',
fileInput("csv_input","Select CSV File to Import",accept=".csv"),
selectInput("num_var_1","Numerical Variable 1",choices=not_sel),
selectInput('num_var_2','Numerical Variable 2',choices = not_sel),
selectInput('num_var_3','Numerical Variable 3',choices = not_sel),
actionButton("run_button","Run Analysis",icon=icon("play"))
),
mainPanel(
tabsetPanel(
tabPanel(
title = 'Statistics',
fluidRow(
column(width = 4, strong(textOutput('combine_data')))
),
fluidRow(
column(width = 4, tableOutput('combine_data_table'))
),
fluidRow(
column(width = 4, strong(textOutput('list_of_outlier_combine')))
)
)
)
)
)
)
ui <- navbarPage(
title = 'Data Analyser',
main_page
)
# Define server logic required to draw a histogram
server <- function(input, output){
output$book <- renderText(input$title)
data_input <- reactive({
req(input$csv_input)
fread(input$csv_input$datapath)
})
observeEvent(data_input(),{
choices <- c(not_sel, names(data_input()))
updateSelectInput(inputId = 'num_var_1', choices = choices)
updateSelectInput(inputId = 'num_var_2',choices = choices)
updateSelectInput(inputId = 'num_var_3',choices = choices)
})
num_var_1 <- eventReactive(input$run_button,input$num_var_1)
num_var_2 <- eventReactive(input$run_button,input$num_var_2)
num_var_3 <- eventReactive(input$run_button,input$num_var_3)
create_num_var_table <- function(data_input,num_var){
if(num_var != not_sel){
col <- data_input[,get(num_var)]
if(length(col)>5000) col_norm<-sample(col,5000) else col_norm<-col
statistic <- c('mean','median','standard deviation' )
df <- as.data.frame(
isolate(num_var_1),isolate(num_var_2),isolate(num_var_3)
)
mean = rowMeans(df)
std=rowSds(df)
Tmin=round(mean-3*std)
Tmax=round(mean+3*std)
value <- c(mean,std)
data.table(statistic,value)
}
}
output$combine_data <- renderText('Combine Data Table')
combine_data_table <- eventReactive(input$run_button,{
create_num_var_table(data_input(), num_var_1())
})
output$combine_data_table <-
renderTable(combine_data_table(),colnames = FALSE)
create_list_of_outlier <- function(data_input,num_var){
if(num_var != not_sel){
outlier <- data_input[,get(num_var)]
outlier[which(outlier<Tmin | outlier>Tmax)]
}
}
list_of_outlier <- eventReactive(input$run_button,{
create_list_of_outlier(data_input(),num_var_1())
})
output$list_of_outlier <-
renderText(paste('List of Outlier(s):',list_of_outlier()))
}
# Run the application
shinyApp(ui = ui, server = server)