我想制作直方图:具有给定价格的加密货币数量。我想在y轴上设置频率,可以使用bin滑块更改频率:1-50,因为此API调用使用了50种加密货币。 x轴的价格应为区间,例如:1000000、1100000、1200000、1300000 ...等,以及这些区间中有多少种加密货币。我向coinmarket Open API发出请求,并从JSON Total Supply中获取以下信息:
library(shiny)
library(jsonlite)
fetchjson <- fromJSON("https://api.coinmarketcap.com/v1/ticker/?limit=50")
# Define server logic required to draw a histogram
# Define server logic required to draw a histogram ----
server <- function(input, output) {
# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- as.double( fetchjson$total_supply)
bins <- seq(as.integer(min(x)), .Machine$integer.max, by = 1000)
#barplot(table(fetchjson$total_supply))
hist(x, breaks = bins)
#col = "#75AADB", border = "white",
#main = "Number of currencies with some Total Supply"
})
}
这里是排序后的数据(为double,因为jsonlite将其转换为字符串):
> sort(as.double(fetchjson$total_supply))
[1] 1000000 5214419 8437465 8798633 16562199 17371487 17437911 17453125
[9] 53252246 56869592 59117338 100000000 100000000 101028884 103143260 105961236
[17] 127276552 133248289 140245398 156756875 157911504 190799315 273685830 300343598
[25] 763306930 800460000 1000000000 1000000000 1000000000 1000000000 1006245120 1407000000
[33] 1500000000 2580109502 2671180000 2779530283 8999999999 10000000000 11041428664 12600000000
[41] 15172086051 31112483745 37679831499 86712634466 99000000000 99991792688 104443257863 116945854633
[49] 184066828814 280255193861
问题是:当我运行应用程序时,只有Bins滑块,但是没有直方图,并且出现错误:
警告:hist.default中的错误。默认值:未计入某些“ x”;也许“中断”没有跨越“ x”的范围
我认为垃圾箱定义有问题,但是我不知道是什么。我以为是因为double不起作用,所以我使用了整数,并以MAXINTEGERVALUE作为限制。 “ bin的=“ =是:1000,作为测试,因为我想显示min(x),+ 1000,+ 2000,+ 3000,+ 4000这样的数据。因此它可以显示在这些时期内具有价格的货币的频率。我不知道为什么它不起作用。请帮助
还有ui.R,我可以在其中定义Bin滑块
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
# Define UI for application that draws a histogram
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)
)
)