R Shiny:动态调整大小的绘图

8

我希望在shinyUI中创建一个大小可变的图表。

以下是我的代码:

   shinyUI{
      sidebarPanel(
            sliderInput("width", "Plot Width", min = 10, max = 20, value = 15),
            sliderInput("height", "Plot Height", min = 10, max = 20, value = 15)
       )

        mainPanel(
            plotOutput("plot", width="15cm", height="15cm")
        )
    }

我只是设置了“15cm”来查看这个图。

我尝试过不同的方法将滑块输入数据传递给绘图输出。我尝试使用“input.height”、“input$heigt”,但都没有成功。


你尝试过使用 imageOutput 吗? - Stéphane Laurent
1个回答

10

你必须在服务器端使用输入,例如这里是一个解决方案:

宽度和高度的单位必须是有效的CSS单位,我不确定"cm"是否有效,请使用"%"或"px"(或整数,它将被强制转换为以"px"结尾的字符串)

library(shiny)

runApp(list(
    ui = pageWithSidebar(
    headerPanel("Test"),
    sidebarPanel(
            sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100),
            sliderInput("height", "Plot Height (px)", min = 0, max = 400, value = 400)
       ),
        mainPanel(
            uiOutput("plot.ui")
        )
    ),
    server = function(input, output, session) {

        output$plot.ui <- renderUI({
            plotOutput("plot", width = paste0(input$width, "%"), height = input$height)
        })

        output$plot <- renderPlot({
            plot(1:10)
        })
    }
))

5
有没有一种方法可以在没有用户输入的情况下完成这个操作?图表的大小可以根据其所在的窗口动态调整吗? - Kyle Weise

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