通过采用用户选择的变量进行闪亮的 R 应用程序关联

Shiny R App Correlation by Taking User Selected Variables

我希望我的 shiny 应用程序从我的数据集中获取用户选择的变量(在本例中称为“CD”),我希望它打印两件事:相关性和散点图。

首先,这是数据的标题:

 wins kills kdRatio killstreak losses headshots misses scorePerMinute
  <dbl> <dbl>   <dbl>      <dbl>  <dbl>     <dbl>  <dbl>          <dbl>
1     0     0    0             0      0         0      0             0 
2     0     0    0             0      0         0      0             0 
3     0    66    1.03          0      0        16      0             0 
4     3     2    0.4           0      0         0      0             0 
5     0     2    0.2           0      0         1      0             0 
6   684 27011    1.07         18     10      5113 305319           256.

变量名为“变量:

variables = names(CD)

这是我在 ui 方面取得的进展(至少对于此选项卡):

tabPanel("Correlation Metrics",
                         headerPanel("Correlations Between Different COD Stats"),
                         mainPanel(
                           "Now I would like to show some correlation metrics to give a better understanding of which Call of Duty
                           stats really correlate with others. Please select the variables that you are interested and observe
                           which CoD stats correlate!",
                          
                           selectInput('var1', "First Variable", variables),
                           selectInput('var2', "Second Variable", variables, selected = variables[[2]]),
                           plotOutput('correlation')
                        
                         )

这是我在服务器端为此选项卡取得的进展:

output$correlation<- renderPlot({
    x<-as.numeric('var1')
    y<-as.numeric('var2')
    plot(x, y)
  })

我知道我跑题了,但请帮我解决这个问题!

我们可能需要从 input 中对列进行子集化。根据显示的输入数据,列都是numeric,所以提取后不需要再做as.numeric

output$correlation<- renderPlot({
    req(input$var1)
    req(input$var2)
    x <- CD[[input$var1]]
    y <- CD[[input$var2]]
    plot(x, y)
  })

-完整代码

library(shiny)

CD <- structure(list(wins = c(0L, 0L, 0L, 3L, 0L, 684L), kills = c(0L, 
                                                                   0L, 66L, 2L, 2L, 27011L), kdRatio = c(0, 0, 1.03, 0.4, 0.2, 1.07
                                                                   ), killstreak = c(0L, 0L, 0L, 0L, 0L, 18L), losses = c(0L, 0L, 
                                                                                                                          0L, 0L, 0L, 10L), headshots = c(0L, 0L, 16L, 0L, 1L, 5113L), 
                     misses = c(0L, 0L, 0L, 0L, 0L, 305319L), scorePerMinute = c(0, 
                                                                                 0, 0, 0, 0, 256)), class = "data.frame", row.names = c("1", 
                                                                                                                                        "2", "3", "4", "5", "6"))
variables = names(CD)
ui <- fluidPage(
  tabPanel("Correlation Metrics",
           headerPanel("Correlations Between Different COD Stats"),
           mainPanel(
             "Now I would like to show some correlation metrics to give a better understanding of which Call of Duty
                           stats really correlate with others. Please select the variables that you are interested and observe
                           which CoD stats correlate!",
             
             selectInput('var1', "First Variable", variables),
             selectInput('var2', "Second Variable", variables, selected = variables[[2]]),
             plotOutput('correlation')
             
           )
           
))

server <- function(input, output) {
  output$correlation<- renderPlot({
    req(input$var1)
    req(input$var2)
    x <- CD[[input$var1]]
    y <- CD[[input$var2]]
    plot(x, y)
  })
  
}

shinyApp(ui = ui, server = server)

-输出