【发布时间】:2025-12-07 22:10:01
【问题描述】:
背景:这很奇怪。从本质上讲,我正在开发一个闪亮的应用程序,人们可以在其中从特定网站中提取 csv 导出,上传它然后与之交互。因为数字很大(数百万),它默认为科学记数法,这在眼睛上并不容易,所以我试图使用“labels = comma”来纠正这个问题。
问题:当我在 ggplot 函数中同时具有 scale_x_cont 和 scale_y_cont 时,应用程序崩溃。当我只有 x 或 y 时,它运行良好。
现在我尝试编写尽可能小的可重现代码,但是当我使用 mtcars 和相同的 selectInput 方法编写了一个简单的代码时,它运行良好,同时 scale_x_cont 和 scale_y_cont 都没有错误...
错误
eval(substitute(expr), envir, enclos) 中的错误: geom_point 需要以下缺失的美学:x, y 错误:geom_point 需要以下缺失的美学:x, y
要复制的最小 CSV
https://raw.githubusercontent.com/nzcoops/datasets/master/dump_test
应用
require(shiny)
require(DT)
require(ggplot2)
require(scales)
runApp(
list(
ui = fluidPage(
sidebarPanel(fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
htmlOutput("contents2"),
htmlOutput("contents3")
),
mainPanel(
plotOutput("plot1"),
dataTableOutput("contents4")
)
),
server = function(input, output, session) {
contents1 <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
dat <<- read.csv(inFile$datapath)
dat[,2:5] <<- lapply(dat[,2:5],function(x){as.numeric(gsub(",", "", x))})
names(dat)
})
output$contents2 <- renderUI({
if (is.null(input$file1))
return(NULL)
selectInput('columnsx', 'Columns X', contents1()[3:5])
})
output$contents3 <- renderUI({
if (is.null(input$file1))
return(NULL)
selectInput('columnsy', 'Columns Y', contents1()[3:5])
})
output$contents4 <- renderDataTable({
if (is.null(input$file1))
return(NULL)
dat
}, options = list(paging = FALSE, searching = FALSE))
output$plot1 <- renderPlot({
if (is.null(input$file1))
return(NULL)
p <- ggplot(dat, aes_string(x=input$columnsx, y=input$columnsy)) +
geom_point() +
scale_x_continuous(labels = comma) #+ scale_y_continuous(labels = comma)
# Remove the above hash and the presence of scale_y_ will crash app
print(p)
})
}
))
【问题讨论】:
-
只有 ggplot 代码可以工作吗,除了闪亮但有麻烦的数据吗?
-
@tegancp 是的,确实如此,我对此进行了一些测试,尽管它可能是导致问题的“aes_string”部分。