r - 如何在shiny或flexdashboard中制作用户选择的变量的图表?

标签 r shiny plotly flexdashboard

我对 R 还很陌生,我正在尝试组装一个 Flexdashboard,它从用户输入中获取 x 和 y 变量并返回这些值的图表。到目前为止,我可以在下面的代码中使用 ggplotly 生成所需的图表。

output$scatter <-renderPlotly({
  
  cat('input$x=',input$x,'\n')
  cat('input$y=',input$y,'\n')
  p <- ggplot(Merged_data_frame_hcat, aes_string(x=input$x, y=input$y)) +
       geom_point()+
       theme_minimal(base_size = 14) 
  g <- ggplotly(p, source = 'source') %>%
       layout(dragmode = 'lasso',
       margin = list(l = 100),
       font = list(family = 'Open Sans', size = 16))
})

Image with code using ggplotly 然而,我意识到使用 ggplotly,我的 x 轴的定义并不像我使用plot_ly 在仪表板外部绘制相同变量时那样定义。 Image with code using plot_ly() 有没有办法在 Flexdashboard 旁边使用plot_ly?到目前为止,我写了这个,但没有用。顺便说一句,我在这里使用 noquote 因为plot_ly 没有很好地接受作为字符串的输入名称

output$scatter <-renderPlotly({
  
  cat('input$x=',input$x,'\n')
  cat('input$y=',input$y,'\n')
  if (length(input$y) == 2){
     x1 = noquote(input$x)
     y1 =noquote(input$y[1])
     y2 = noquote(input$y[2])
  
   plot_ly(Merged_data_frame_hcat)%>%
     add_lines(x= ~x1,y =~y1, name = "Red") 
     add_lines(x= ~x1, y =~y2, name = "Green")
   }
})

在我忘记之前,这是我的数据框的示例,为了简单起见,我已经减少了

df <-data.frame("Timestamp.Excel_1900."=c("2019-04-01 16:52:51","2019-04-01 16:57:46","2019-04-01 17:02:51","2019-04-01 17:07:46","2019-04-01 17:12:52","2019-04-01 17:17:46"), "Temperature.C."= c(5.2995,5.3155,5.3353,5.3536,5.3770,5.4044), "pH.pH."= c(7.60,7.80,7.96,8.04, 8.09, 8.14))

最佳答案

有多种方法可以实现这项工作。不幸的是,您使用 noquote 的方法不起作用。

  1. 可能最简单的方法是从 df 中提取列并将它们作为向量传递给 plotly,例如x = df[[输入$x]]
  2. 由于 plotly API 使用片面公式,第二种方法是将变量作为公式传递,例如x = as.formula(paste0("~", input$x))
  3. 关注此post您还可以使用base::get,例如x = ~get(input$x)
  4. 关注此post您还可以使用整洁的评估

以下示例 flexdashboard 中说明了所有四种方法:

---
title: "Plotly"
output: flexdashboard::flex_dashboard
runtime: shiny
---

```{r}
library(plotly)
library(rlang)
```

```{r global, include=FALSE}
# load data in 'global' chunk so it can be shared by all users of the dashboard
df <- data.frame("Timestamp.Excel_1900." = c("2019-04-01 16:52:51","2019-04-01 16:57:46","2019-04-01 17:02:51","2019-04-01 17:07:46","2019-04-01 17:12:52","2019-04-01 17:17:46"), "Temperature.C."= c(5.2995,5.3155,5.3353,5.3536,5.3770,5.4044), "pH.pH."= c(7.60,7.80,7.96,8.04, 8.09, 8.14))

```

Column {.sidebar}
-----------------------------------------------------------------------

```{r}
selectInput("x",
  "x",
  choices = names(df),
  selected = "Timestamp.Excel_1900."
)
selectizeInput("y",
  "y",
  choices = names(df),
  selected = c("Temperature.C.", "pH.pH."),
  multiple = TRUE,
  options = list(maxItems = 2)
)
```

Column
-----------------------------------------------------------------------

```{r}
# Pass the data columns as vectors
renderPlotly({
  if (length(input$y) == 2) {
    x1 <- df[[input$x]]
    y1 <- df[[input$y[1]]]
    y2 <- df[[input$y[2]]]

    plot_ly() %>%
      add_lines(x = x1, y = y1, name = "Red") %>%
      add_lines(x = x1, y = y2, name = "Green")
  }
})
```

```{r}
# One-sided formulas
renderPlotly({
  if (length(input$y) == 2) {
    x1 <- input$x
    y1 <- input$y[1]
    y2 <- input$y[2]

    plot_ly(df) %>%
      add_lines(x = as.formula(paste("~", x1)), y = as.formula(paste("~", y1)), name = "Red") %>%
      add_lines(x = as.formula(paste("~", x1)), y = as.formula(paste("~", y2)), name = "Green")
  }
})
```

Column
-----------------------------------------------------------------------

```{r}
# Using base::get
renderPlotly({
  if (length(input$y) == 2) {
    x1 <- input$x
    y1 <- input$y[1]
    y2 <- input$y[2]

    plot_ly(df) %>%
      add_lines(x = ~ get(x1), y = ~ get(y1), name = "Red") %>%
      add_lines(x = ~ get(x1), y = ~ get(y2), name = "Green")
  }
})
```

```{r}
# Using tidy evaluation
renderPlotly({
  if (length(input$y) == 2) {
    x1 <- input$x
    y1 <- input$y[1]
    y2 <- input$y[2]

    eval_tidy(
      quo_squash(
        quo({
          plot_ly(df) %>%
            add_lines(x = ~ !!sym(x1), y = ~ !!sym(y1), name = "Red") %>%
            add_lines(x = ~ !!sym(x1), y = ~ !!sym(y2), name = "Green")
        })
      )
    )
  }
})
```

enter image description here

关于r - 如何在shiny或flexdashboard中制作用户选择的变量的图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63543144/

相关文章:

r - 如何在R Shiny中设置页面宽度?

r - 如何从 R 绘图图中获取坐标

python - 在浏览器中渲染时如何更改绘图图形周围的主体背景?

Javascript - Plotly.js 数字频率顺序

r - 查找包含缺失值的列的名称

r - 袋差(类似于 setdiff() 但不适用于集合)

R Shiny : Save everything, 中的 react 变量和输入但并非每次都如此

python - 如何将 update_layout 边距限制为 Plotly/Dash 中的一个子图?

R:向表达式添加文本

r - 更改ggplot中点的颜色填充和形状