使用带有Shiny App和R

时间:2015-10-14 12:32:25

标签: r shiny subset

我对这个话题很新。我想将闪亮的应用程序与R一起使用来分析我的数据。 这是我的ui:R

ui<-(fluidPage(  
titlePanel("My project"), 
sidebarLayout(
sidebarPanel(
textInput("caption", "Caption:", "Data Summary"),

selectInput("dataset", "Choose a dataset:", 
              choices = c("exp1","exp2", "exp3")),


checkboxInput(inputId = "notgr",
                label = strong("Delete superflous data"),
                value = FALSE),

      ),
mainPanel(
  h3(textOutput("caption", container = span)),
       plotOutput("strip"),
               ))))

这里是server.R

    library(shiny)
    library(datasets)

server<-(function(input, output,clientData, session) {
 df<-read.table("/some data.txt",header=T)
 df_new<-tag[tag$factor2>0.3,] #should only use data with factor  > 0.3 for plotting

 datasetInput <- reactive({
    switch(input$dataset,
           "exp1" = df,
           "exp1" = df2,
           "exp1" = df3)
       })


  output$strip<-renderPlot({
    stripchart(data=datasetInput(),factor2~tag,vertical=TRUE,method="jitter",pch=19,col="red")
    if (input$notgr) {
       df<-renderTable({df_new})
    } 

      })

 shinyApp(ui=ui,server=server)

我的想法是:if if(inputId)&#34; notgr&#34;激活新的data.frame,其值为> 0.3,因为factor2

显然我拥有的版本(使用if)不起作用。没有错误,但绘制了所有数据。 当我使用

时也是如此
if (input$notgr) {
           df<-df[df$factor2>0.3,]
}

 if (input$notgr) {
               datasetInput()<-datasetInput()[datasetInput()$factor2>0.3,]
}

非常感谢你的帮助

1 个答案:

答案 0 :(得分:0)

你可以这样做:

library(shiny)
library(datasets)

ui <- shinyUI( fluidPage( 
    titlePanel("My project"),
    sidebarLayout(
      sidebarPanel(
        textInput("caption", "Caption:", "Data Summary"),
        selectInput("dataset", "Choose a dataset:", 
                    choices = c("exp1","exp2", "exp3")),
        checkboxInput(inputId = "notgr",
                      label = strong("Delete superflous data"),
                      value = FALSE)
      ),
      mainPanel(
        h3(textOutput("caption", container = span)),
        plotOutput("strip")
      )
    )
  ))

server <- shinyServer(function(input, output,clientData, session) {
  # Mockup data
  df  <- data.frame("tag"=runif(100, min=0,    max=0.4),"factor2"=runif(100, min=0, max=0.40))
  df2 <- data.frame("tag"=runif(100, min=0.2,  max=0.6),"factor2"=runif(100, min=0.2, max=0.6))
  df3 <- data.frame("tag"=runif(100, min=0.25, max=0.8),"factor2"=runif(100, min=0.25, max=0.8))

  datasetInput <- reactive({
    switch(input$dataset,
           "exp1" = df,
           "exp2" = df2,
           "exp3" = df3)
  })

  # Listen for events, if checkbox is checked or data set switched, run renderData
  observeEvent(input$notgr,{
    renderData()
  })
  observeEvent(input$dataset,{
    renderData()
  })

  # Function to draw data for client
  renderData <- function(){
    dat <- datasetInput()
    if (input$notgr) {
      dat <- dat[dat$factor2 > 0.3 ,]
    }
    output$strip<-renderPlot({
      stripchart(data=dat, factor2~tag, vertical=TRUE, method="jitter", pch=19, col="red")
    })
  }

})

shinyApp(ui=ui,server=server)