使用Shiny浏览输入文件后绘制数据

时间:2019-07-11 17:47:10

标签: r shiny

我正在尝试构建执行以下任务的Shiny App:

1)浏览看起来像这样的值文件

Sample x y
     A 1 3
     B 2 1
     C 3 6
     D 4 4

2)浏览第二个信息文件,

Sample Country Status
     A      US     OK
     B      UK     OK
     C      UK   NOPE
     D      US     OK

3)当我按下Submit按钮时,

4)我通过Sample列合并两个文件,

5)并使用ggplot制作散点图,并使用一个下拉菜单使我能够根据信息文件中列的名称为点着色。

使用下面的代码,我面临两个问题:(i)加载文件并按Submit按钮后,什么都没发生,以及(ii)我怎么在selectInput中提及为我的下拉菜单阻止可能的choices的数量(假设我事先不知道)?

library(shiny)
library(ggplot2)


ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            fileInput(
                inputId = "user_value_file",
                label = "Choose a file with numeric values"
            ),
            fileInput(
                inputId = "user_info_file",
                label = "Choose a file with sample info"
            ),
            actionButton(
                inputId = "my_button",
                label = "Submit"
            )
        ),
        mainPanel(
            # browse sample annotation file
            selectInput(
                inputId = "info_col",
                label = "Choose an info to color",
                choices = c("Country", "Status")           # I am cheating here because I know in advance what are the colnames of the info file 
            ),
            # outputs
            plotOutput(
                outputId = "my_scatter_plot"
            )
        )
    )
)


server <- function(input, output) {

    output$contents <- renderTable(
        {
        valueFile <- input$user_value_file
        if (is.null(valueFile))
            return(NULL)

        infoFile <- input$user_info_file
        if (is.null(infoFile))
            return(NULL)

        }
    )

    randomVals <- eventReactive(
        input$goButton,
        {
        my_val <- read.table(valueFile$datapath, header = T, sep = "\t")        
        my_info <- read.table(infoFile$datapath, header = T, sep = "\t")
        df <- merge(my_val, my_info, by="Sample")

        output$my_scatter_plot <- renderPlot(
            {
            ggplot(df, aes_string(x=df$x, y=df$y, color=input$info_col)) + 
            geom_point()
            }
        )
        }
    )
}


shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

要使其正常工作,需要注意以下几点:

  1. 布局中的inputID需要与服务器renderTable参数匹配(例如,input $ goButton应该为input $ my_button)
  2. renderPlot已从eventReactive移出,并调用randomVals以获取数据帧df
  3. 要从用户信息文件中获取选择,请将会话添加到服务器功能和updateSelectInput(请注意,取决于该文件结构,您可能想要执行一些操作,例如删除第一列名称或其他更改)

否则,请保留原样。请告诉我这是否具有您想要的行为。

library(shiny)
library(ggplot2)

ui <- fluidPage(

  sidebarLayout(
    sidebarPanel(
      fileInput(
        inputId = "user_value_file",
        label = "Choose a file with numeric values"
      ),
      fileInput(
        inputId = "user_info_file",
        label = "Choose a file with sample info"
      ),
      actionButton(
        inputId = "my_button",
        label = "Submit"
      )
    ),
    mainPanel(
      # browse sample annotation file
      selectInput(
        inputId = "info_col",
        label = "Choose an info to color",
        choices = NULL      # Get colnames from user_info_file later on    
      ),
      # outputs
      plotOutput(
        outputId = "my_scatter_plot"
      )
    )
  )
)

server <- function(input, output, session) {

  output$contents <- renderTable({
      valueFile <- input$user_value_file
      if (is.null(valueFile))
        return(NULL)

      infoFile <- input$user_info_file
      if (is.null(infoFile))
        return(NULL)
  })

  randomVals <- eventReactive(input$my_button, {
      my_val <- read.table(input$user_value_file$datapath, header = T, sep = "\t")        
      my_info <- read.table(input$user_info_file$datapath, header = T, sep = "\t")
      updateSelectInput(session, "info_col", "Choose an info to color", choices = names(my_info)[-1])
      merge(my_val, my_info, by="Sample")
  })
  output$my_scatter_plot <- renderPlot({
      df <- randomVals()
      ggplot(df, aes_string(x=df$x, y=df$y, color=input$info_col)) + 
        geom_point()
  })
}

shinyApp(ui, server)