I want to create a shiny page with a model (already exists). Yet, additional to the "old" models implemented I would like the user to be able to upload new data and combine it with the old data. Based on this new data I want to create a new model and display, this discarding the old model. If the user does not upload new data I want the old model and data to be displayed. In short if(new){new model + new plot}else{old model + old plot}. However, the (example) code below only displays the plot if new data is provided, but not the old model if no data is provided. I am not able to figure out how to incorporate this into the reactive environment. Does anyone know how to incorporate this in shiny?
Thank you in advance,
library(shiny)
#stored in app folder as model1
x <- 1:100
set.seed(123)
y1<- 1.5*x+rnorm(100,0,50)
old <- data.frame(x,y1)
model1 <- lm(y1~.,data = old)
#Uploaded as new data
#set.seed(1)
#y2 <- 1.43*x+rnorm(100,0,40)
#new <- data.frame(x,y2)
#write.csv(new,file = "new.csv",row.names = F)
#shiny app
ui <- fluidPage(
titlePanel("Stuff"),
sidebarPanel(fileInput("file1", "Upload file y~x.",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))),
mainPanel(
plotOutput("Plot")
)
)
server <- function(input, output, session) {
newdata <- reactive({
req(input$file1)
new <- input$file1
if(is.null(new)){return(old)}else{
new <- as.data.frame(read.csv(input$file1$datapath, stringsAsFactors = TRUE))
colnames(new)<-c("x","y1")
return(rbind(old, new))}})
output$Plot <- renderPlot({
new <- newdata()
if(!is.null(new)){
plot(new)
abline(lm(new$y1~.,data=new))}else{
plot(old)
abline(model1)}
})
}
shinyApp(ui = ui, server = server)
You are right, not if I run the code provided. But your example gave me some difficulties incorporating it in the code, although it gave me information to look for some other solutions +1. Coding jargon is often a difficult topic searching for solution without a coding background.