【发布时间】:2021-02-17 21:53:11
【问题描述】:
我对 R Shiny 比较陌生。我使用 ggplot 创建了一个函数,该函数在 R Shiny 之外运行良好,但在 Shiny 中抛出“只有字符串可以转换为符号”错误。我想知道问题是什么?我已经尝试在函数中将 aes 更改为 aes_string,但仍然没有运气。
我添加一个可重现的例子
library(shiny)
library(shinyWidgets)
library(tidyverse)
scatter_fun<-function(data,xval,yval){
xval <- ensym(xval)
yval <- ensym(yval)
ggplot(data, aes_string(!!xval, !!yval))+
geom_point(alpha=0.25)+
theme(axis.line.x.bottom = element_line("black"),
axis.line.y.left = element_line("black"),
axis.text.x = element_blank(),
legend.title = element_blank(),
panel.background = element_rect(fill = "white",
colour = "black",
size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "gray90"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "gray90"))+ylab(quo_name(yval))+
xlab(quo_name(xval))+
scale_y_continuous(labels = scales::comma)+
expand_limits(y = 0)+
geom_smooth(method="lm", se=FALSE, colour="red")+
annotate("text",
hjust=-0.5,
x=0,
y=max(eval(yval, data)),
label = paste0("R=",round(cor(data[,quo_name(xval)],
data[,quo_name(yval)]),2)),
size = 6)
}
ui <- fluidPage(
titlePanel("Variable testing"),
selectInput("KPI", "Variable 1",
names(mtcars[,2:ncol(mtcars)])),
selectInput("KPI2", "Variable 2",
names(mtcars[,2:ncol(mtcars)])),
mainPanel(plotOutput("plot"),
verbatimTextOutput('mean'))
)
server <- function(input, output) {
output$plot<-renderPlot({
scatter_fun(mtcars,input$KPI, input$KPI2)
})
output$mean <- renderText({"Hello"})
}
shinyApp(ui=ui, server=server)
【问题讨论】: