如何在selectInput()函数中绘制选定的输入?

时间:2018-06-06 17:35:28

标签: r input shiny data-analysis

我在闪亮中使用以下两个输入函数代码:

selectInput("categoryVisu", label="SELECT CATEGORY", choices = list("Full" = "full", "Fact" = "fact", "Fact Positive" = "factpos", selected = "full", multiple = TRUE)

selectInput("investerVisu", label="SELECT INVESTOR", choices = list("Informed" = "inf", "Noise" = "noise"), selected = "inf", multiple = TRUE)

我的任务现在是,如果用户选择例如“完整”和“知情”,那么我的代码应该从我的数据集中选择“InformedFull”列来打印代码。我怎么处理这个?我的DataSet如下所示:

Dataset

我已经完成了ggplot代码,如下所示:

ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=...))+geom_line()

所以我放置了......它们应该基于我的selectInput的booth选择,是我的数据集的正确列。

我的UI框看起来像这样:

  tabItem(tabName = "visu",
          fluidRow(
            box(
              title = "Controls-1", 
              status = "primary", 
              solidHeader = TRUE,
              width = 3,
              selectInput("categoryVisu", label="SELECT CATEGORY", choices = list("Full" = "Full", "Fact" = "Fact", "Fact Positive" = "Fact.Pos", "Fact Negative" = "Fact.Neg", "Emotions" = "Emotions", "Emotions Fact" = "EmotionsFact"), selected = "Full", multiple = TRUE)
            ),

            box(
              title = "Controls-2", 
              status = "primary", 
              solidHeader = TRUE,
              width = 3,
              selectInput("investerVisu", label="SELECT INVESTOR", choices = list("Informed" = "Informed", "Noise" = "Noise"), selected = "Informed", multiple = TRUE)
            ),

我的服务器文件:

server <- function(input, output) {

  observeEvent(input$categoryVisu,{

    partA<-input$catagoryVisu
    partA<-as.character(partA)    

  })

  observeEvent(input$investerVisu,{

    partB<-input$investerVisu
    partB<-as.character(partB)

  })

  partC<-paste(partB,partA)

  DataFus<-OLS.Data$partC

  output$myPlot <- renderPlot({
    ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=OLS.Data$NYSE))+geom_line()+geom_line(data = OLS.Data,aes(x=OLS.Data$Date, y=DataFus),color="red")+geom_line(alpha = input$alphaVisu)
  })

}

2 个答案:

答案 0 :(得分:1)

找到解决方案:

server <- function(input, output) {

  partA = NULL
  partB = NULL

  makeReactiveBinding("partA")
  makeReactiveBinding("partB")

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

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

  observe({
    PartC<-as.character(paste(partB,partA,sep=""))
    print(PartC)
      output$myPlot <- renderPlot({
        ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=OLS.Data$NYSE))+geom_line()+geom_line(data = OLS.Data,aes(x=OLS.Data$Date, y=OLS.Data[PartC]),color="red")+geom_line(alpha = input$alphaVisu)
      })
  })

答案 1 :(得分:0)

将输入拉到两者,然后将它们粘贴在一起。一旦你用它来从数据框中提取数据。将数据作为变量输入到绘图函数中。

# If you have a button simply observeEvent(input$button, { and put this all 
# under that environment.

partA<-reactive({input$categoryVisu

})

partB<-reactive({input$investerVisu

})


#paste together to get full column name

PartC<-reactive(paste(partA(),partB(),sep=""))

# Pull column from data frame OR put it directly into ggplot argument
Data<-OLS.Data[PartC]

output$myplot<-renderPlot({

#use column as Y-Axis series
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=OLS.Data[PartC]))+geom_line()

})

请注意,您已指定&#34; inf&#34;作为某人选择&#34; Informed&#34;因此它与您的数据框列标题不匹配。解决这个问题的简单方法是将选项的值设置为将在列标题中使用的名称,例如&#34; Informed&#34; =&#34; Informed&#34;

如果我们使用按钮

>ui
#Add button to ui.R
actionButton("go","Reload")

>server
observeEvent(input$go, {

#Grab input to categoryVisu
partA<-input$categoryVisu
partA<-as.character(partA)

#Grab input to investerVisu
partB<-input$investerVisu
partB<-as.character(partB)

#paste together to get full column name
PartC<-paste(partA,partB,sep="")

# Pull column from data frame OR put it directly into ggplot argument
Data<-OLS.Data[PartC]

output$myplot<-renderPlot({

#use column as Y-Axis series
ggplot(data = OLS.Data, aes(x=OLS.Data$Date, y=OLS.Data[PartC]))+geom_line()

}) #Closes plot

}) #Closes observing of action button