Can someone explain to me why this code, which works fine in R gives me errors (or simply does not work in Rshiny). After creation of a tibble I want to rename certain variables (actually variables in multiple tibbles, hence the use of a reactive()
enviroment. The new name is based on multiple inputs that are pased together I realize that the solution given here might fix this specifc instance, but for the app I am buidling it would not, as my renaming is not only based on a user input, but on some other events as well. af
This is the normal R code that does work
library(tidyverse)
X1 <- "My"
X2 <- "var"
header <- paste0(X1, X2)
mtcars %>%
as_tibble() %>%
rename(!!header := mpg) %>%
head()
but this does not work in Shiny
library(tidyverse)
library(shiny)
ui <- fluidPage(
textInput("Part_1", "first part var name"),
textInput("Part_2", "second part var name"),
tableOutput("result")
)
server <- function(input, output, session) {
header <- reactive({
paste0(input$Part_1, input$Part_2)
})
output$result <- renderTable({
mtcars %>%
as_tibble() %>%
rename(!!header() := mpg) %>%
head()
})
}
shinyApp(ui, server)