R Shiny:具有动态尺寸的情节

时间:2022-04-04 13:17:01

I wanna have a plot with dynamic size and all should happen in the shinyUI.

我想要一个动态大小的情节,所有这些都应该发生在有光泽的UI中。

Here is my code:

这是我的代码:

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

I set "15cm" only to see the plot.

我设置“15cm”只是为了看情节。

I tried different methods to take the data from the sliderInputs and bring it to the plotOutput. I tried "input.height", "input$heigt" but nothing worked.

我尝试了不同的方法从sliderInputs获取数据并将其带到plotOutput。我尝试了“input.height”,“输入$ heigt”,但没有任何效果。

1 个解决方案

#1


9  

You must use the inputs in the server side, for example here is one solution :

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

And the unit of the width and height must be a valid CSS unit, i'm not sure that "cm" is valid, use "%" or "px" (or an int, it will be coerced to a string with "px" at the end)

并且宽度和高度的单位必须是有效的CSS单位,我不确定“cm”是否有效,使用“%”或“px”(或者int,它将被强制转换为带有“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)
        })
    }
))

#1


9  

You must use the inputs in the server side, for example here is one solution :

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

And the unit of the width and height must be a valid CSS unit, i'm not sure that "cm" is valid, use "%" or "px" (or an int, it will be coerced to a string with "px" at the end)

并且宽度和高度的单位必须是有效的CSS单位,我不确定“cm”是否有效,使用“%”或“px”(或者int,它将被强制转换为带有“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)
        })
    }
))