闪亮:renderPlot 与 fluidrow 重叠
Shiny: overlapping of renderPlot with fluidrow
在 Shiny 示例中 http://shiny.rstudio.com/gallery/plot-plus-three-columns.html 如果我们替换 server.R
中的代码部分
print(p)
})
来自
print(p)
}, height = 600)
我们得到图形 window 的重叠,下面的输入与 fluidrow
集成。
当在 renderPlot
中指定 height
时,我们如何避免这种重叠?
谢谢。
将绘图设为 UI 对象,然后渲染时就会知道大小。我将绘图大小设为滑块,这当然是可选的:)
ui.R
library(shiny)
library(ggplot2)
dataset <- diamonds
shinyUI(fluidPage(
title = "Diamonds Explorer",
uiOutput('sized_plot'),
hr(),
fluidRow(
column(3,
h4("Diamonds Explorer"),
sliderInput('sampleSize', 'Sample Size',
min=1, max=nrow(dataset),
value=min(1000, nrow(dataset)),
step=500, round=0),
sliderInput('plotSize', 'Plot Size',
min=100, max=2000,
value=600,
step=50, round=0),
br(),
checkboxInput('jitter', 'Jitter'),
checkboxInput('smooth', 'Smooth')
),
column(4, offset = 1,
selectInput('x', 'X', names(dataset)),
selectInput('y', 'Y', names(dataset), names(dataset)[[2]]),
selectInput('color', 'Color', c('None', names(dataset)))
),
column(4,
selectInput('facet_row', 'Facet Row',
c(None='.', names(diamonds[sapply(diamonds, is.factor)]))),
selectInput('facet_col', 'Facet Column',
c(None='.', names(diamonds[sapply(diamonds, is.factor)])))
)
)
))
server.R
library(shiny)
library(ggplot2)
shinyServer(function(input, output) {
dataset <- reactive({
diamonds[sample(nrow(diamonds), input$sampleSize),]
})
output$plot <- renderPlot({
p <- ggplot(dataset(), aes_string(x=input$x, y=input$y)) + geom_point()
if (input$color != 'None')
p <- p + aes_string(color=input$color)
facets <- paste(input$facet_row, '~', input$facet_col)
if (facets != '. ~ .')
p <- p + facet_grid(facets)
if (input$jitter)
p <- p + geom_jitter()
if (input$smooth)
p <- p + geom_smooth()
print(p)
})
output$sized_plot <- renderUI({
plotOutput("plot", height = input$plotSize)
})
})
在 Shiny 示例中 http://shiny.rstudio.com/gallery/plot-plus-three-columns.html 如果我们替换 server.R
print(p)
})
来自
print(p)
}, height = 600)
我们得到图形 window 的重叠,下面的输入与 fluidrow
集成。
当在 renderPlot
中指定 height
时,我们如何避免这种重叠?
谢谢。
将绘图设为 UI 对象,然后渲染时就会知道大小。我将绘图大小设为滑块,这当然是可选的:)
ui.R
library(shiny)
library(ggplot2)
dataset <- diamonds
shinyUI(fluidPage(
title = "Diamonds Explorer",
uiOutput('sized_plot'),
hr(),
fluidRow(
column(3,
h4("Diamonds Explorer"),
sliderInput('sampleSize', 'Sample Size',
min=1, max=nrow(dataset),
value=min(1000, nrow(dataset)),
step=500, round=0),
sliderInput('plotSize', 'Plot Size',
min=100, max=2000,
value=600,
step=50, round=0),
br(),
checkboxInput('jitter', 'Jitter'),
checkboxInput('smooth', 'Smooth')
),
column(4, offset = 1,
selectInput('x', 'X', names(dataset)),
selectInput('y', 'Y', names(dataset), names(dataset)[[2]]),
selectInput('color', 'Color', c('None', names(dataset)))
),
column(4,
selectInput('facet_row', 'Facet Row',
c(None='.', names(diamonds[sapply(diamonds, is.factor)]))),
selectInput('facet_col', 'Facet Column',
c(None='.', names(diamonds[sapply(diamonds, is.factor)])))
)
)
))
server.R
library(shiny)
library(ggplot2)
shinyServer(function(input, output) {
dataset <- reactive({
diamonds[sample(nrow(diamonds), input$sampleSize),]
})
output$plot <- renderPlot({
p <- ggplot(dataset(), aes_string(x=input$x, y=input$y)) + geom_point()
if (input$color != 'None')
p <- p + aes_string(color=input$color)
facets <- paste(input$facet_row, '~', input$facet_col)
if (facets != '. ~ .')
p <- p + facet_grid(facets)
if (input$jitter)
p <- p + geom_jitter()
if (input$smooth)
p <- p + geom_smooth()
print(p)
})
output$sized_plot <- renderUI({
plotOutput("plot", height = input$plotSize)
})
})