I try to change the data type of an imported data frame within a shiny app based on a condition but when I run the code I get the error: invalid (NULL) left side of assignment.
server <- function(input, output, session){
Reactive_Data <- eventReactive(input$Import_File_Button, {
read.csv(input$Data_File$datapath,
header = input$Header_Option,
sep = input$Seperator_Option,
quote = input$quote_Option)
})
Reactive_Preview <- eventReactive(input$Import_File_Button, {
input$Data_Preview
})
output$Data <- renderTable({
if(Reactive_Preview() == "head") {
return(head(Reactive_Data()))
} else {
return(Reactive_Data())
}
})
observe({
updateSelectizeInput(session, "X_Variable1" , choices = c(names(Reactive_Data())))
updateSelectizeInput(session, "Y_Variable1" , choices = c(names(Reactive_Data())))
})
output$Figure1 <- renderPlotly({
figure1 <- ggplot(Reactive_Data(), aes_string(input$X_Variable1, input$Y_Variable1)) + geom_point()
ggplotly(figure1)
})
observe({
updateSelectizeInput(session, "X_Variable2" , choices = c(names(Reactive_Data())))
updateSelectizeInput(session, "Y_Variable2" , choices = c(names(Reactive_Data())))
})
output$Figure2 <- renderPlotly({
figure2 <- ggplot(Reactive_Data(), aes_string(input$X_Variable2, input$Y_Variable2)) + geom_point()
ggplotly(figure2)
})
observe({
updateSelectizeInput(session, "X_Variable3" , choices = c(names(Reactive_Data())))
updateSelectizeInput(session, "Y_Variable3" , choices = c(names(Reactive_Data())))
})
output$Figure3 <- renderPlotly({
figure3 <- ggplot(Reactive_Data(), aes_string(input$X_Variable3, input$Y_Variable3)) + geom_point()
ggplotly(figure3)
})
test <- reactive({
req(input$Import_File_Button)
group_countx <- table(Reactive_Data()[[which(names(Reactive_Data()) == input$X_Variable1)]])
if (min(group_countx) > 2) {
Reactive_Data()[which(names(Reactive_Data()) == input$X_Variable1)] <- as.factor(Reactive_Data()[which(names(Reactive_Data()) == input$X_Variable1)])
return(Reactive_Data())
}
else {
Reactive_Data()[[which(names(Reactive_Data()) == input$X_Variable1)]] <- as.numeric(Reactive_Data()[[which(names(Reactive_Data()) == input$X_Variable1)]])
return(Reactive_Data())
}
})
output$summary <- renderPrint({
print(test())
})
})
I figured out that the line of code stated below is the problem:
Reactive_Data()[which(names(Reactive_Data()) == input$X_Variable1)] <- as.factor(Reactive_Data()[which(names(Reactive_Data()) == input$X_Variable1)])
I think that it has to do with the reactivity of the Reactive_Data() variable, cause if execute the same code in Rstudio without being part of a shiny app (so without "()" at the end of the variable) it works. Could someone help me out?