【问题标题】:Shiny input variables are selectively ignored by ggplotggplot 选择性地忽略闪亮的输入变量
【发布时间】:2019-10-09 21:10:23
【问题描述】:

我希望我的闪亮应用能够绘制具有不同列数的数据框。由于我不希望数据包含超过 5 列,因此我为 ggplot 对象编写了所有可能情况的明确列表。我还希望使用 UI 上的滑块来改变第一个对象的大小。

问题在于该滑块的值仅适用于多列数据。

为了测试这一点,这里有一个数据列的示例数据: https://drive.google.com/file/d/182Qi-2I37OscSeLir_AyXdoedstIxx6D/view?usp=sharing

这里有两个: https://drive.google.com/file/d/1eB0eKvgfj94xIp80P0SG3QnoGZJ1Zq9a/view?usp=sharing

这是我在这里的第一个问题,我不确定如何理想地为使用 fileInput 的闪亮应用程序提供示例。如果有比这更好的方法,请告诉我。

#The ui

shinyUI(

  pageWithSidebar(

    headerPanel(""),

    sidebarPanel(

      fileInput(inputId = "file", label = "Choose an Excel (.xlsx) File", 
                multiple = FALSE, 
                accept = c("text/xlsx", "text/microsoft-excel-pen-XML-format-spreadsheet-file", ".xlsx"),
                width = NULL, buttonLabel = "Find...",
                placeholder = "No File selected"
      ),

      sliderInput(inputId = "width", label = "width", min = 0.01, max = 0.4, value = 0.2, step = 0.01)),

    mainPanel(plotOutput("graph"), height = "600px", quoted = TRUE)
  )
)

#and the server

library(shiny)
library(ggplot2)
library(readxl)


shinyServer(function(input, output, session) {

  data <- reactive({
            data.frame(read_xlsx(input$file$datapath))
           })


  output$graph <- renderPlot({

      print(

        #somehow the interactive heigth setting is now muted within the expression
        ggplot(data(), aes(x = data()[,1], y = "", fill = as.factor(data()[2:ncol(data())])) ) +
        {if(ncol(data()) >= 2)geom_tile(aes(x = data()[,1], y = 0, fill = as.factor(data()[,2])), data(), width = 0.2, height = input$width, size = 2)} + 
        {if(ncol(data()) >= 3)geom_tile(aes(x = data()[,1], y = 0.125, fill = as.factor(data()[,3])), data(), width = 0.2, height = 0.02, size = 2)} +
        {if(ncol(data()) >= 4)geom_tile(aes(x = data()[,1], y = -0.125, fill = as.factor(data()[,4])), data(), width = 0.2, height = 0.02, size = 2)} +
        {if(ncol(data()) == 5)geom_tile(aes(x = data()[,1], y = -0.145, fill = as.factor(data()[,5])), data(), width = 0.2, height = 0.02, size = 2)}

          scale_y_continuous(breaks = NULL, labels = NULL)
          )



  })
})

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    所以,我终于找到了错误。问题是scale_y_continuous自动将 y 轴重新缩放为绘图元素的大小。就我而言,如果我添加了固定大小的绘图元素(如程序所做的那样,如果数据包含多于一列),这并没有显示出来,这也对 y 轴的大小设置了限制。在这种特殊情况下,我的解决方案是改用 ylim(-0.2005, 0.2005),从而固定 y 轴大小。

    【讨论】:

      【解决方案2】:

      renderPlot({...}) 已经是响应式的。您可以删除PLOT &lt;-reactive({print(,直接进入ggplot()

      总的来说,我想我会做这样的事情:

      output$graph <- renderPlot({
      
        df <- data()
        names(df) <- paste0("v", 1:length(df))
      
      
        # initial plot
        p <- 
          ggplot(df, aes(x = v1)) + 
          scale_y_continuous(breaks = NULL, labels = NULL)
      
      
        # conditions
        if (ncol(df) >= 2) { 
          p <- p + geom_tile(aes(y = 0, fill = as.factor(v2), width = 0.2, height = input$width, size = 2)
        }
      
      
        if (ncol(df) >= 3) { 
          p <- p + geom_tile(aes(y = 0.125, fill = as.factor(v3), width = 0.2, height = 0.02, size = 2)
        }
      
      
        etc...
      
        # return
        p
      
      })
      
      

      【讨论】:

      • 感谢清理!不过,它不会改变 if 语句的行为
      • 感谢您的帮助,最后它变得更加简单。我添加了我的解决方案。
      猜你喜欢
      • 2020-03-28
      • 2021-05-25
      • 2017-11-24
      • 1970-01-01
      • 2020-04-14
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多