无法在Rshiny中显示情节

时间:2018-11-02 04:57:22

标签: r shiny

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

这是我的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控制台中手动运行该绘图时,该绘图的代码肯定可以正常工作。

2 个答案:

答案 0 :(得分:1)

您的代码看起来不错。但是我相信没有必填字段可让您在文件中选择manhattanly。我刚刚尝试了HapMap包随附的默认manhattanly数据。只需将数据导入到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")
      }

    }
  })

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

答案 1 :(得分:0)

我知道了。我使用了错误的功能来打印绘图。由于manhattanlyplotly库,因此我应该在server.R中使用renderPlotly代替renderPlot,而在服务器中使用plotlyOutput代替plotOutput ui.R