响应式绘图大小,闪亮

3

在shiny中是否有可能实现响应式图表大小?

这里是一个小例子,我希望它能够工作。然而它会报错,因为响应式表达式不在输出中。

ui.R文件中,您可以选择宽度和两个图表输出:

shinyUI(pageWithSidebar(
 headerPanel("Title"),
 sidebarPanel(
 selectInput("width", "Choose width:", 
             choices = c("200", "400"))
 ),

 mainPanel(
 plotOutput(outputId = "main_plot",  width = "100%"),
 plotOutput(outputId = "main_plot2", width = "100%")
 )
))

服务器.R文件,第二个图应该具有输入宽度:
shinyServer(function(input, output) { 
 x <- 1:10
 y <- x^2
 width <- reactive({
 switch(input$direction,
       '200' = 200,
       '400' = 400)
 })
 output$main_plot <- renderPlot({    
 plot(x, y)}, height = 200, width = 400)
 output$main_plot2 <- renderPlot({
 plot(x, y) }, height = 200, width = width() )
})
1个回答

8
你离正确的答案很近了,尝试这个:
ui <- shinyUI(pageWithSidebar(
  headerPanel("Title"),
  sidebarPanel(
    selectInput("direction", "Choose width:", 
                choices = c("200", "400"))
  ),

  mainPanel(
    plotOutput(outputId = "main_plot",  width = "100%"),
    plotOutput(outputId = "main_plot2", width = "100%")
  )
))

server <- shinyServer(function(input, output) { 
  x <- 1:10
  y <- x^2

  output$main_plot <- renderPlot({    
    plot(x, y)}, height = 200, width = 400)

  observe({
    output$main_plot2 <- renderPlot({
      plot(x, y) }, height = 200, width = as.numeric(input$direction))
  })

})

shinyApp(ui=ui,server=server)

啊,原来只是复制粘贴失败了...非常感谢您的澄清! :) - panuffel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接