如何根据我选择的列绘制rshiny

时间:2018-09-05 20:47:59

标签: r shiny

我制作了一个闪亮的应用程序,它可以接收任何数据并根据数据显示列名。

c1 <- rnorm(10,0,1)
c2 <- c(rep("txA",5),rep("txB",5))
c3 <- c(1:4,1:4,1:2)
c4 <- rep(LETTERS[1:5],2)
mydata <- data.frame(c1,c2,c3,c4)

ui <- fluidPage(
fileInput(inputId = "file",
        label = "import file"),
tableOutput("tb"),
sidebarLayout(
sidebarPanel(
uiOutput(outputId = "aa")
),
mainPanel(textOutput("a"),
          verbatimTextOutput("info"),
          verbatimTextOutput("summary"),
          plotOutput("plot", click = "plot_click")
)
          )
)


server <- function(input,output) {



output$aa <- renderUI({
validate(need(input$file != "", ""))
mydata <- read.csv(input$file$datapath)
selectInput(inputId = "aa", #can be any name?
            label="Select:",
            choices = colnames(mydata))
})

output$tb <- renderTable({
data <- input$file
if (is.null(data))return()
read.table(data$datapath,sep=",")
})

output$summary <- renderPrint({
summary(mydata)
})

output$plot <- renderPlot({
plot(mydata)
})

output$info <- renderText({
paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
})


}
shinyApp(ui=ui, server=server)

如果运行此命令,则会得到以下信息:

the following

我正在尝试制作一个闪亮的应用程序,该程序根据我选择的列显示基本的情节。我该怎么做?

1 个答案:

答案 0 :(得分:0)

类似的操作,请确保取消注释文件输入

library(shiny)

c1 <- rnorm(10,0,1)
c2 <- c(rep("txA",5),rep("txB",5))
c3 <- c(1:4,1:4,1:2)
c4 <- rep(LETTERS[1:5],2)
mydata <- data.frame(c1,c2,c3,c4)


ui <- fluidPage(
  fileInput(inputId = "file",
            label = "import file"),
  tableOutput("tb"),
  sidebarLayout(
    sidebarPanel(
      uiOutput(outputId = "aa")
    ),
    mainPanel(textOutput("a"),
              verbatimTextOutput("info"),
              verbatimTextOutput("summary"),
              plotOutput("plot", click = "plot_click")
    )
  )
)

server <- function(input,output) {

  output$aa <- renderUI({
    #validate(need(input$file != "", ""))
    #mydata <- read.csv(input$file$datapath)

    ## Since your  output$aa already has name aa you cant use it twice!
    selectInput(inputId = "aa2", #can be any name?
                label="Select:",
                choices = colnames(mydata))
  })

  output$tb <- renderTable({
    data <- input$file
    if (is.null(data))return()
    read.table(data$datapath,sep=",")
  })

  mysubsetdata <- eventReactive(input$aa2,{
    mydata[[input$aa2]]
  })


  output$summary <- renderPrint({
    summary(mysubsetdata())
  })

  output$plot <- renderPlot({
    plot(mysubsetdata())
  })

  output$info <- renderText({
    paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
  })
}
shinyApp(ui=ui, server=server)

enter image description here

  • 添加了eventReactive以收听selectInput
  • 所有小部件必须具有唯一的ID,因此您不能使用aa两次,一次用于renderui,一次用于selectInput