[英]How to make a plotly chart of variables selected by a user in shiny or flexdahsboard?
I am pretty new to R and I am trying to put together a flexdashboard that takes in an x and y variables from user inputs and returns a graph of those values.我对 R 很陌生,我正在尝试组合一个 flexdashboard,它从用户输入中接收 x 和 y 变量并返回这些值的图形。 So far I am able to generate the desired graphs using ggplotly in the code below.到目前为止,我能够在下面的代码中使用 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))
})
However, I realized with ggplotly my x axis was not as defined as when I used plot_ly to graph the same variables outside of the dashboard.但是,我意识到使用 ggplotly 时,我的 x 轴不像我使用 plot_ly 在仪表板外绘制相同变量时那样定义。 Is there a way to use plot_ly iside a flexdashboard.有没有办法在 flexdashboard 旁边使用 plot_ly。 So far I wrote this but that did not work.到目前为止,我写了这个,但那没有用。 BTW I am using noquote here because plot_ly did not take well the input names which were strings顺便说一句,我在这里使用 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")
}
})
Before I forget, Here is an example of my data frame that I have reduced for the sake of simplicity在我忘记之前,这是我为简单起见而减少的数据框示例
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))
There are several approaches to make this work.有几种方法可以完成这项工作。 Unfortunaetly your approach using noquote
does not work.不幸的是,您使用noquote
的方法不起作用。
plotly
as vectors, eg x = df[[input$x]]
可能最简单的方法是从 df 中提取列并将它们作为向量传递给plotly
,例如x = df[[input$x]]
plotly
API works with one-sided formula a second approach would be to pass the variables as formulas, eg x = as.formula(paste0("~", input$x))
由于plotly
API 使用单边公式,第二种方法是将变量作为公式传递,例如x = as.formula(paste0("~", input$x))
base::get
, eg x = ~get(input$x)
在这篇文章之后,您还可以使用base::get
,例如x = ~get(input$x)
All four approaches are illustrated in the following example flexdashboard:以下示例 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")
})
)
)
}
})
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.