【问题标题】:Plot in shiny appears different in browser [duplicate]闪亮的情节在浏览器中看起来不同[重复]
【发布时间】:2022-02-03 15:34:12
【问题描述】:

我尝试用 Shiny 编写一个简单的应用程序。下面是ui.R

library(shiny)
library(ggplot2)
shinyUI(fluidPage(
  titlePanel("Explore mtcars dataset"),
  sidebarLayout(
    sidebarPanel(
      selectInput("inputDF","Enter Y to plot vs mpg",choices = c('wt','disp','hp'))
    ),
    plotOutput("plot1")
  )
))

下面是服务器。R

shinyServer(function(input, output) {
  
  output$plot1 <- renderPlot({
    g <- ggplot(data=mtcars, aes(x=mpg, y=input$inputDF, col=factor(cyl)))
    g + geom_point() + geom_smooth(method="lm")
  })
})

当我单独运行 ggplot 时,情节看起来很完美。

但是,它看起来像一条在Run App 中运行的直线。这有什么问题?我附上了情节。

提前致谢。

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    最简单的方法是使用aes_string而不是aes来处理来自input$inputDF的字符串输入:

    g <- ggplot(data=mtcars, aes_string(x='mpg', y=input$inputDF, col='factor(cyl)'))
    

    你也可以使用quasinotation / tidy evaluation

    library(rlang)
    g <- ggplot(data=mtcars, aes(x=mpg, y= !! sym(input$inputDF), col=factor(cyl)))
    

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2011-08-07
      • 1970-01-01
      相关资源
      最近更新 更多