简体   繁体   English

在R中子类化反应数据帧

[英]Subset a reactive dataframe in R

Hello I want to find the correlation coefficient of two columns of my dataset. 您好,我想找到数据集两列的相关系数。 If I use cor(subset(iris, select=c("Sepal.Length")),subset(iris, select=c("Sepal.Width"))) 如果我使用cor(subset(iris, select=c("Sepal.Length")),subset(iris, select=c("Sepal.Width")))

the correlation is being found but I cannot subset with my actual dataset which comes as a CSV file input and is in a reactive expression. 正在找到相关性,但我无法将我的实际数据集作为CSV文件输入来提供,并且处于反应式表达式中。

cor(subset(rt(), select=c("Sepal.Length")),subset(rt(), select=c("Sepal.Width")))`

So how could I subset a data frame of this reactive form? 那么我该如何对这种反应形式的数据框架进行子集化呢?

rt<-reactive({
    req(input$file1)

    csvdata <- read.csv(input$file1$datapath,
                        header = input$header
    )


    csvdata
  }) 

I put my whole code if that may help understand the question.It cannot work without the proper csv but it works if you replace rt() with iris dataset. 如果可以帮助我理解这个问题,我会写完整的代码。没有适当的csv,它将无法正常工作,但如果您将rt()替换为iris数据集,它将起作用。

#ui.r
library(shiny)
library(ggplot2)
library(plotly)
library(extrafont)

fluidPage(

  # App title ----
  titlePanel(div("CROSS CORRELATION",style = "color:blue")),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Input CSV-File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

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

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

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


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

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





    ),
    # Main panel for displaying outputs ----
    mainPanel(

      tabsetPanel(type = "tabs",
                  tabPanel("Table",
                           shiny::dataTableOutput("contents")),
                  tabPanel("Correlation Plot",
                           fluidRow(
                             column(3, uiOutput("lx1")),
                           column(3,uiOutput("lx2"))),
                           hr(),
                           fluidRow(
                             tags$style(type="text/css",
                                        ".shiny-output-error { visibility: hidden; }",
                                        ".shiny-output-error:before { visibility: hidden; }"
                             ),
                           column(3,uiOutput("td")),
                           column(3,uiOutput("an"))),
                           plotlyOutput("sc"))
      ))
  ))
#server.r
function(input, output) {
  rt<-reactive({
    req(input$file1)

    csvdata <- read.csv(input$file1$datapath,
                        header = input$header
    )


    csvdata
  }) 

  output$contents <- shiny::renderDataTable({

    rt()
  })


  output$lx1<-renderUI({
    selectInput("lx1", label = h4("Select 1st Expression Profile"), 
                choices = colnames(rt()[,4:15]), 
                selected = "Lex1")
  })
  output$lx2<-renderUI({
    selectInput("lx2", label = h4("Select 2nd Expression Profile"), 
                choices = colnames(rt()[,4:15]), 
                selected = "Lex2")
  })

  output$td<-renderUI({
    radioButtons("td", label = h3("Trendline"),
                 choices = list("Add Trendline" = "lm", "Remove Trendline" = ""), 
                 selected = "")
  })

  output$an<-renderUI({

    radioButtons("an", label = h3("Correlation Coefficient"),
                 choices = list("Add R^2" = cor(subset(rt(), select=c(input$lx1)),subset(rt(), select=c(input$lx2))), "Remove R^2" = ""), 
                 selected = "")
  })   


 output$sc<-renderPlotly({

   p1 <- ggplot(rt(), aes_string(x = input$lx1, y = input$lx2))+
     # Change the point options in geom_point
     geom_point(color = "darkblue") +
     # Change the title of the plot (can change axis titles
     # in this option as well and add subtitle)
     labs(title = "Cross Correlation") +
     # Change where the tick marks are
     scale_x_continuous(breaks = seq(0, 80000, 10000)) +
     scale_y_continuous(breaks = seq(0, 120000, 20000)) +
     # Change how the text looks for each element
     theme(title = element_text(family = "Calibri", 
                                size = 10, 
                                face = "bold"), 
           axis.title = element_text(family = "Calibri Light", 
                                     size = 16, 
                                     face = "bold", 
                                     color = "darkgrey"), 
           axis.text = element_text(family = "Calibri", 
                                    size = 11))+
     theme_bw()+
     geom_smooth(method = input$td)+
     annotate("text", x = 50000, y = 50000, label = as.character(input$an))
   ggplotly(p1) %>%
     layout(hoverlabel = list(bgcolor = "white", 
                              font = list(family = "Calibri", 
                                          size = 9, 
                                          color = "black")))

 }) 

}

There are two ways of solving this: 有两种解决方法:

  1. Store dataframes inside reactiveValues objects (see: Creating a reactive dataframe with shiny apps for quite similar issue) 内部存储dataframes reactiveValues对象(见: 创建具有光泽的应用程序的反应数据帧相当类似的问题)
  2. Coerce reactive expression to regular R variable via rt.df <- rt() before calling cor or other functions that don't handle reactives gracefuly. 在调用cor或其他无法正常处理反应式的函数之前,通过rt.df <- rt()反应式表达式强制转换为常规R变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM