R 闪亮相关图

R 闪亮相关图,r,shiny,R,Shiny,我正在尝试使用USARREST数据库构建一个简单的闪亮应用程序,显示人口密度与犯罪的3个变量(谋杀、袭击、强奸)之间的相关性,并使用SelectInput更改犯罪变量 下面是ui.R的代码: shinyUI(fluidPage( titlePanel("Violent Crime Rates by US State"), sidebarLayout( sidebarPanel( helpText("A series of plots t

我正在尝试使用USARREST数据库构建一个简单的闪亮应用程序,显示人口密度与犯罪的3个变量(谋杀、袭击、强奸)之间的相关性,并使用SelectInput更改犯罪变量

下面是ui.R的代码:

 shinyUI(fluidPage(
    titlePanel("Violent Crime Rates by US State"),

    sidebarLayout(
        sidebarPanel(
            helpText("A series of plots that display correlation between density population and various kind of crimes"),

            selectInput("var", 
                        label = "Choose a crime",
                        choices = c("Murder"=1, "Assault"=2,
                                       "Rape"=4),
                        selected = "Murder")


            ),

        mainPanel(plotOutput('crimeplot'))
    )
))
和服务器

shinyServer(function(input,output){


output$crimeplot<- renderPlot({
    x<-as.numeric(input$var)
    y<-as.numeric(USArrests$UrbanPop)

    plot(x, y, log = "xy")




})
shinyServer(功能(输入、输出){

output$crimeplot我在下面的代码中修复了几个小错误

  • 您的选择返回列号(作为字符串),您需要将其转换为数字,并从
    server.R
    中的数据框中提取相关列
  • ui.R
    中所选
    的默认值应为起始值,而不是标签
更新后的
server.R
如下所示

shinyServer(function(input,output){
                output$crimeplot<- renderPlot({
                    x<-as.numeric(USArrests[,as.numeric(input$var)])
                    y<-as.numeric(USArrests$UrbanPop)
                    plot(x, y, log = "xy", xlab=colnames(USArrests)[as.numeric(input$var)], ylab="Urban Pop")
                })
            })

亲爱的ekstroem,很有效!非常感谢您的帮助和解释!
shinyUI(fluidPage(
    titlePanel("Violent Crime Rates by US State"),    
    sidebarLayout(
        sidebarPanel(
            helpText("A series of plots that display correlation between density population and various kind of crimes"),

            selectInput("var",
                        label = "Choose a crime",
                        choices = c("Murder"=1, "Assault"=2,
                            "Rape"=4),
                        selected = 1)
            ),

        mainPanel(plotOutput('crimeplot'))
        )
    ))