1

I want to display a dynamic number of plots in Shiny according to some data rows that the user can pick from. I achieved this, however I keep getting the same plot. The right amount of times, but always with the wrong data. I already tried debugging, but the loops seem to work. Three plots with the plot id's 'plot1' 'plot2' 'plot3' are created. I also checked the browser inside the loop where I create the plots...the x and y values are the right ones... Anyone have a clue what I'm doing wrong?

ui <- bootstrapPage(

navbarPage("Anpassung Lastmodell",

tabPanel("Graph",

fluidRow(width=4,
uiOutput('alphaui'),
uiOutput('graphui')
)
)
)
)

server <- function(input, output, session) {
## Output Graphs
output$alphaui <- renderUI({

# Here I usually have a manually uploaded dataset, so that's why this part is in a dynamic UI
# The user selects the columns of the data he wants in the plots

alphacat <- beaver1

paramnames <- colnames(alphacat)
graphparam <- vector("list",length(paramnames))

for (i in 1:length(paramnames)) {
  graphparam[[i]] <- list(checkboxInput(paste0("graphparam",i,sep=""),paramnames[i],value=FALSE))

}
graphparam[[i+1]] <-  actionButton("graph_button", "Los!",width="200px")

return(graphparam)

 })

  output$graphui <- renderUI({
  graph <- plots()
  graph
 })

plots <- eventReactive(input$graph_button, {

# Selecting data to be plotted
alphacat <- beaver1 
paramnames <- colnames(alphacat)
paramnames_keep <- isolate(unlist(reactiveValuesToList(input)[paste0("graphparam",1:length(paramnames),sep="")]))
paramnames <- cbind(paramnames,paramnames_keep)
paramnames <- subset(paramnames,paramnames[,"paramnames_keep"]==TRUE,"paramnames")

graph <- list()

i <- 1
for (i in 1:length(paramnames)) {

  plotname <- paste("plot",i, sep="")

  x <- alphacat[,paramnames[i]]
  y <- alphacat$time

  output[[plotname]] <- renderPlot({
    plot(x,y)
  })
  graph[[i]] <- plotOutput(plotname,height=330,width=300)
  tagList(graph)

}

return(graph)
})

}


shinyApp(ui,server)
Ma Breidi
  • 11
  • 2
  • It's difficult to help you without a reproducible example. See: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Florian Jul 21 '17 at 15:59
  • Okay, I edited my entry. Sorry it's still quite a lot of code, but that's what's necessary, I guess... – Ma Breidi Jul 22 '17 at 09:41

1 Answers1

0

The issue in your code as it is posted right now, is that the y variable is not definedL

  x <- alphacat[,paramnames[i]]
  y <- alphacat$aq1

  output[[plotname]] <- renderPlot({
    plot(x,y)
  })

y$aq1 does not exist. Therefore, right now your plotting statement plot(x,y) is the same as plot(x). (try print(y) after the statement where you define y)

Florian
  • 24,425
  • 4
  • 49
  • 80
  • Oh, sorry, that's something I forgot to edit. aq1 exists in my dataset. When I tested this version it was probably still in my working environment. aq1 can be any fixed column of the dataset, essentially. I will edit it again. Sorry! The trouble when I run this is more that the data displayed on the x axis is always the same. – Ma Breidi Jul 22 '17 at 10:55