【问题标题】:How do I connect fileInput to ggplot in Shiny?如何在 Shiny 中将 fileInput 连接到 ggplot?
【发布时间】:2017-09-28 07:25:06
【问题描述】:

我今天已经为此工作了 6 个多小时,我知道这是一个简单的问题,但我无法弄清楚。我想要的是输入一个文件,然后能够使用多个 selectInput 下拉菜单来更改 ggplot 上的输出。这是我目前所拥有的:

用户界面:

ui = dashboardPage(
  dashboardHeader(title = "Apple Financials"),
  dashboardSidebar(
    fileInput("file1", label = "Upload SAS Data:", accept = ".sas7bdat"),
    selectInput("x", label = "X-Axis Variable", choices = c("Sales", "Cash", "Assets", "Profits", "R&D", "SG&A")),
    selectInput("y", label = "Y-Axis Variable", choices = c("Sales", "Cash", "Assets", "Profits", "R&D", "SG&A"), selected = "R&D"),
    selectInput("scale", label = "Choose the Scale:", choices = c("Levels", "Log 10")),
    radioButtons("model", label = "Choose the Model:", choices = c("Linear Model", "LOESS", "Robust Linear", "None"), selected = "LOESS"),
    checkboxInput("ribbon", label = "Standard Error Ribbon", value = TRUE),
    conditionalPanel(
      condition = "input.model == 'LOESS'",
      sliderInput("span", label = "Span for LOESS", min = 0, max = 1, value = .75)
    )
  ),
  dashboardBody(
    box(width = NULL, height = 415, plotOutput("plots"))
  )
)

服务器:

server = function(input, output) {


   observe({
     data = input$file1
      if(is.null(data))
      return(NULL)

    df = read_sas(data$datapath)
    output$plots = renderPlot({
    ggplot(df, aes(x = input$x, y = input$y)) +
      geom_line()
   })
 })
}

【问题讨论】:

    标签: r rstudio shiny


    【解决方案1】:

    由于输入是字符串,我们需要aes_string in ggplot

    server = function(input, output) {
    
    
      observe({
        data = input$file1
        if(is.null(data))
          return(NULL)
    
        df = read_sas(data$datapath)
        output$plots = renderPlot({
          ggplot(df, aes_string(x = input$x, y = input$y)) +
            geom_line()
        })
      })
    }
    shinyApp(ui, server)
    

    注意:为了演示,我们上传的是 csv 文件而不是 SAS 文件

    【讨论】:

    • 感谢您的回复!当我使用 aes_string 时,它会告诉我对象“销售”不存在。在实际的 SAS 文件中,它被表示为 SALEQ,所以我认为我需要以某种方式让 SALEQ 运行为“Sales?”
    • 更新:我能够渲染图表。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 2017-10-27
    • 2015-11-25
    • 1970-01-01
    • 2018-08-26
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多