Bastien Bastien - 1 month ago 17
R Question

Dealing with loop, fluidRow and column in shiny

I'm trying to make a Shiny application that display multiple plots on different rows and allow user to select the proper tendencies by using the radioButtons situated beside the graph. The problem is that I'm not able to get the radioButtons located directly beside the plots.

I want:

enter image description here

And I get:
enter image description here

My code:

server.R:

library(shiny)

shinyServer(function(input, output) {

lapply(1:3, function(iter) {
output[[paste0("g",iter)]] <- renderPlot({

set.seed(iter)
xx <- rnorm(10)
yy <- rnorm(10)
plot(xx,yy)
abline(reg=lm(yy~xx), col=2, lwd=ifelse(input[[paste0("radio",iter)]]==1,2,1))
abline(reg=lm(yy~xx+0), col=3, lwd=ifelse(input[[paste0("radio",iter)]]==2,2,1))

})
})

})


ui.R:

library(shiny)

shinyUI(fluidPage(

titlePanel("My loop test"),

fluidRow(
column(6,
lapply(1:3, function(iter) {
plotOutput(paste0("g",iter))
}
)),
column(3,
lapply(1:3, function(iter){
radioButtons(paste0("radio",iter),label = "buttons", choices = list("with intercept"=1,"without intersept"=2),selected = 1)

}
))

)
))


I hope it's clear. I'm new to Shiny (but not R) and I'm still in the steep part of the learning curve!

Thanks

Answer

Maybe something like this:

shinyUI(fluidPage(
  titlePanel("My loop test"),
  lapply(1:3, function(iter) {
    fluidRow(
      column(
        6, 
        plotOutput( paste0("g",iter) )
      ),
      column(
        3,
        radioButtons(
          paste0("radio", iter), 
          label = "buttons",
          choices = list("with intercept"=1,"without intersept"=2),
          selected = 1)
        )
      )
  })
))
Comments