【问题标题】:Dealing with loop, fluidRow and column in shiny处理闪亮的循环,流体行和列
【发布时间】:2017-02-15 10:54:15
【问题描述】:

我正在尝试制作一个闪亮的应用程序,该应用程序在不同的行上显示多个图,并允许用户通过使用位于图形旁边的单选按钮来选择正确的趋势。问题是我无法直接在地块旁边找到单选按钮。

我想要:

我得到:

我的代码:

服务器.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)      

    }
    ))

  )
))

我希望它很清楚。我是 Shiny 的新手(但不是 R),我仍处于学习曲线的陡峭部分!

谢谢

【问题讨论】:

  • 绘图的默认高度为 400px,因此您可以将单选按钮放在 div 或其他具有相同高度的容器中。

标签: r shiny


【解决方案1】:

可能是这样的:

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)
        )
      )
  })
))

【讨论】:

  • 有效,但你有一个括号问题(必须删除第三行的那个)。谢谢
【解决方案2】:

这是一个很好的模块用例。我没有让按钮完美对齐,但这可以通过一些 CSS 来解决:

library(shiny)

myModUI <- function(id) {
  ns <- NS(id)

  tagList(
  fluidRow(
    splitLayout(cellWidths=c("75%","25%"),
      plotOutput(ns("g")),
      radioButtons(ns("radio"),label = "buttons",
                   choices = list("with intercept"=1,"without intersept"=2),
                   selected = 1))      
    )
  )

} 

myMod <- function(input, output, server, seed) {

  output$g <- renderPlot({
    set.seed(seed)
    xx <- rnorm(10)
    yy <- rnorm(10)
    plot(xx,yy)
    abline(reg=lm(yy~xx), col=2, lwd=ifelse(input$radio==1,2,1))
    abline(reg=lm(yy~xx+0), col=3, lwd=ifelse(input$radio==2,2,1))

  })

  return(reactive(input$radio))

}

server <- shinyServer(function(input, output, server) {

  lapply(1:5,function(i) {
    callModule(myMod,i,seed=i)
  })

})

ui <- shinyUI(fluidPage(

  titlePanel("My loop test"),
  mainPanel(
  lapply(1:5,function(i) {
    myModUI(i)
  })
  )
))

shinyApp(ui=ui,server=server)

注意fluidRow是不够的,我们必须使用splitLayout

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多