【问题标题】:Change plot axis values dynamically from dataframe in Shiny从 Shiny 中的数据框动态更改绘图轴值
【发布时间】:2017-05-30 10:54:08
【问题描述】:

我在项目中使用 Rstudio 和 Shiny。

我定义了一个变量res,它包含具有多行和多列的数据框,然后我制作了一个图,它的 x y 和颜色是来自res 数据框的数据。 我的问题是,当我运行它时,如果我写我希望 x 轴输入变量值(input$SelInp),我没有得到数据框值,而是只得到列名。

如果我更改代码以直接从数据框 (res$some_column_name) 获取值,我会得到正确的值。

ui.R

selectInput("SelInp",
                         label = "Choose one:",
                         choices = colnames(res)
                         )

服务器.R

  output$plt = renderPlot({
                  qplot(data = res,
                     x = input$SelInp, #this only returns a column name 
                     y = res$loan_amnt, # this returns correct values from column loan_amt
                     ylab = "some y axis",
                     xlab = "some x axis",
                     main="Our Chart")
                     }
                     )

所以,我想提前获得input$SelInp 中的值

【问题讨论】:

    标签: r dataframe shiny rstudio


    【解决方案1】:

    我认为原因是 selectInput 将列名作为字符返回。 qplot 需要一个变量。我没有检查 qplot 是否有使用字符来指定比例的选项,但是 ggplot 中的 aes_string 会这样做:

    ui.R

    library(shiny)
    library(ggplot2)
    
    shinyUI(fluidPage(
      titlePanel("Old Faithful Geyser Data"),
      sidebarLayout(sidebarPanel(
        selectInput(
          "selectedCol",
          "Select colum for X axis",
          choices = colnames(mtcars),
          selected = colnames(mtcars)[1]
        )
      ),
      mainPanel(plotOutput("distPlot")))
    ))
    

    服务器.R

    library(shiny)
    library(ggplot2)
    
    shinyServer(function(input, output) {
      output$distPlot <- renderPlot({
        x_axis <- input$selectedCol
        gg <-
          ggplot(mtcars, aes_string(x = x_axis, y = "hp", color = "cyl"))
        gg <- gg + geom_point()
        gg
      })
    })
    

    如果这有帮助,请告诉我。

    【讨论】:

    • 谢谢@Valter,我设法使用您提供的ggplot 示例成功地重新创建了情节,并且它按照我的意愿进行了动态更改,但我仍然无法弄清楚如何使用qplot就像我的例子是
    • 很高兴它有帮助。我确信 qplot 有一个解决方案。我会在一周内查看它并通知您。
    猜你喜欢
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多