Home > Net >  R shiny app reads data with parentheses in attribute name
R shiny app reads data with parentheses in attribute name

Time:01-06

i have a problem with following app, my goal is to analyze data, from input dataset, which has a attributes name including parentheses. When i try to work with that attribute later, it´s looking for function with this name.

I dont have this problem in basic R code, where R replace the parentheses with dots - Temperature.F. instead of Temperature(F). But in R Shiny i cannot find a solution.

I use this dataset: https://www.kaggle.com/sobhanmoosavi/us-accidents.

So my goal is to show numeric graph of Temperature(F), however it´s looking for function with that name, based on parentheses.

library(shiny)
library(shinythemes)
library(data.table)
library(ggplot2)

not_sel <- "Not Selected"

about_page <- tabPanel(
    title = "About",
    titlePanel("About"),
    "Created with R Shiny",
    br(),
    "2021 April"
)

main_page <- tabPanel(
    title = "Analysis",
    titlePanel("Analysis"),
    sidebarLayout(
        sidebarPanel(
            title = "Inputs",
            fileInput("csv_input", "Select CSV File to Import", accept = ".csv"),
            selectInput("num_var", "Numeric Variable ", choices =  c( "Temperature(F)" = "Temperature(F)",
                                                                      "Temperature_C" = "Temperature_C",
                                                                      "Wind_Chill_F" = "Wind_Chill_F",
                                                                      "Wind_Chill_C" = "Wind_Chill_C")),
            actionButton("num_button", "Num Analysis", icon = icon("play")),
            br(),
            
            
        ),
        mainPanel(
            tabsetPanel(
                tabPanel(
                    title = "Numeric",
                    plotOutput("plot_2")
                )
            )
        )
    )
    
)

#funkcia na vytvorenie grafu pre num atributy
draw_plot_num <- function(data_input, num_var){
    ggplot(data = data_input,
           aes_string(x = num_var))  
        geom_histogram()
}

ui <- navbarPage(
    title = "Data Analyser",
    main_page,
    about_page
)
server <- function(input, output){
    options(shiny.maxRequestSize=550*1024^2)
    
    data_input <- reactive({
        req(input$csv_input)
        fread(input$csv_input$datapath)
    })
    
    num_var <- eventReactive(input$num_button,input$num_var)
    
    plot_2 <- eventReactive(input$num_button,{
        draw_plot_num(data_input(), num_var())
    })
    
    output$plot_2 <- renderPlot(plot_2())
    
    
}



shinyApp(ui = ui, server = server)

CodePudding user response:

Problem is with this part:

draw_plot_num <- function(data_input, num_var){
    ggplot(data = data_input,
           aes_string(x = num_var))  
        geom_histogram()
}

aes_string is soft-deprecated according to documentation, so you should move to some modern approach :).

Try this:

draw_plot_num <- function(data_input, num_var){
  ggplot(data = data_input,
         aes(x = .data[[num_var]]))  
    geom_histogram()
}

To understand this topic, you should refer to the Programming with dplyr, where you can find that when you use non standard evaluation (like reffering to the column without using $ operator or [[) and the name of column is stored in variable as a string (and it will be string if it comes from shiny), then you need to use .data[[col_name]], where col_name is a variable containing column name as string (character).

  •  Tags:  
  • Related