我想要一个动态大小的情节,一切都应该发生在 Shiny 的UI中。
这是我的代码:
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”只是为了看情节。
我尝试了不同的方法从 slider 输入中获取数据并将其带到 plotOutput。我尝试了“input.height”、“input$heigt”,但没有任何效果。
最佳答案
您必须使用服务器端的输入,例如这里是一种解决方案:
并且宽度和高度的单位必须是有效的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)
})
}
))
关于R Shiny : plot with dynamical size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22408144/