【问题标题】:Cannot display plot in Rshiny无法在 R Shiny 中显示绘图
【发布时间】:2018-11-02 22:38:28
【问题描述】:

我有一个闪亮的应用程序,我想做的是在一个选项卡中打印标题或完整数据框,在另一个选项卡中打印摘要,最后在第三个选项卡中打印图表。出于某种原因,我能够在前两个选项卡中生成表格和摘要,但不能在绘图中生成。我不知道我做错了什么。

这是我的ui.R 脚本

library(shiny)
library(shinyFiles)

shinyUI(pageWithSidebar(
  headerPanel(
    'Selections with shinyFiles',
    'shinyFiles example'
  ),
  sidebarPanel(
    tags$h3('Select your file'),

    # Shiny button   
    shinyFilesButton(id = 'file', label = 'File select', title = 'Please select a file', multiple = FALSE),

    tags$h3('Select your options for table'),

    # Horizontal line ----
    br(),

    # Input: Checkbox if file has header ----
    checkboxInput("header", "Header", TRUE),

    # Input: Select separator ----
    radioButtons("sep", "Separator",
                 choices = c(Comma = ",",
                             Semicolon = ";",
                             Tab = "\t"),
                 selected = ","),

    # Input: Select quotes ----
    radioButtons("quote", "Quote",
                 choices = c(None = "",
                             "Double Quote" = '"',
                             "Single Quote" = "'"),
                 selected = '"'),

    # Horizontal line ----
    tags$hr(),

    # Input: Select number of rows to display ----
    radioButtons("disp", "Display",
                 choices = c(Head = "head",
                             All = "all"),
                 selected = "head"),

    tags$h3('Select your options for plotting'),

    br(),

    # Input: 
    radioButtons("sho", "Show",
                 choices = c(Yes = "yes",
                             No = "no"),
                 selected = "yes")
    ),
  mainPanel(
    tabsetPanel(type = "tabs",
                tabPanel("Table", dataTableOutput("contents")),
                tabPanel("Summary", verbatimTextOutput("summary")),
                tabPanel("Plot", plotOutput("plt"))

  )
  ))
)

这是我的server.R 脚本

library(shiny)
library(shinyFiles)

shinyServer(function(input, output, session) {
  shinyFileChoose(input, 'file', roots=c(wd='/Users/upendra_35/Documents/CyVerse/Images_apps/DE/VICE/'), filetypes=c('', 'csv'))
  shinySaveButton("save", "Save file", "Save file as ...", filetype=c('', "csv"))
  output$contents <- renderDataTable({
    inFile <- parseFilePaths(roots=c(wd='/Users/upendra_35/Documents/CyVerse/Images_apps/DE/VICE/'), input$file)
    if( NROW(inFile)) {
      df <- read.csv(as.character(inFile$datapath),
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote)
      if(input$disp == "head") {
        return(head(df))
      }
      else {
        return(df)
      }
    }
  })


  output$summary <- renderPrint({
    inFile <- parseFilePaths(roots=c(wd='/Users/upendra_35/Documents/CyVerse/Images_apps/DE/VICE/'), input$file)
    if( NROW(inFile)) {
      df <- read.csv(as.character(inFile$datapath),
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote)

      summary(df)
    }
  })

  output$plt <- renderPlot({
    inFile <- parseFilePaths(roots=c(wd='/Users/upendra_35/Documents/CyVerse/Images_apps/DE/VICE/'), input$file)
    if( NROW(inFile)) {
      df <- read.csv(as.character(inFile$datapath),
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote)
      if(input$sho == "yes") {
        manh <- manhattanr(df, chr = "chrom", bp = "pos", p = "P", snp = "marker")
        manhattanly(manh, col=c("#D2691E","#800080","#6495ED","#9ACD32"), point_size=7, showlegend = FALSE, 
                    xlab = "Chromosome", ylab = "-log10(p)", suggestiveline_color = "blue", suggestiveline_width = 2, 
                    genomewideline =FALSE, title = "")
      }
      else {
        print("No plot")
      }

    }
  })

})

当我在 R 控制台中手动运行时,该图的代码肯定有效。

【问题讨论】:

  • 你试过在 R 中渲染吗?

标签: r shiny


【解决方案1】:

您的代码看起来不错。但我相信没有必填字段可以在您选择的文件中获取manhattanly plot。我刚刚尝试使用 manhattanly 包附带的默认 HapMap 数据。只需将该数据导入您的 R 环境并将其作为 csv 文件保存到您的本地计算机。然后为您的测试目的选择相同的文件。 以下是您代码中对我有用的renderPlot 块。

  output$plt <- renderPlot({
    inFile <- parseFilePaths(roots = c(wd = 'Data/'), input$file)
    if (NROW(inFile)) {
      df <- read.csv(as.character(inFile$datapath)
                      header = input$header,
                      sep = input$sep,
                      quote = input$quote
                     )
    if (input$sho == "yes") {
        #manh <- manhattanr(df, chr = "CHR", bp = "pos", p = "P", snp = "SNP", gene = "GENE")
      # manhattanly(manh, col=c("#D2691E","#800080","#6495ED","#9ACD32"), point_size=7, showlegend = FALSE, 
      #             xlab = "Chromosome", ylab = "-log10(p)", suggestiveline_color = "blue", suggestiveline_width = 2, 
      #             genomewideline =FALSE, title = "")
        manhattanly(df, 
                    snp = "SNP", gene = "GENE", 
                    annotation1 = "ZSCORE", annotation2 = "EFFECTSIZE", 
                    highlight = significantSNP)
      }
      else {
      print("No plot")
      }

    }
  })

注意:它在我的浏览器的单独选项卡中绘制。看起来像一个小解决方法应该解决它。

【讨论】:

    【解决方案2】:

    我已经想通了。我使用了错误的函数来打印情节图。由于manhattanly 是plotly 库,我应该在server.R 中使用renderPlotly 而不是renderPlot,在ui.R 中使用plotlyOutput 而不是plotOutput

    【讨论】:

      猜你喜欢
      • 2020-07-13
      • 2016-07-01
      • 2021-12-03
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2014-08-03
      相关资源
      最近更新 更多