大佬教程收集整理的这篇文章主要介绍了为什么相同的 ggplot 直接工作但在 Shiny 中失败?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我创建一个小数据框并在 Rstudio 中将直接绘图运行到 PLOT 选项卡,然后尝试在 Shiny 中运行相同的概念时,直接绘图有效,并且 Shiny 绘图显示为空白并显示错误消息:
Warning in xy.coords(x,y,xlabel,ylabel,log) :
NAs introduced by coercion
我做错了什么?
我将此代码尝试基于在网络上阅读和 Stack Overflow 中的先前答案,因此很接近,但我错过了一些隐式转换或其他内容。我只想使用 Shiny 绘制数据框的两列。
library(shiny)
dfx <- data.frame(xx=c(1,2,3,4,5),yy=c(2,6,8,10))
plot(dfx$xx,dfx$yy,xlim=c(0,6),ylim=c(0,10))
# the result in PLOTS in Rstudio is a 5 point rising line graph as expected.
ui <- fluIDPage(
headerPanel('my first shiny app'),sIDebarPanel(
SELEcTinput('xcol','X Variable',names(dfX)),SELEcTinput('ycol','Y Variable',names(dfX))
),mainPanel(
plotOutput('plot1')
)
)
server <- function(input,output) {
output$plot1 <- renderPlot({
plot(input$xcol,input$ycol,10))
})
}
shinyApp(ui = ui,server = server)
# the result of this in shiny is a blank graph
在闪亮中,input$xcol
只是一个从 UI 返回的字符串。所以如果你使用这些值,就像调用
plot("xx","yy",xlim=c(0,6),ylim=c(0,10))
返回与 Shiny 相同的错误。
如果您想从 data.frame 中获取值,则需要执行 dfx[[input$xcol]]
。所以试试
plot(dfx[[input$xcol]],dfx[[input$ycol]],10))
当然 plot()
是基本的 R 绘图命令。如果您确实想使用 ggplot
,您可以使用类似
ggplot(dfX) +
aes(.data[[input$xcol]],.data[[input$ycol]]) +
geom_point()
,
所以除了上面提到的双方括号符号解决方案,它是有效的,并在这里解释 davetang.org/muse/2013/08/16/double-square-brackets-in-r –
我想证明还有一个使用“reactive”的解决方案,如下所示,其中“reactive”函数动态查找列名文本字段描述的值,然后(不要忘记!)“如此定义的变量(Xvalues,Yvalues)被视为函数(它们最终成为),因此语法要求在使用它们时在它们之后放置“()”。以下工作:
library(shiny)
df <- data.frame(xx=c(1,2,3,4,5),yy=c(2,6,2))
# the result in PLOTS in Rstudio is a 5 point rising line graph as expected.
ui <- fluidPage(
headerPanel('my first shiny app'),sidebarPanel(
SELEcTinput('xcol','X Variable',names(df)),SELEcTinput('ycol','Y Variable',names(df))
),mainPanel(
plotOutput('plot1')
)
)
server <- function(input,output) {
Xvalues <- reactive({ df[,input$xcol] })
Yvalues <- reactive({ df[,input$ycol] })
output$plot1 <- renderPlot({
plot(Xvalues(),Yvalues(),10))
})
}
shinyApp(ui = ui,server = server)
我发现的一个例子在这里为新手解释
https://campus.datacamp.com/courses/case-studies-building-web-applications-with-shiny-in-r/shiny-review?ex=12
其中规定:
反应性上下文
反应值是 Shiny 中的特殊结构;他们没有被看见 R 编程中的任何其他地方。因此,它们不能仅用于 任何 R 代码,反应值只能在反应性内访问 上下文。
这就是为什么任何依赖于反应值的变量的原因 必须使用reactive()函数创建,否则你会得到 一个错误。闪亮的服务器本身不是响应式上下文,而是 react() 函数,observe() 函数,以及所有的 render*() 功能是。
“Shiny from Rstudio”教程中提供的示例,网址为 https://shiny.rstudio.com/tutorial/ 在可下载内容的第一个案例中,在文件“app.R”中,他们以这种方式使用了“reactive”,但我不明白它在做什么,通过使用reactive一次处理两个数据列向量,这有点模糊!这运行。
# 01-kmeans-app
palette(c("#E41A1C","#377EB8","#4DAF4A","#984EA3","#FF7F00","#FFFF33","#A65628","#F781BF","#999999"))
library(shiny)
ui <- fluidPage(
headerPanel('Iris k-means clustering'),names(iris)),names(iris),SELEcted = names(iris)[[2]]),numericInput('clusters','Cluster count',min = 1,max = 9)
),output) {
SELEctedData <- reactive({
iris[,c(input$xcol,input$ycol)]
})
clusters <- reactive({
kmeans(SELEctedData(),input$clusters)
})
output$plot1 <- renderPlot({
par(mar = c(5.1,4.1,1))
plot(SELEctedData(),col = clusters()$cluster,pch = 20,cex = 3)
points(clusters()$centers,pch = 4,cex = 4,lwd = 4)
})
}
shinyApp(ui = ui,server = server)
以上是大佬教程为你收集整理的为什么相同的 ggplot 直接工作但在 Shiny 中失败?全部内容,希望文章能够帮你解决为什么相同的 ggplot 直接工作但在 Shiny 中失败?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。