简体   繁体   中英

R Shiny Bar Plot and input$var issue

There is some issue with the input variable type that is preventing the R Shiny work. The drop down is generated and I can choose the variable, but no plot is generated and I get no error message. If I substitute by the ggplot with the data frame name and field directly (commented one), the bar graph is generated. I have tried the as.character (gives me an error), no conversion or as.factor (give no error, but does not generate the bar plot.

library(shiny)
library(ggplot2)
# DEFINE UI FUNCTION #######################################
ui <- fluidPage(
  titlePanel("Charting GE Data"),
  sidebarLayout(
    sidebarPanel(
      # Dropdown menu for selecting variable from GE data.
      selectInput("var2",
                  label = "Select X Axis Variable",
                  choices = c("Indicator_Type" = 12, "Class" = 13),
                  selected = 13)  # Default selection
    ),
    mainPanel(
      plotOutput("BarPlot")  
    )
  )
)
# DEFINE SERVER FUNCTION ###################################
server <- function(input, output) {
  # Define bar plot output
  output$BarPlot <- renderPlot({  
    iX   <- as.factor(input$var2) #
    xvar    <- Event_identity_noNAsComp[, iX]
    require(graphics)
   g <- ggplot(Event_identity_noNAsComp, aes(xvar, fill = xvar))
  #  g <- ggplot(Event_identity_noNAsComp, aes(Event_identity_noNAsComp$Class, #fill = Event_identity_noNAsComp$Class))
  g + geom_bar()        
  })
}
# CALL THE SHINY APP #######################################
shinyApp(ui = ui, server = server)

Hope this helps...

iX   <- as.numeric(input$var2)

If it is not necessary to provide the column number position, it will work if you provide the column name:

Eg

  # Dropdown menu for selecting variable from GE data.
  selectInput("var2",
              label = "Select X Axis Variable",
              choices = c("Mkt Returns" = "mktreturns", "Mkt Prices" = "mktprices"),
              selected = "mktreturns") 

So for me I used a data set below:

summary(df_bm)

     Date        mktreturns          mktprices   

2013-02-14: 1 Min. :-2095.500 Min. :1890
2013-02-15: 1 1st Qu.: -13.000 1st Qu.:2395
2013-02-18: 1 Median : 0.000 Median :2665
2013-02-19: 1 Mean : -5.361 Mean :2597
2013-02-20: 1 3rd Qu.: 8.000 3rd Qu.:2842
2013-02-21: 1 Max. : 268.000 Max. :3378
(Other) :1007

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM