Reputation: 449
I am having trouble getting my function to plot the required chart/s in Rshiny. This same function works perfectly fine in Rmarkdown with a ggplot output. Within Rshiny if I utilise the iris dataset, I can get the function to work. However I am having trouble getting the function to work with column names being input using the selectInput function in Rshiny. I have tried to change aes to aes_string in ggplot, but I still get the same error of "Only strings can be converted to symbols".
Below is some reproducible code together with my function:
library(shiny)
library(tidyverse)
library(scales)
library(ggpubr)
scatter_fun<-function(data,xval,yval){
xval <- ensym(xval)
yval <- ensym(yval)
plot_list = list()
corr=list()
for (i in 0:3)
{ data2<-data%>%mutate(x1=lag(!!xval,i),y1=!!yval)
p<-ggplot(data2, aes(x1, y1))+
geom_point(size=3, color="#6724C6")+
theme(axis.line.x.bottom = element_line("black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.y.left = element_line("black"),
panel.grid.major.x = element_blank(),
#axis.text.x = element_blank(),
legend.title = element_blank())+ylab(quo_name(yval))+
xlab(quo_name(xval))+
scale_y_continuous(labels = scales::comma)+
scale_x_continuous(labels = scales::comma)+
expand_limits(y = 0)+
geom_smooth(se=FALSE,method='lm', colour="gray48")+ggtitle(paste0("Lag",i))+
annotate(geom = 'text', label=paste0("R=",round(cor(data2[,"x1"],
data2[,"y1"],
use = "complete.obs",method="spearman" ),2)), x = -Inf, y = Inf, hjust = 0, vjust = 1)
plot_list[[i+1]] =p
q=round(cor(data2[,"x1"],
data2[,"y1"],
use = "complete.obs",method="spearman" ),2)
corr[[i+1]]=q
corr<<-corr
}
return(plot_list)}
dataset<-iris
ui <- fluidPage(
titlePanel("Example"),
fluidRow(
column(7,
selectInput("variable1", "Variable 1:",
choices=names(dataset[1:4]))),
column(7,
selectInput("variable2", "Variable 2:",
choices=names(dataset[1:4])))),
mainPanel(
plotOutput("distPlot")
))
server <- function(input, output) {
output$distPlot <- renderPlot({
#m<-scatter_fun(iris, Petal.Length, Petal.Width)
m<-scatter_fun(dataset, input$variable1, input$variable2)
print(ggarrange(plotlist=m, ncol=2,nrow=3, common.legend = T))
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1
Views: 23
Reputation: 21287
Try this
library(shiny)
library(tidyverse)
library(scales)
library(ggpubr)
scatter_fun<-function(data,xval,yval){
# xval <- ensym(xval)
# yval <- ensym(yval)
plot_list = list()
corr=list()
for (i in 0:3)
{ data2<-data %>% mutate(x1=lag(.data[[xval]],i),y1=.data[[yval]])
p<-ggplot(data2, aes(x1, y1))+
geom_point(size=3, color="#6724C6")+
theme(axis.line.x.bottom = element_line("black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.y.left = element_line("black"),
panel.grid.major.x = element_blank(),
#axis.text.x = element_blank(),
legend.title = element_blank())+ylab(yval) +
xlab(xval)+
scale_y_continuous(labels = scales::comma)+
scale_x_continuous(labels = scales::comma)+
expand_limits(y = 0)+
geom_smooth(se=FALSE,method='lm', colour="gray48")+ggtitle(paste0("Lag",i))+
annotate(geom = 'text', label=paste0("R=",round(cor(data2[,"x1"],
data2[,"y1"],
use = "complete.obs",method="spearman" ),2)), x = -Inf, y = Inf, hjust = 0, vjust = 1)
plot_list[[i+1]] =p
q=round(cor(data2[,"x1"],
data2[,"y1"],
use = "complete.obs",method="spearman" ),2)
corr[[i+1]]=q
corr<<-corr
}
return(plot_list)}
dataset<-iris
ui <- fluidPage(
titlePanel("Example"),
fluidRow(
column(7,
selectInput("variable1", "Variable 1:",
choices=names(dataset[1:4]))),
column(7,
selectInput("variable2", "Variable 2:",
choices=names(dataset[1:4])))),
mainPanel(
plotOutput("distPlot")
))
server <- function(input, output) {
output$distPlot <- renderPlot({
#m<-scatter_fun(iris, Petal.Length, Petal.Width)
m<-scatter_fun(dataset, input$variable1, input$variable2)
print(ggarrange(plotlist=m, ncol=2,nrow=3, common.legend = T))
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1