闪亮:动态更改ggplot2中使用的列

Wit*_*dow 5 ggplot2 shiny

我尝试创建一个Shiny应用程序,您可以在其中选择ggplot每个“ selectizeInput” 的x轴。

我知道Gallery示例,在这里可以通过预选所需的列来解决。因为我可能会动态更改x =属性,所以我希望数据结构有点复杂aes()。

为了更好地理解,我添加了一个最小的工作示例。不幸的是,ggplot使用输入作为值,而是使用相应的列。

library(shiny)
library(ggplot2)


# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(

   # Application title
   titlePanel("Select x Axis"),       

   sidebarLayout(
      sidebarPanel(
        selectizeInput("xaxis", 
                       label = "x-Axis",
                       choices = c("carat", "depth", "table"))            
      ),          

      mainPanel(
         plotOutput("Plot")
      )
   )
))

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

   output$Plot <- renderPlot({
     p <- ggplot(diamonds, aes(x = input$xaxis, y = price))
     p <-p + geom_point()
     print(p)
   })
})

# Run the application 
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

Axe*_*man 6

aes使用NSE(非标准评估)。这对于交互使用非常有用,但对于编程却不是那么好。因此,有两种SE(标准评估)替代方法aes_(以前为aes_q)和aes_string。第一个使用带引号的输入,第二个使用字符串输入。在这种情况下,使用可以很容易地解决问题aes_string(因为selectizeInput无论如何都会给我们一个字符串)。

ggplot(diamonds, aes_string(x = input$xaxis, y = 'price')) +
  geom_point()
Run Code Online (Sandbox Code Playgroud)