简体   繁体   中英

R Programming Shiny Reactive basics - data types

Thanks all in advance. I'm trying to narrow this down, but I'm new to shiny so I'm having trouble narrowing it down. My main confusion is reactive versus other types of reactive(?) structures.

What I'm trying to do is list the R basic data packages. I have that in a select box. That works. Having selected that, R will show the fields of that data set so the user can accept Y and X values. (I realize some sets aren't formatted for that.) Code listed below gives, "incorrect number of dimensions" in the main panel. Error seems to be somewhere in calling the names. I don't seem to understand how to get reactive elements into the ui after they're creative. Any tips on this?

Again, the list of datasets work. data table is pulled properly. But the names aren't there so they can be selected to build the plot.

library(shiny)
set.seed(10101)

dataPkgs <- data(package='datasets')[[3]]
pkgNames <- dataPkgs[,3]

ui <- fluidPage(
  headerPanel('R Data k-means clustering'),
  sidebarPanel(

    selectInput('choosedata', 'Choose a Data Set',pkgNames,selected="mtcars"),

    helpText("Listing should make available all basic datasets available in base R install.
             Remember many of the data sets will be ill-fit for this type of analysis."),

    selectInput('ycol', 'Select Independent Y', textOutput('selDFNames')),
    selectInput('xcol', 'Select Dependent X', textOutput('selDFNames')),

    numericInput('clusters', '1-7 clusters', 3,
                 min = 1, max = 7)
  ),
  mainPanel(
    plotOutput('plot1')
  )
)


server <- function(input, output, session) {

  selDF <- renderTable({
    get(input$choosedata)
  })

  output$selDFNames <- renderText({
    names(selDF())
  })

  selDFset <- reactive({
    selDF()[, c(input$xcol, input$ycol)]
  })

  clusters <- reactive({
    kmeans(selDFset(), input$clusters)
  })

  output$plot1 <- renderPlot({

    plot(selDFset(),
         col = clusters()$cluster,
         pch = 20, cex = 3)
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })

}

shinyApp(ui = ui, server = server)

Use observe and updateSelectInput update your select input. Here is the server function, it worked well. Good luck.

server <- function(input, output, session) {

  selDF <- reactive({
    get(input$choosedata, envir = as.environment('package:datasets'))
  })

  observe({
    updateSelectInput(session, "ycol", choices = names(selDF()))
  })

  observe({
    updateSelectInput(session, "xcol", choices = names(selDF()))
  })

  selDFset <- reactive({
    selDF()[, c(input$xcol, input$ycol)]
  })

  clusters <- reactive({
    kmeans(selDFset(), input$clusters)
  })

  output$plot1 <- renderPlot({

  plot(selDFset(),
     col = clusters()$cluster,
     pch = 20, cex = 3)
 points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })
}

shinyApp(ui = ui, server = server)

I will add more test cases and catches with my answer but its best if you get the datasets that you know work instead of just asking for all of them:

library(shiny)
set.seed(10101)

dataPkgs <- data(package='datasets')[[3]]
pkgNames <- dataPkgs[,3]

longley[,c("GNP.deflator","GNP.deflator")]

ui <- fluidPage(
  headerPanel('R Data k-means clustering'),
  sidebarPanel(

    selectInput('choosedata', 'Choose a Data Set',pkgNames,selected="mtcars"),

    helpText("Listing should make available all basic datasets available in base R install.
             Remember many of the data sets will be ill-fit for this type of analysis."),

    selectInput('ycol', 'Select Independent Y', choices = NULL),
    selectInput('xcol', 'Select Dependent X', choices = NULL),

    numericInput('clusters', '1-7 clusters', 3,min = 1, max = 7)
    ),
  mainPanel(
    plotOutput('plot1')
  )
)

server <- function(input, output, session) {

  selDF <- reactive({
    data <- tryCatch(get(input$choosedata), error=function(e) NULL)
    if(is.null(data)){
      return()
    }
    data
  })

  observeEvent(selDF(),{
    updateSelectInput(session,"xcol",choices=c(names(selDF())))
    updateSelectInput(session,"ycol",choices=c(names(selDF())))
  })

  selDFset <- eventReactive(c(input$xcol, input$ycol),{
    req(input$xcol)
    req(input$ycol)
    selDF()[,c(input$xcol, input$ycol)]
  })

  clusters <- reactive({
    kmeans(selDFset(), input$clusters)
  })

  output$plot1 <- renderPlot({
    req(selDFset())
    plot(selDFset(),col = clusters()$cluster,pch = 20, cex = 3)
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })
}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM