本文介绍了子集数据框并使用ggplot进行绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个闪亮的应用程序,并且需要一些有关我的数据子集的帮助.我插入 dateRangeInput ,客户端可以在开始日期和结束日期之间进行过滤.此过滤器包含在我的ggplot代码中,因此,在选择其他日期时,该图将始终自动更改.我的问题是,它不会根据所选日期 partC 的数据进行过滤.问题是这行代码: geom_line(aes(x = Date,y = OLS.Data [partC]),color ="red"). partC 是一个连接到selectinput的变量,可以访问我的数据框.示例:客户端选择 input1 =知情 input2 = Full partC 生成 InformedFull (这是一个名称)我的数据集的列)等等.所以 partC 只是两个输入的连接器,这是我的问题.如果我把我的代码放到geom_line中,例如 geom_line(aes(x = Date,y = InformedFull),color ="red"),那么上述所有方法都可以正常使用,但是我需要partC.

I created a shiny app and need some help with the subset of my data. I insert a dateRangeInput where the client can filter between a start and end date. This filter is included into my ggplot code, so that the plot always automatically changes when a different date is selected. My problem is it does not filter based on the selected date, the data of partC. The problem is this line of code: geom_line(aes(x = Date, y = OLS.Data[partC]), color="red"). partC is a variable that connects to selectinputs to have access to my dataframe. Example: Client selects input1 = Informed and input2 = Full, partC makes InformedFull (which is the name of one column of my dataset) and so on. So partC is just a a connector of the two inputs, and this is my problem. If I put into my geom_line this code e.g geom_line(aes(x = Date, y = InformedFull), color="red"), instead the above everything works perfect, but I need it with partC.

这是我的ui.R代码(仅必要的部分):

Here is my ui.R code (only necessary part):

        box(
          title = "Controls-0",
          status = "primary",
          solidHeader = TRUE,
          width = 3,
          height = 142,
          dateRangeInput("daterange", "SELECT DATE:", start = min(OLS.Data$Date), end = max(OLS.Data$Date))
        ),

            box(
              title = "Investor Control",
              status = "primary",
              solidHeader = TRUE,
              width = 3,
              selectInput("investor", label="Select Investor", choices = list("Informed" = "Informed", "Noise" = "Noise"), selected = "Informed")
            ),

            box(
              title = "Category Control",
              status = "primary",
              solidHeader = TRUE,
              width = 3,
              selectInput("category", label="Select Category", choices = list("Full" = "Full", "Fact" = "Fact", "Fact Positive" = "Fact.Pos", "Fact Negative" = "Fact.Neg", "Emotions" = "Emotions", "Emotions Fact" = "EmotionsFact"), selected = "Full")
            ),

使用ggplot更新server.R:

Update server.R with ggplot:

