Select 绘图数据框中的列 - R Shiny

Select column in data frame for plotly graph - R Shiny

通过下面的编码,我一直在尝试能够更改 y 轴以开发该图表,但我不太确定我在做什么 worng。在 this 问题中,他们似乎想要提取类似的东西,但使用数据 table,不同之处在于他们有一个全局数据框,我需要它是 reactive 因为我想要整个可视化当我更改该输入时更改。

# 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)

在控制台中它会打印出这个,所以我确信结构可以正常工作,但将它放入应用程序中会出现问题。

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)

在这个 中,他们似乎以与我所做的类似的方式拉动它,但它一直在打印此错误: Listening on http://127.0.0.1:7465 Warning: Error in [.data.frame: undefined columns selected [No stack trace available]

对不起,如果它太简单了,但是

问题似乎出在您的 radioButtons - 即使 choices 设置为 return 数值 3 到 6,它也会 return 一个字符串。

如果您查看帮助 ?radioButtons,您会在 choices:

下看到此注释

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

如果您在 renderPlotly 的两个地方都指定 as.numeric(input$y_axis) 它应该可以工作。