在 R 中对反应数据帧进行子集

Firmo23

你好我想找到我的数据集两列的相关系数。如果我使用cor(subset(iris, select=c("Sepal.Length")),subset(iris, select=c("Sepal.Width")))

正在找到相关性,但我无法使用我的实际数据集进行子集化,该数据集作为 CSV 文件输入并处于反应式表达式中。

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

那么我如何才能对这种反应形式的数据框进行子集化呢?

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

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


    csvdata
  }) 

如果这可能有助于理解问题,我把我的整个代码放在一起。如果没有正确的 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")))

 }) 

}
早期的

有两种方法可以解决这个问题:

  1. 将数据帧存储在reactiveValues对象中(请参阅:使用闪亮的应用程序创建反应性数据帧以解决非常相似的问题)
  2. 通过rt.df <- rt()调用之前cor或其他不能正常处理反应的函数将反应表达式强制转换为常规 R 变量

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何基于R中的条件对数据帧进行子集

根据R中的某些条件对数据帧进行子集

子集R中的数据帧中的列

索引 R 中的非 NA 值以对 R 中的新数据帧进行子集

使用数据表单单元格选择对 R 中的数据帧进行子集化

R中数据帧的等大小子集

R中的子集数据帧

R中的条件子集数据帧

在 R 中,如何根据向量中的值对数据帧的行进行子集

根据用户选择的索引对 r 中的数据帧中的列进行子集化

什么策略建议按月和年对数据帧进行子集化以获得R中的单个数据帧列表

如何基于基础R中的另一个数据帧对数据帧进行子集

在R中的ggplot中使用相同的列名称对数据帧进行子集和绘图

使用逻辑表达式向量对R中的数据帧进行子集

在 R 中对具有特定条件的数据帧进行子集

通过R中的列内的唯一值对数据帧进行子集

为什么我可以使用3维在R中对数据帧进行子集化?

R - 如何根据条件向量对存储在列表中的所有数据帧进行子集

基于多个字符串的部分匹配,在R数据帧中对行进行子集

如何使用占位符在 r 中对数据帧进行子集化

在 R 中,对 ID 出现多次的行上的数据帧进行子集化

如何像R中的SAS那样按列名对数据帧进行子集化?

创建一个函数来对R中的数据帧进行子集

如何使用选择输入在 r Shiny 中对数据帧进行子集化?

在特定时间范围内对 R 中的数据帧进行子集化

R:子集数据帧时进行循环

在r Shiny中对反应数据帧进行多元回归的最佳方法是什么?

用 R 中数据帧的过滤子集替换数据

在R中的数据帧列表中更改列名称的子集