【问题标题】:Why does my plot disappear in shiny when I add a table?为什么当我添加表格时我的情节会以闪亮的形式消失?
【发布时间】:2020-06-12 15:15:47
【问题描述】:

我创建了一个闪亮的应用程序,应该从三个滑块中获取输入:

  1. 在 ggplot 中绘制分布
  2. 在上面 #1 的图下方显示值的汇总表

如果我只想绘制直方图(并注释掉表格数据),我可以让代码正常工作。但是,当我添加表格时,即使情节标题仍然存在,情节也会消失。我尝试将逗号移动到大括号中,看看它是否是一个简单的语法错误,但没有任何运气。

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

    # Application title
    titlePanel("Test Shiny Layout"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            h4("Input Data"),
            sliderInput("bins", "Bin Width", min = 4,max = 12, value = 8),
        ),
        # Show a plot of the generated distribution
        mainPanel(
            h4("Histogram"),
            plotOutput("distPlot", width = "600", height = "600"),
            h4("Table of Values"),
            tableOutput("table")
        )
    )

))

服务器

library(shiny)
library(ggplot2)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
        output$distPlot <- renderPlot({
        bins <- input$bins 
        df1 <- (iris$Sepal.Length)
        x <- mean(df1)
        y <- sd(df1)


        ggplot(data = iris) +
            geom_histogram(mapping = aes(x = Sepal.Length), color = "blue", binwidth = "bins")

# Create an empty dataframe and then plug in the mean and standard deviation
        results <- data.frame("0", "0")
        results[1,1] = x
        results[1,2] = y
        colnames(results) <- c("Mean", "SD")
        rownames(results) <- c("Sepal Length")

        output$table <- renderTable(results)
    })
})

【问题讨论】:

  • 我尝试将renderTable 放在下一组大括号之外(所以在图大括号之外),但它没有改变

标签: r ggplot2 shiny


【解决方案1】:

您的renderTable() 在您的renderPlot() 呼叫中。所以renderPlot 没有返回任何东西。

你是对的:这是一个简单的语法错误。但是您的代码中还有其他几个问题。至少十几个。仅binwidth = "bins" 中的三个。

这是一个工作版本。我怀疑你仍然想要进行调整,但至少你有一个直方图和一个汇总表,看起来都相当合理。

library(shiny)
library(ggplot2)
data(iris)

# Define server logic required to draw a histogram
server <- function(input, output) {
  output$distPlot <- renderPlot({
    ggplot(data = iris) +
      geom_histogram(aes(x = Sepal.Length), color = "blue", bins = input$bins)
  })

  output$table <- renderTable({
    iris %>%
      summarise(Mean=mean(Sepal.Length),
                SD=sd(Sepal.Length))
  })
}

ui <- fluidPage(
  titlePanel("Test Shiny Layout"),
  sidebarLayout(
    sidebarPanel(
      h4("Input Data"),
      sliderInput("bins", "Bin Width", min = 4,max = 12, value = 8),
    ),
    mainPanel(
      h4("Histogram"),
      plotOutput("distPlot", width = "600", height = "600"),
      h4("Table of Values"),
      tableOutput("table")
    )
  )
)

shinyApp(ui = ui, server = server)

【讨论】:

  • 我把renderTable 放在下一组大括号之外,(所以在情节大括号之外),它仍然没有改变
猜你喜欢
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 2017-03-18
  • 2021-10-19
  • 2018-09-01
  • 2016-09-27
  • 2017-06-14
相关资源
最近更新 更多