2017-05-05 1 views
0

私は最初のShiny Appを作成しようとしており、基本的に2つの単純なヒストグラムをプロットするようにしていますが、入力ボタンを使用して2つのヒストグラムを並べて表示するかどうかを選択できます横に)または上下に1つ(垂直に)。Shiny Appの入力ボタンを使用してレイアウトを反応的に変更するにはどうすればよいですか?

実際にはうまくいくと思っているよりも、自分の思考プロセスを説明するために、以下のコードを書きました。

ご協力いただきありがとうございます!

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) 

答えて

0

あなたは結果を表示するために、入力に依存したレイアウトを作成するために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) 
+0

グレート、ありがとう!常に物事を並べて表示することを考慮すると、1つのヒストグラム、あるいはその両方を表示したい場合は、代わりに入力ボタンを選択する方法がありますか? – lc23

+0

はい、あなたのニーズに合わせてrenderUIのコンテンツを変更するだけです – HubertL

関連する問題