R 闪亮和ggplot-使用scale_x_和scale_y_连续时出错
背景:这是一个奇怪的例子。基本上,我正在开发一个闪亮的应用程序,人们可以从特定网站上导出csv,上传,然后与之交互。因为数字很大(百万),它默认为科学符号,这在视觉上并不容易,所以我尝试使用“labels=comma”来纠正这一点 问题:当我在ggplot函数中同时使用scale\u x\u cont和scale\u y\u cont时,应用程序崩溃。当我只有x或y时,它运行良好 现在我试着编写尽可能小的可复制代码,但当我使用mtcars和相同的selectInput方法编写一个简单的代码时,它运行良好,scale\u x\u cont和scale\u y\u cont都没有错误 错误 eval中出错(替换(expr)、envir、enclose): geom_point需要以下缺失的美学:x,y 错误:geom_point需要以下缺少的美学:x,y 要复制的最小CSV 应用程序R 闪亮和ggplot-使用scale_x_和scale_y_连续时出错,r,shiny,R,Shiny,背景:这是一个奇怪的例子。基本上,我正在开发一个闪亮的应用程序,人们可以从特定网站上导出csv,上传,然后与之交互。因为数字很大(百万),它默认为科学符号,这在视觉上并不容易,所以我尝试使用“labels=comma”来纠正这一点 问题:当我在ggplot函数中同时使用scale\u x\u cont和scale\u y\u cont时,应用程序崩溃。当我只有x或y时,它运行良好 现在我试着编写尽可能小的可复制代码,但当我使用mtcars和相同的selectInput方法编写一个简单的代码时,它
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)
})
}
))
require(闪亮)
需要(DT)
需要(ggplot2)
需要(天平)
runApp(
名单(
ui=fluidPage(
侧边栏面板(fileInput('file1','选择CSV文件'),
接受=c('text/csv',
'文本/逗号分隔值,文本/普通',
“.csv”),
htmlOutput(“内容2”),
htmlOutput(“内容3”)
),
主面板(
plotOutput(“plot1”),
dataTableOutput(“contents4”)
)
),
服务器=功能(输入、输出、会话){
contents1函数中正在进行一些时髦的范围界定。如果用以下内容替换函数的第一行:
p <- ggplot(dat, aes_string(x=dat[input$columnsx], y=dat[input$columnsy]))
p只做ggplot代码的工作,在闪亮之外,但有麻烦的数据吗?@tegancp是的,我做了一些测试,因为我认为可能是“aes_字符串”部分导致了问题。谢谢@jeremycg。你能解释范围吗,或者你能发现明显的错误吗?我很惊讶这是一个范围问题,因为它工作正常当只有scale_x或scale_y中的一个存在时,这很好吗?您的解决方案的一个问题(我可以确认它是有效的)是,如果在轴上没有得到正确的名称,就会得到可怕的长向量,当然,可以通过添加实验室(x=输入$columnsx)来修复.Hmmm.我真的不明白它为什么不起作用-ggplot在函数中做了一些奇怪的范围界定工作-参见示例-。我猜是aes_字符串解释输入$columnx的方式导致了这个问题。