Shiny 如何使用Shining应用程序中的输入按钮以反应方式更改布局?

Shiny 如何使用Shining应用程序中的输入按钮以反应方式更改布局?,shiny,shinydashboard,Shiny,Shinydashboard,我正在尝试创建我的第一个闪亮的应用程序,我正在尝试让应用程序绘制两个简单的直方图,但通过一个输入按钮,让用户选择是要并排(水平)查看两个直方图,还是要在另一个直方图下方(垂直)查看一个直方图 我试着写下面的代码更多的是为了向你解释我的思维过程,而不是希望它能起作用 感谢您的帮助,谢谢 library(shiny) ui <- fluidPage( radioButtons('layout', 'Layout:', choices=c('Vertically', 'Horizontally

我正在尝试创建我的第一个闪亮的应用程序,我正在尝试让应用程序绘制两个简单的直方图,但通过一个输入按钮,让用户选择是要并排(水平)查看两个直方图,还是要在另一个直方图下方(垂直)查看一个直方图

我试着写下面的代码更多的是为了向你解释我的思维过程,而不是希望它能起作用

感谢您的帮助,谢谢

library(shiny)

ui <- fluidPage(
radioButtons('layout', 'Layout:', choices=c('Vertically', 'Horizontally'), inline=TRUE),
sliderInput(inputId = "num",
          label = "Choose a number",
          value = 25, min = 1, max = 100),
plotOutput("hist1"),
plotOutput("hist2"))

server <- function(input,output) {

if (input$layout == "Horizontally") {
  output$hist1<-fluidRow(
  column(3,plotOutput(hist(rnorm(input$num)))))
  output$hist2<-column(3,plotOutput(hist(rnorm(input$num))))
} 
else if (input$layout == "Vertically") {
  output$hist1<-fluidRow(
    column(3,plotOutput(hist(rnorm(input$num)))))
  output$hist2<-fluidRow(
    column(3,plotOutput(hist(rnorm(input$num)))))
}

}

shinyApp(ui=ui, server=server)
库(闪亮)

ui您可以使用
renderUI
根据输入创建布局,使用
uiOutput
显示结果:

ui <- fluidPage(
  radioButtons('layout', 'Layout:', choices=c('Vertically', 'Horizontally'), inline=TRUE),
  sliderInput(inputId = "num",
              label = "Choose a number",
              value = 25, min = 1, max = 100),
  uiOutput("plots"))

server <- function(input,output) {
  output$hist1<-renderPlot(hist(rnorm(input$num)))
  output$hist2<-renderPlot(hist(rnorm(input$num)))
  output$plots<-renderUI(if (input$layout == "Horizontally") {
    fluidRow(column(3,plotOutput("hist1")),
             column(3,plotOutput("hist2")))
    } 
    else if (input$layout == "Vertically") {
      fluidRow(plotOutput("hist1"),plotOutput("hist2"))
      })
}

shinyApp(ui=ui, server=server)

ui太好了,谢谢!考虑到总是并排显示,如果我只想显示一个直方图或两个直方图,是否有办法通过输入按钮进行选择?是的,只需更改渲染的内容以适应您的需要