【问题标题】:Subset a reactive dataframe in R在 R 中子集反应性数据帧
【发布时间】:2018-08-25 02:25:16
【问题描述】:

您好,我想查找数据集两列的相关系数。如果我使用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")))

 }) 

}

【问题讨论】:

  • 什么是反应形式?
  • 我的意思是我使用 reactive() 创建了它。我编辑了。
  • @TimBiegeleisen 它来自闪亮,添加了标签
  • @MichaelChirico 我太老了,不知道这个:-)
  • @TimBiegeleisen 不用担心,我最近才知道。很有用:)

标签: r shiny subset


【解决方案1】:

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

  1. 将数据帧存储在 reactiveValues 对象中(参见:Creating a reactive dataframe with shiny apps 以了解非常相似的问题)
  2. 在调用 cor 或其他不能正常处理反应的函数之前,通过 rt.df &lt;- rt() 将反应表达式强制转换为常规 R 变量。

【讨论】:

  • 实际上问题是不同的,我的数据集包括我用 x
猜你喜欢
  • 2018-07-31
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 2018-07-08
相关资源
最近更新 更多