簡體   English   中英

R閃亮包中的ggplot錯誤

[英]ggplot error in R shiny package

我剛剛學習了R Shiny軟件包,對於本課程中的一項練習,我們必須創建一個應用程序,該應用程序在側邊欄中具有兩個下拉菜單,在主面板中具有一個ggplot2圖

我幾乎弄清楚了大部分R代碼,但是在繪制過程中出現了錯誤( object 'input' not found )。 有人可以指出我做錯了什么嗎?

ui.R

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Demonstration of 2 dropdown menus"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput("element_id1", "select variable for x-axis", c("mpg", "cyl", "disp", "hp", "wt"), selected = "wt"),
      selectInput("element_id2", "select variable for x-axis", c("mpg", "cyl", "disp", "hp", "wt"), selected = "mpg")
    ),

    # Show a plot of the generated distribution
    mainPanel(
      textOutput("id1"),
      textOutput("id2"),
      plotOutput("plt")
    )
  )
))

server.R

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {
  output$id1 <- renderText({
    sprintf("You have selected %s on the x-axis", input$element_id1)
})
  output$id2 <- renderText({
    sprintf("You have selected %s on the y-axis", input$element_id2)
  })
  output$plt <- renderPlot(
    ggplot(mtcars, aes(x = input$element_id1, y = input$element_id2)) + geom_point()
  )
})

您要提供字符變量來提及ggplot中的軸。 因此,在構建圖形時需要使用aes_string

ggplot(mtcars, aes_string(x = input$element_id1, y = input$element_id2)) + geom_point()

虛擬示例:

df = data.frame(x=1:3, y=c(4,5.6,1))

ggplot(df, aes(x=x, y=y)) + geom_line()
ggplot(df, aes_string(x='x', y='y')) + geom_line()

兩者提供相同的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM