在数据框中选择用于绘图的列-R Shiny

问题描述 投票:1回答:1

通过下面的编码,我一直试图能够更改y轴来开发该图,但是我不确定自己在做什么。在this问题中,似乎他们想拉相似的东西,但使用数据表,不同之处在于他们有一个Global数据框,我需要将其设为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)

Graph

在此other question中,他们似乎将其拉出的方式与我所做的类似,但它继续打印此错误: Listening on http://127.0.0.1:7465 Warning: Error in [.data.frame: undefined columns selected [No stack trace available]

抱歉,太简单了,但是

r ggplot2 shiny plotly r-plotly
1个回答
0
投票

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

如果您查看帮助?radioButtons,则会在choices下看到此提示:

值应为字符串;其他类型(例如逻辑和数字)将被强制转换为字符串。

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

© www.soinside.com 2019 - 2024. All rights reserved.