使用rshiny绘制svm图片

时间:2018-10-23 18:08:49

标签: r shiny rstudio shiny-server

我正在学习Rshiny。我想绘制一张带有两个变量的图片。我不确定如何完成此操作。以下是我的代码:

UI

library(shiny)

# Define UI ----
fluidPage(

fluidRow(

  selectInput("var1", 
           label = h3("Variable1"), 
            choices=c("age",
                        "sex",
                        "cp","
                         chol",
                         "fbs",
                         "exang",
                         "thalach",
                         "restecg",
                         "oldpeak",
                         "trestbps",
                          "class"),
                  selected ="age"),
                 hr()

   ),  

 fluidRow(

   selectInput("var2", 
            label = h3("Variable2"), 
            choices=c("age",
                        "sex",
                        "cp","
                         chol",
                         "fbs",
                         "exang",
                         "thalach",
                         "restecg",
                         "oldpeak",
                         "trestbps",
                          "class"),
                  selected ="cp"),
                  hr()
   ),

mainPanel(
 textOutput("selected_var1"),
 textOutput("selected_var2")
 plotOutput(outputId="img")

 )   

) 

然后是服务器     图书馆(闪亮) 来源('svm.R')

server <- function(input, output){

     dataInput <- reactive({


        if (!input$var1==input$var2){

        output$selected_var1<- renderText({ 
         paste("You have selected", input$var1)

        })


        output$selected_var2<- renderText({ 
        paste("You have selected", input$var2)

          })
       output$img <- renderPlot({

        var1<-input$var1
        var2<-input$var2
       plot(data$var,data$var2,color=c(28,32))
      })

      }else{
     break
     }
  })

 } 


I want to solve two problems now:
 1. How could I got the plotted picture?
 2. I know there are repeat code in the UI part. How could I improve it?

谢谢!    我已经将这些代码推送到了github     链接:https://github.com/testCodeSand/test/tree/master

最后,我在此处附加图片link,以向您展示我要尝试的操作,用户选择两个变量,服务器使用这两个变量绘制图像。我收到的错误消息例如 .getReactiveEnvironment()$ currentContext()中的错误:   没有活动的响应上下文,不允许进行操作。 (您尝试做只能从反应式表达式或观察器内部完成的操作。)

Update: I just added reactive but it did not show any output

2 个答案:

答案 0 :(得分:0)

这是生成示例所需的完整代码,请注意,我们在弹出消息中使用shinyalert。我将在此示例中创建示例数据

 library(shiny)
 library(shinyalert)

# Define UI ----
choices_list<-
c("age",
"sex",
"cp",
"chol",
 "fbs",
"exang",
"thalach",
"restecg",
 "oldpeak",
"trestbps",
"class")

#Test Data
col1<-c(1:5)
col2<-c(5:9)
col3<-c(10:14)
data<-data.frame(col1,col2,col3)
names(data)<-c("age","sex","cp")

#Ui
ui<-

fluidPage(

   fluidRow(

    selectInput("var1", 
                label = h3("Variable1"), 
                choices=choices_list,
                selected ="age"),
    hr()  
  ),  

  fluidRow(

    selectInput("var2", 
                label = h3("Variable2"), 
                choices=choices_list,
                selected ="cp"),
    hr()
  ),

  mainPanel(
    textOutput("selected_var1"),
    textOutput("selected_var2"),
    plotOutput(outputId="img"),
     useShinyalert()

  )   
) 

#Server
 server <- function(input, output){

  var1<-reactive({input$var1})
  var2<-reactive({input$var2})


  output$selected_var1<- renderText({ 
    paste("You have selected", var1())

  })


  output$selected_var2<- renderText({ 
    paste("You have selected", var2())

  })

  output$img <- renderPlot({

    if(var1()!=var2()){
    var1<-data[input$var1]
    var2<-data[input$var2]
    df<-data.frame(var1,var2)
    plot(df)
    }else{shinyalert("Warning","Please select non-duplicate paramaters")}
    })

} 


shinyApp(ui, server)

答案 1 :(得分:0)

UI

图书馆(闪亮)

定义用户界面

fluidPage(

fluidRow(

  selectInput("var1", 
            label = h3("Variable1"), 
            choices=c("age",
                      "sex",
                      "cp",
                      "chol",
                      "fbs",
                      "exang",
                      "thalach",
                      "restecg",
                      "oldpeak",
                      "trestbps",
                      "class"),
             selected ="age"),
   hr()

 ),  

fluidRow(

   selectInput("var2", 
            label = h3("Variable2"), 
            choices=c("age",
                      "sex",
                      "cp",
                      "chol",
                      "fbs",
                      "exang",
                      "thalach",
                      "restecg",
                      "oldpeak",
                      "trestbps",
                      "class"),
             selected ="cp"),
              hr()

),

mainPanel(
  textOutput("selected_var1"),
  textOutput("selected_var2"),
  plotOutput(outputId="img"),
  hr(),
  actionButton("go", "Go"),
  textOutput(outputId="info")

)   

) #

服务器

图书馆(闪亮)  来源('svm.R')

服务器<-功能(输入,输出){

randomVals <- eventReactive(input$go, {
  runif(data[input$var1], data[input$var2])
})


#Assign reactive variables
var1<-reactive({input$var1})
var2<-reactive({input$var2})

#Output reactive variables
output$selected_var1<- renderText({ 
  paste("You have selected", var1())
})
hr()
output$selected_var2<- renderText({ 
  paste("You have selected", var2())
})

output$img <- renderPlot({

    if(var1()!=var2()){
      var1<-data[input$var1]
      var2<-data[input$var2]
      df<-data.frame(var1,var2)
      plot(df,col=c(28,32))
      #plot(randomVals())
}else{
  output$Info<-renderText("Please select a different variable")
 }
})
}