How to plot selected input from selectInput() function in shiny?

半腔热情 提交于 2019-12-25 03:29:14

问题


I am using the following two Input functions code in shiny:

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

and

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

My task is now, if the user select for example "Full" and "Informed" than my code should take the column "InformedFull" from my dataset to print the code. How can I handle this? My DataSet looks like this:

Dataset

I already did the ggplot code, that looks like this:

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

So where I placed the ... their should be based on the booth selection of my selectInput, be the correct column of my dataset.

My UI Boxes looking like this:

  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)
            ),

And my server file:

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)
  })

}

回答1:


Solution found:

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)
      })
  })



回答2:


Pull the inputs to the two, and then paste them together. Once you have that use that to pull the data from your data frame. Input the data as a variable into the plot function.

# 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()

})

Note that you have assigned "inf" as the value for someone selecting "Informed" therefore it will not match your data frame column headers. The easy way to fix this is to set the values of the choices to the names that will be used in the column headers, e.g "Informed"="Informed"

If we use a button

>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


来源:https://stackoverflow.com/questions/50726365/how-to-plot-selected-input-from-selectinput-function-in-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!