简体   繁体   中英

Shiny R: Use input from radio button as argument in by()

I have a simple radio button that allows the user to select "Region" or "Room Size." I would like the input to be passed to the INDICES argument of by(), so that I can run a function for each factor level of the data frame and render the results in a table.

A couple things I have tried are:

 df <- Heating
 output$allmodelsbychar <- renderTable({    
    var <- input$runallmodels

    betas <- by(df, df$var, function(df){as.data.frame(summary(mlogit(depvar ~ ic + oc | 0, data= df, shape = "wide", choice = "depvar", varying = c(3:12)))$CoefTable)}, simplify = T)
    betas <- do.call("rbind", betas)
    betas
  })   
}

And:

 df <- Heating
 output$allmodelsbychar <- renderTable({    

    betas <- by(df, df[ ,input$runallmodels], function(df){as.data.frame(summary(mlogit(depvar ~ ic + oc | 0, data= df, shape = "wide", choice = "depvar", varying = c(3:12)))$CoefTable)}, simplify = T)
    betas <- do.call("rbind", betas)
    betas
  })   
}

And:

df <- Heating
 output$allmodelsbychar <- renderTable({

dfindecies <- reactive({
  if (input$runallmodels == "regiontype") {
    df <- df[, 15]
  }

  if (input$runallmodels == "roomsize") {
    df <- df[, 16]
    return(df)
  }
})

betas <- by(df, dfindecies(), function(df){as.data.frame(summary(mlogit(depvar ~ ic + oc | 0, data= df, shape = "wide", choice = "depvar", varying = c(3:12)))$CoefTable)}, simplify = T)
betas <- do.call("rbind", betas)
betas

I solved the issue by using if eles

server <- function(input, output) {

  df <- Heating
output$allmodelsbychar <- renderTable({

dfindecies <- reactive({
  if (input$runallmodels == "regiontype") {
    df <- df[, 16]
  } else {
    df <- df[, 15]
    return(df)
  }
})

### Do stuff

  }) 



}

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