server <- function(input, output) {

  partC = NULL

  makeReactiveBinding("partC")


  observeEvent(input$investor, {
    partA<<-input$investor
    partA<<-as.character(partA)
  })

  observeEvent(input$category, {
    partB<<-input$category
    partB<<-as.character(partB)
  })

  OLS.Data$InformedEmotionsFact <- as.numeric(as.character(OLS.Data$InformedEmotionsFact))
  OLS.Data$NoiseEmotionsFact <- as.numeric(as.character(OLS.Data$NoiseEmotionsFact))

  output$myPlotVisu <- renderPlot({
    partC<-as.character(paste(partA,partB,sep=""))

    OLS.Data %>%
      select(partC, NYSE,Date,Sector) %>%
      filter(Date >= input$daterange[1], Date <= input$daterange[2]) %>%
      ggplot(aes(x = Date, y = NYSE)) +
      geom_line() +
      ggtitle(paste(input$investor,input$category,sep = "")) +
      theme(plot.title = element_text(hjust = 0.5,face="bold")) +
      labs(x="Time",y="Return S&P500") +
      geom_line(aes(x = Date, y = OLS.Data[partC]), color="red")
  })

推荐答案

我不知道为什么将partA/partB分配给全局环境,甚至两次.您不需要这样做.我创建了一个 reactiveValues 对象,用于存储值(partA,partB和partC).然后,您可以在应用程序中的任何位置使用它们.

I dont know why you assign partA/partB to the global environment, and even twice. You dont need to do that. I created an reactiveValues object instead, where you store the values (partA, partB and partC). Then you can use them wherever you want in your app.

也许下面的示例将帮助您编写代码.我为此创建了一些虚拟数据.

Maybe the following example will help you with your code. I created some dummy data for it.

library(shiny)
library(shinydashboard)
library(ggplot2)

## DATA #######################
DateSeq = seq(as.Date("1910/1/1"), as.Date("1911/1/1"), "days")

OLS.Data = data.frame(
  ID = 1:length(DateSeq),
  Date = DateSeq,
  NoiseEmotionsFact = sample(1:100,length(DateSeq), T),
  InformedEmotionsFact = sample(100:1000,length(DateSeq), T),
  InformedFull = sample(10:1000,length(DateSeq), T),
  NoiseFull = sample(50:5000,length(DateSeq), T),
  NoiseFact = sample(1:15,length(DateSeq), T),
  NoiseFact.Pos = sample(100:110,length(DateSeq), T),
  NoiseFact.Pos = sample(10:200,length(DateSeq), T)
)


## UI #######################
ui <- {dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    plotOutput("myPlot"),
    box(
      title = "Controls-0",
      status = "primary",
      solidHeader = TRUE,
      width = 3,
      height = 142,
      dateRangeInput("daterange", "SELECT DATE:", start = min(OLS.Data$Date), end = max(OLS.Data$Date))
    ),
    box(
      title = "Alpha",
      sliderInput("alphaVisu", label = "Alpha :", min = 0, max = 1, value = 0.4, step = 0.1)
    ),

    box(
      title = "Investor Control",
      status = "primary",
      solidHeader = TRUE,
      width = 3,
      selectInput("investor", label="Select Investor",
                  choices = list("Informed" = "Informed", "Noise" = "Noise"), selected = "Informed")
    ),

    box(
      title = "Category Control",
      status = "primary",
      solidHeader = TRUE,
      width = 3,
      selectInput("category", label="Select Category",
                  choices = list("Full" = "Full", "Fact" = "Fact", "Fact Positive" = "Fact.Pos",
                                 "Fact Negative" = "Fact.Neg", "Emotions" = "Emotions",
                                 "Emotions Fact" = "EmotionsFact"), selected = "Full")
    )
  )
)}

## SERVER #######################
server <- function(input, output) {

  ## Reactive Values ############
  parts <- reactiveValues(partA=NULL, partB=NULL, partC=NULL)

  ## Observe Events ############
  observeEvent(input$investor, {
    parts$partA <- as.character(input$investor)
  })
  observeEvent(input$category, {
    parts$partB <- as.character(input$category)
  })

  ## Plot ############
  output$myPlot <- renderPlot({

    parts$partC <- as.character(paste(parts$partA, parts$partB,sep=""))

    OLS.Data.filtered <-  OLS.Data %>%
      filter(Date >= input$daterange[1], Date <= input$daterange[2])

    req(OLS.Data.filtered)

    OLS.Data.filtered %>%
      ggplot(aes(x = Date, y = ID)) +
      geom_line() +
      ggtitle(paste("input$investor","input$category",sep = "")) +
      theme(plot.title = element_text(hjust = 0.5,face="bold")) +
      labs(x="Time",y="Return S&P500") +

      geom_line(aes(x = Date, y = OLS.Data.filtered[parts$partC]), color="red",
                alpha = rep(as.numeric(input$alphaVisu), nrow(OLS.Data.filtered[parts$partC])))
  })
}

shinyApp(ui, server)

这篇关于子集数据框并使用ggplot进行绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:20