在闪亮的应用程序中显示动态图表

时间:2016-07-21 12:05:10

标签: r shiny

我有以下闪亮的代码应用程序。我的server.R文件如下所示:

library(shiny)
library(ggplot2)

#df_filter <- df2[df2$month == input$var,]

shinyServer(function(input, output){

  #Hier output je je plot, let op $myhist is iets wat je teruggeeft
  output$myhist1 <- renderPlot({

    df4 <- df2[df2$month == var, ]

    gg <- ggplot(data = df4, aes(x=names, y = number, fill = kind)) + geom_bar(stat="identity") + coord_flip()
    ggg <- gg + ylim(0,1) + theme(legend.position="left") + xlab("")

    plot(ggg, height = 100, width = 100)

  })

})

UI.R文件看起来像这样

library(shiny)
shinyUI(fluidPage(

  #sample data 1
  names <- c("R", "Python", "Qlikview", "R", "Python", "Qlikview"),
  number <- c(0.4, 0.8, 0.3, 0.3, 0.7, 0.2),
  kind <- c("Programming", "Programming", "Dashboarding","Programming", "Programming", "Dashboarding"),
  month <- c(1,2,3,1,2,3),
  df2 <- data.frame(names, number, kind, month),

  #outlinen title
  titlePanel(title = "227 panel"),
  sidebarLayout(
    sidebarPanel(
      selectInput("var", "Select the month", choices = month)
    ),

    mainPanel((""),
              plotOutput("myhist1))
  )
)) 

但是当我运行我的闪亮时,我没有显示动态图表。谁能告诉我哪里出错了?

1 个答案:

答案 0 :(得分:0)

您输错了 - 您忘了将结束"添加到plotOutput,忘记将变量var定义为var <- input$var,或者只是添加input$访问它。

我还在ui.R之外移动了一个数据框的行,并在("")之后删除了mainPanel,并添加了req uirement,在您选择之后必须显示该情节1,2或3个月。

library(shiny)
library(ggplot2)


# removed all comas at the end of each line 
names <- c("R", "Python", "Qlikview", "R", "Python", "Qlikview")
number <- c(0.4, 0.8, 0.3, 0.3, 0.7, 0.2)
kind <- c("Programming", "Programming", "Dashboarding","Programming", "Programming", "Dashboarding")
month <- c(1,2,3,1,2,3)
df2 <- data.frame(names, number, kind, month)


ui <- shinyUI(fluidPage(
  #outlinen title
  titlePanel(title = "227 panel"),
  sidebarLayout(
    sidebarPanel(
      selectInput("var", "Select the month", choices = month)
    ),
    mainPanel(
      plotOutput("myhist1", width = 500, height = 300) 
      # you forgot to add closing " in myhist1
      # added custom width and height of plotOutput 
  )
))) 


server <-  shinyServer(function(input, output){

  #Hier output je je plot, let op $myhist is iets wat je teruggeeft
  output$myhist1 <- renderPlot({
    req(input$var) # require that input$var is not "" to show the plot

    # you can access the input var by input$var
    var <- as.numeric(input$var) # convert character to numeric

    df4 <- df2[df2$month == var, ] 

    gg <- ggplot(data = df4, aes(x=names, y = number, fill = kind)) + 
            geom_bar(stat="identity") + 
            coord_flip()

    ggg <- gg + ylim(0,1) + theme(legend.position="left") + xlab("")

    #plot(ggg, height = 100, width = 100)
    # change the heigth and width of the plot in ui.R by adding width and height 
    # to plotOutput
    ggg

  })

})
shinyApp(ui, server)