微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有特定高度的渲染图不允许接下来显示表格闪亮

如何解决具有特定高度的渲染图不允许接下来显示表格闪亮

我正在尝试绘制一个直方图,然后绘制一个表格。但是,由于我想要一个特定的高度,表格(必须出现在直方图之后)从直方图的中间开始。此外,我想在表格前显示一个标题。 (我怎么能在表格前写一个合适的标题?因为我写了“renderText”但它看起来不太好)。

我该怎么办?

如果我删除“高度”,表格就会显示得很好。

这是我的代码示例。

library(shiny)
library(ggplot2)
library(scales)

################### DATA ########################
val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.frame(val)
df1$type <- "Type 1"

val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.frame(val)
df2$type <- "Type 2"

df3 <- rbind(df1,df2)


################ SHINY APP ########################
ui <- fluidPage(
  
  titlePanel("Histogram"),sidebarLayout(
    sidebarPanel(
    ),mainPanel(
      plotOutput("hist"),textoutput("text"),tableOutput("table")
    )
  )
)

server <- function(input,output) {
  
  output$hist <- renderPlot(height=700,{
    p <- ggplot(df3,aes(val,fill=type)) +
      geom_histogram(position = "identity",colour = "grey40",bins = 10) +
      ggtitle("Here must be a title") +
      xlab("Values") +
      ylab("Frequency") +
      facet_grid(type ~ .) + 
      scale_x_continuous(breaks=pretty(as.matrix(df3$val),n=10))
    
    p + theme(strip.text.x = element_blank(),strip.text.y = element_blank())
    
  })
  
  output$text <- renderText("People with these values: ")
  output$table <- renderTable(df3)
  
} 


shinyApp(ui = ui,server = server)

这里可以看到问题:

problem

非常感谢,

问候

解决方法

我已经解决了。

这个想法是我应该在 ui 中移动“高度”而不是在服务器中。 plotOutput("hist",height=700)

为了在图和表之间留一些空间,可以解决写作br()

library(shiny)
library(ggplot2)
library(scales)

################### DATA ########################
val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.frame(val)
df1$type <- "Type 1"

val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.frame(val)
df2$type <- "Type 2"

df3 <- rbind(df1,df2)


################ SHINY APP ########################
ui <- fluidPage(
  
  titlePanel("Histogram"),sidebarLayout(
    sidebarPanel(
    ),mainPanel(
      plotOutput("hist",height=700),br(),textOutput("text"),tableOutput("table")
    )
  )
)

server <- function(input,output) {
  
  output$hist <- renderPlot({
    p <- ggplot(df3,aes(val,fill=type)) +
      geom_histogram(position = "identity",colour = "grey40",bins = 10) +
      ggtitle("Here must be a title") +
      xlab("Values") +
      ylab("Frequency") +
      facet_grid(type ~ .) + 
      scale_x_continuous(breaks=pretty(as.matrix(df3$val),n=10))
    
    p + theme(strip.text.x = element_blank(),strip.text.y = element_blank())
    
  })
  
  output$text <- renderText("People with these values: ")
  output$table <- renderTable(df3)
  
} 


shinyApp(ui = ui,server = server)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。