MelaniaCB
MelaniaCB

Reputation: 435

Select column in data frame for plotly graph - R Shiny

With coding below I've been trying to be able to change y-axis for developing that graph, but I'm not quite sure what I'm doing worng. In this question it seems like they want to pull something alike but with a data table, difference is they have a Global dataframe, I need it to be reactive because I want the whole visualization to change when I change that input.

# GLOBAL #


# UI #
ui <- fluidPage( 
  # Set theme 
  theme = shinytheme("lumen"),

  navbarPage("Analysis",

             tabPanel("Impact",

                      titlePanel(
                        div(
                          h1(HTML(paste0("<b>","Graph against cluster count","</b>"))),
                          align = "left"
                        )
                      ),

                      tags$br(),

                      fluidRow(
                        sidebarPanel(
                          hr(style="border-color: #606060;"),
                          h3(HTML(paste0("<b>","Clusters impact.","</b>"))),
                          h5("Key areas of patent concentration can be found around the clusters that reach higher levels."),
                          br(),
                          # Y axis selection 
                          radioButtons("y_axis", 
                                       h4("What do you want to analyze IP collection against?"), 
                                       choices = list("Claims"                  = 3, 
                                                      "Forward citations"       = 4,
                                                      "Backward citations"      = 5,
                                                      "Patent Strenght mean"    = 6),
                                       selected = 3), # radioButtons - y_axis
                          hr(style="border-color: #606060;"),
                          width = 3
                        ),
                        mainPanel(
                          br(),
                          plotlyOutput("impact"),
                          br(),
                          width = 9
                        )
                      )
             )
  )
)

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

  # Set maximun input size as 100Mb
  options(shiny.maxRequestSize=100*1024^2)

  # Plot

  ## Data setting
  dtd5 <- reactive({

    dtd5 <- structure(list(Topic = c("Topic 1", "Topic 3", "Topic 5", "Topic 9"), 
                           Count = c(45L, 51L, 40L, 32L), 
                           Claims = c(630, 346, 571, 599), 
                           Forward = c(64, 32, 27, 141), 
                           Backward = c(266,  177, 101, 397), 
                           `Strength mean` = c(31, 25.22, 30.85, 39.59)), 
                      row.names = c(NA, -4L), class = "data.frame")
    dtd5 <- as.data.frame(dtd5)

  })

  ## Visualization
  output$impact <- renderPlotly({

    # Color setting
    ramp4 <- colorRamp(c("darkred", "snow3"))
    ramp.list4 <- rgb( ramp4(seq(0, 1, length = 15)), max = 255)

    # Scatterplot
    y <- dtd5()[,input$y_axis]

    p <- ggplot(dtd5(), aes(x=Count, y=y) ) +
      geom_point(aes(col=Topic)) +
      labs(y=colnames(dtd5())[input$y_axis],
           x="Cluster count",
           title="Cluster Impact")  +
      theme_minimal() +
      scale_colour_manual(values=ramp.list4)

    ggplotly(p) %>%
      config(displayModeBar = FALSE) 

  })
}

shinyApp(ui,server)

In the console it prints this one out, so I'm sure structure works out fine but getting it inside the App is where something goes worng.

dtd5 <- structure(list(Topic = c("Topic 1", "Topic 3", "Topic 5", "Topic 9"
), Count = c(45L, 51L, 40L, 32L), Claims = c(630, 346, 571, 599
), Forward = c(64, 32, 27, 141), Backward = c(266, 177, 101, 
397), `Stregth mean` = c(31, 25.22, 30.85, 39.59)), row.names = c(NA, 
-4L), class = "data.frame")

# Scatterplot
y <- dtd5[,4]
p <- ggplot(dtd5, aes(x=Count, y=y) ) +
  geom_point(aes(col=Topic)) +
  labs(y=colnames(dtd5)[4],
       x="Number of patents",
       title="Cluster Impact")  +
  theme_minimal() 

ggplotly(p) %>%
  config(displayModeBar = FALSE)

Graph

In this other question they seem to be pulling it similarly to what I've made but it keeps on printing this error: Listening on http://127.0.0.1:7465 Warning: Error in [.data.frame: undefined columns selected [No stack trace available]

Sorry if it's too easy but

Upvotes: 1

Views: 1119

Answers (1)

Ben
Ben

Reputation: 30494

The problem appears to be with your radioButtons - even though the choices are set to return numeric values 3 through 6, it will return a string.

If you check help ?radioButtons you will see this noted under choices:

The values should be strings; other types (such as logicals and numbers) will be coerced to strings.

If you specify as.numeric(input$y_axis) in both places in renderPlotly it should work.

Upvotes: 2

Related Questions