I am trying to use a dataframe within a reactive function to plot ggplot with brush and delete functionality. I read my csv file within a reactive function first and then I call that function to pass the dataframe and it works for plotting but when I try to use it within another reactive function that reacts to the brushing and deletion it fails me. I am unable to figure out whats wrong with the logic. it works when I read the csv directly and use that data frame object in the reactiveVals function that I use to brush and delete. Can someone help me figure out the mistake in my logic, please!
library(shiny)
library(DT)
library(shinydashboard)
library(ggplot2)
library(shinyFiles)
ui <- fluidPage(
shinyFilesButton(id = 'file', label= 'Choose file to upload',
title = 'Select file', multiple = FALSE),
#Shows data table on the main page
fluidRow(
column(12, DT::dataTableOutput('tabl')),
column(12, textOutput('test'))
# dataTableOutput("tabl")
),
# h5('Select two Columns to Plot'),
uiOutput("Col1"),
uiOutput("Col2"),
#-----------------------------------------------------------
#Shows Plot button
fluidRow(
column(6, plotOutput('plot2', height = 500)),
column(6, plotOutput('plot3', height = 500))
),
fluidRow(
column(7, class = "row",
h4("Brush and click to exclude Point"),
plotOutput("plot1", height = 500,
# click = "plot1_click",
brush = brushOpts(
id = "plot1_brush",
resetOnNew = TRUE
)
)
)
)
)
------------------------------------------------------------------------
server <- function(input, output, session) {
observe({
###Read cvs file and convert julian Date to regular Date format
shinyFileChoose(input, 'file', roots=
c(wd="/Users/mnoon/Desktop/projects/2018/rShinyApp_Data"), filetypes= c('', 'csv'))
df_products_upload <- reactive({
inFile->parseFilePaths(roots=c(wd='/Users/mnoon/Desktop/projects/2018/rShinyApp_imageData/'
), input$file)
if (NROW(inFile)){
# return(NULL)
df <- read.csv(as.character(inFile$datapath), header = TRUE, sep = ",", stringsAsFactors = F)
# names(df) <- c("NORA", "Date", "Xloc", "Yloc", "Right", "Dec", "Gmg")
df <- as.data.frame(df)
return(df)
}
})
###Previews data table on the main display window
# output$tabl<- DT::renderDataTable({
# df <- df_products_upload()
# DT::datatable(df)
# }, server = FALSE)
#
#
# ###The following set of functions populate the column selectors
# output$Col1 <- renderUI({
# df <-df_products_upload()
# if (is.null(df)) return(NULL)
#
# cols=names(df)
# names(cols)=cols
# selectInput("column1", "Select Column for X-axis", cols)
#
# })
#
# output$Col2 <- renderUI({
# df <-df_products_upload()
# if (is.null(df)) return(NULL)
#
# cols=names(df)
# names(cols)=cols
# selectInput("column2", "Select Column for Y-axis", cols)
# })
df <- df_products_upload()
# df2 <- as.data.frame(df)
df2 <- df
vals <- reactiveValues(
# df <- df_products_upload(),
# df2 <- as.data.frame(df),
# dat = imag2
# dat = mtcars
dat = df2
)
output$plot1 <- renderPlot({
ggplot(vals$dat, aes(X.location.pixels, Y.location.pixels)) +
geom_point()
})
observe({
df = brushedPoints(vals$dat, brush = input$plot1_brush, allRows = TRUE)
vals$dat = df[df$selected_== FALSE, ] ## Taking only those data points where the selected_ value is FALSE (alternatively ignoring rows with selected_ = TRUE status)
})
})
}
shinyApp(ui, server)
It crashes with error: Warning: Error in [: incorrect number of dimensions [No stack trace available]
when I read my csv directly (outside reactive function) and pass it to the reactive function it works:
imag2 <- read.csv2("/Users/mnoon/Desktop/projects/2018/rShinyApp_imageData/imageData.csv", sep = ",", header = T, stringsAsFactors = F)
head(imag2)
vals <- reactiveValues(
dat = imag2
# dat = mtcars
# dat = df2
)
It also works when I pass mtcars dataset into it as reading csv outside any funcstion . Logically it should work when I call this function
df <- df_products_upload(),
df2 <- as.data.frame(df),
and df <- df_products_upload() works for other plot functions and for displaying the data table (code commented out) but when its used (df2 object) within above reactive function it fails. what is it that I am missing here!
Please note that all what I am doing is inside an observe function. Any help/input is appreciated!