如何解决如何使用闪亮的输入/输出读取向量?
我正在使用线性回归制作一个闪亮的应用程序。我的输入/输出函数有问题,所以我使用 SelectInput 因为我想让用户可以选择 de variable 进行比较。但问题是输入/输出不采用来自 csv 文件的值。我该如何解决?
代码是:
datos <- read.csv("C:/Users/jerez/OneDrive/Escritorio/UAL/Datos1.csv",sep=";",header = TRUE,stringsAsFactors = FALSE)
ui <- fluidPage(
headerPanel("Analisis de Regresion"),sidebarPanel(
#Eleccion de variable x
p("Selecciona la primera variable a comparar"),selectInput(inputId = "varX",label = "Variable eje X",multiple = FALSE,choices = list("NumBusquedas","FrecuenciaAsistente","FrecuenciasCorreos","NumMensajes","Freqdiscover","NumUsosApp","Numpublicidad","NumSuscripciones","NumBusquedasYoutube")),#Eleccion de variable y
p("Selecciona la segunda variable a comparar"),selectInput(inputId = "vary",label = "Variable eje Y","NumBusquedasYoutube"))
),mainPanel(
#verbatimtextoutput(outputId = "Datos"),verbatimtextoutput(outputId = "X"),verbatimtextoutput(outputId = "Y"),verbatimtextoutput(outputId = "coefCorr"),plotOutput(outputId = "Grafico")
)
)
server <- function(input,output) {
output$coefCorr <- renderPrint(cor(input$varX,input$vary))
output$Grafico <- renderPlot(ggplot(datos,aes(input$varX,input$vary)))
output$X <- renderPrint({input$varX})
output$Y <- renderPrint({input$vary})
}
# Run the application
shinyApp(ui = ui,server = server)
结果如下: result
谢谢! :)
解决方法
两个问题:
-
aws ec2 terminate-instances
变量是input$*
,而不是您认为的数字。使用character
。 -
类似
datos[[input$*]]
;指定美学的首选程序化方式是通过ggplot
代替。 (我之前建议首选.data[[ input$* ]]
,现在是 deprecated。感谢 @starja 帮助我看到这一点。)
如何解决 下次进入此绑定时:将 aes_string
插入到导致问题的块的开头的某处。 (另一种方法是使用我在此答案底部包含的技术。)现在,我会选择:
browser()
(由于我没有您的数据,我将从 output$coefCorr <- renderPrint({
browser()
cor(input$varX,input$var)
})
开始,并将两个选择输入更改为 datos <- mtcars
。)
当您运行该应用程序时,它应立即进入控制台上的调试器,等待执行下一行代码 (choices=names(datos)
)。幸运的是,我们在一个 cor(...)
中,它将 sink(.)
所有的输出。虽然这是设计使然,但在我们停止 renderPrint
ing 之前,我们将在控制台上实现零交互。为此,sink
停止它。
sink(NULL)
在两个字符串上运行 sink(NULL)
input$varX
# [1] "mpg"
input$varY
# [1] "mpg"
cor("mpg","mpg")
# Error in cor("mpg","mpg") : 'x' must be numeric
关系有意义吗?您可能需要的是 cor
:
datos[[input$varX]]
当然这是完美的“1”,这次都是同一个变量。为了演示,我将退出调试器,将 Y 变量更改为 cor(datos[[input$varX]],datos[[input$varY]])
# [1] 1
,然后重新进入调试器:
"disp"
这解决了 cor(datos[[input$varX]],datos[[input$varY]])
# [1] -0.8475514
错误。
但是,一旦您到了尝试绘图的地步,您就会发现您遇到了另一个问题。 (我将继续使用 numeric
内的当前调试器,只是因为它很方便。)我将添加 renderPrint
以便显示一些内容。
geom_point()
这只是一点。两个轴都是具有值 ggplot(datos,aes(input$varX,input$varY)) + geom_point()
和 "mpg"
的分类变量。在这种情况下,我们用 "disp"
对抗 ggplot2
的非标准评估。相反,告诉 ggplot 你给它strings,用
aes()
最重要的是,这两个服务器块应该是这样的:
ggplot(datos,aes(.data[[ input$varX ]],.data[[ input$varY ]])) + geom_point()
(我仍在推断 output$coefCorr <- renderPrint({ cor(datos[[input$varX]],datos[[input$varY]]) })
output$Grafico <- renderPlot(ggplot(datos,.data[[ input$varY ]])) + geom_point())
,不过这只是为了演示。)
附注:在学习和开发闪亮的应用程序时,我经常插入一个按钮和 geom_point
只是为了让我直接访问,而不是在 observe
块中。例如,
render
当您遇到问题并且不想停止闪亮的应用程序只是为了插入 ui <- fluidPage(
headerPanel("Analisis de Regresion"),sidebarPanel(
actionButton("debug","DEBUG!"),# ...
),mainPanel(
# ...
)
)
server <- function(input,output) {
observeEvent(input$debug,{ req(input$debug); browser(); 1; })
# ...
}
并重新运行它时,只需按下按钮。
(这不应该部署到闪亮的服务器,它只与本地模式相关。事实上,我相信尝试使用 browser()
部署应用程序应该触发警告,如果不是更多。无论哪种方式,不要尝试使用远程服务器上的调试按钮:-)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。