Home > OS >  How to change variable name using textInput in a ShinyApp?
How to change variable name using textInput in a ShinyApp?

Time:07-20

I would like the name of the variables to change according to a text typed in textInput.

For example, when I typed "Stack Overflow" in "A1" field, this name ("Stack Overflow") would appear as the new name, instead conj1.

My code:

library(shiny)
library(shinydashboard)

header <- dashboardHeader(title = "Dashboard", titleWidth = 300)

sidebar <- dashboardSidebar(width = 300, 
  sidebarMenu(
    menuItem(text = "Simulador", tabName = "simulador1",icon = icon("dashboard"))
    )
  )

body <- dashboardBody(
  
  column(id = "c1", width = 12, 
         
         textInput(inputId = "ar1", label = "A 1", placeholder = "Digite")
    
  ), 
  
  column(id = "colsimul4", width = 12,
         
         textInput(inputId = "lvl1", value = 1,label = "Nível 1", placeholder = "Digite")
    
  ),
  
  column(width = 12, tableOutput(outputId = "new"))
  
)

server <- function(session, input, output) {
  
  fpred_1 <- function(x) {
    
    x
    
  }
  
  predattr1 <- reactive({
    fpred_1(x = input$ar1)
  })
  
  pred_1 <- reactive({
    fpred_1(x = input$lvl1)
  })
  
  output$new <- renderTable({
    isolate(expr = conj1 <- predattr1())
    experiment <- expand.grid(conj1 = c(pred_1()))
    isolate(expr = experiment)
  })
  
}

ui <- dashboardPage(header, sidebar, body)

shinyApp(ui, server) 

I would like the name of the variables conj to be modified according to what is typed in the field A1

I tried this:

isolate(expr = conj1 <- predattr1())

But doesn't work.

For example, if I typed "Stack Overflow", this name appears instead of conj1.

The values ​​change normally, only the variable names do not.

Edit

I tried that too:

output$new <- renderTable({

  isolate(expr = conj1 <- predattr1())

  x <- names(predattr1())

  experiment <- expand.grid(
    
      colnames(x)[1] = c(pred_1())
    
  )

  expr = experiment

})

Nothing...

CodePudding user response:

library(shiny)
library(shinydashboard)

################################################################################
# UI
################################################################################

# Header
header <- dashboardHeader(title = "Dashboard", titleWidth = 300)

# Sidebar
sidebar <- dashboardSidebar(width = 300,
                            sidebarMenu(menuItem(
                              text = "Simulador",
                              tabName = "simulador1",
                              icon = icon("dashboard")
                            )))

# Body
body <- dashboardBody(
  column(
    id = "c1",
    width = 12,
    
    # Text input 1
    textInput(
      inputId = "ar1",
      label = "A1",
      placeholder = "Digite"
    )
    
  ),
  
  column(
    id = "colsimul4",
    width = 12,
    
    # Text input 2
    textInput(
      inputId = "lvl1",
      value = 1,
      label = "Nível 1",
      placeholder = "Digite"
    )
    
  ),
  
  # Table appears below text inputs in same column/panel
  column(width = 12, tableOutput(outputId = "new"))
  
)

ui <- dashboardPage(header, sidebar, body)

################################################################################
# Server
################################################################################

server <- function(session, input, output) {
  # Create table
  experiment <- reactive({
    df <- expand.grid(req(input$lvl1))
    colnames(df) <- req(input$ar1)
    return(df)
  })
  
  # Render table
  output$new <- renderTable({
    experiment()
  })
}

shinyApp(ui, server) 
  • Related