【问题标题】:R shiny - Warning: Error in : Only strings can be converted to symbolsR闪亮 - 警告:错误:只有字符串可以转换为符号
【发布时间】: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)

【问题讨论】:

    标签: r function shiny


    【解决方案1】:

    只需删除所有整洁的 eval 并仅使用带有 aes_string() 的字符串:

    scatter_fun<-function(data,xval,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(yval)+
        xlab(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(data[,yval]),
                 label = paste0("R=",round(cor(data[,xval],
                                               data[,yval]),2)),
                 size = 6)
    }
    

    【讨论】:

    • 请注意 aes_string 已被软性弃用,现在建议使用 .data[[input$*]] 代替,参见例如here
    猜你喜欢
    • 2020-12-02
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    相关资源
    最近更新 更多