小编Sim*_*ard的帖子

想要将Pandas Dataframe绘制为具有log10刻度x轴的多个直方图

我在Pandas数据帧中有浮点数据.每列代表一个变量(它们具有字符串名称),每一行代表一组值(这些行具有不重要的整数名称).

>>> print data
0      kppawr23    kppaspyd
1      3.312387   13.266040
2      2.775202    0.100000
3    100.000000  100.000000
4    100.000000   39.437420
5     17.017150   33.019040
...
Run Code Online (Sandbox Code Playgroud)

我想为每列绘制直方图.我实现的最好结果是使用了dataframe的hist方法:

data.hist(bins=20)
Run Code Online (Sandbox Code Playgroud)

但我希望每个直方图的x轴都是log10刻度.并且bin上的bin也是log10,但这很容易使用bins = np.logspace(-2,2,20).

一种解决方法可能是在绘图之前对log10进行数据转换,但是我尝试过的方法,

data.apply(math.log10)
Run Code Online (Sandbox Code Playgroud)

和

data.apply(lambda x: math.log10(x))
Run Code Online (Sandbox Code Playgroud)

给我一个浮点错误.

    "cannot convert the series to {0}".format(str(converter)))
TypeError: ("cannot convert the series to <type 'float'>", u'occurred at index kppawr23')
Run Code Online (Sandbox Code Playgroud)

python logarithm matplotlib histogram pandas

10
推荐指数
1
解决办法
2814
查看次数

R Shiny 应用程序检测手机

我有一个闪亮的应用程序(实际上是一个交互式 R Markdown 报告),我想根据用户是否在移动设备上对其进行格式化。我发现g3rv4的这篇博客文章描述了如何测试这一点,但我无法让它在下面的示例应用程序中工作。

https://g3rv4.com/2017/08/shiny-detect-mobile-browsers

我是一名建模者,而不是程序员,所以我可能在 Javascript 方面做错了一些事情。我没有收到错误,但没有收到任何输出textOutput('isItMobile')。

# shiny example from
# https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/
# mobile detect code from
# https://g3rv4.com/2017/08/shiny-detect-mobile-browsers
library(shiny)

onStart <- function(input, output) {

  ### function to detect mobile ####
  mobileDetect <- function(inputId, value = 0) {
    tagList(
      singleton(tags$head(tags$script(src = "js/mobile.js"))),
      tags$input(id = inputId,
                 class = "mobile-element",
                 type = "hidden")
    )
  }

}

# Define UI for app that draws a histogram ----
ui <- fluidPage(

  # App title ----
  titlePanel("Hello Shiny!"),

  mobileDetect('isMobile'), …
Run Code Online (Sandbox Code Playgroud)

mobile r shiny

5
推荐指数
1
解决办法
3018
查看次数

如何使用 datatable 函数替换 R 中渲染的 DT 中的数据

我有一个R shiny应用程序,DT datatable它是使用该datatable函数呈现的,以便设置各种选项。我想使用dataTableProxy和replaceData更新表中的数据,但我能找到的所有示例都假设DT直接从数据对象呈现,而不是使用该datatable函数。下面的代表显示了我想做的事情,但replaceData在这种模式下不起作用。我该怎么做呢?谢谢。


# based on 
# https://community.rstudio.com/t/reorder-data-table-with-seleceted-rows-first/4254

library(shiny)
library(DT)

ui = fluidPage(
    actionButton("button1", "Randomize"),
    fluidRow(
        column(6,
               h4("Works"),
               DT::dataTableOutput('table1', width="90%")),
        column(6,
               h4("Doesn't Work"),
               DT::dataTableOutput('table2', width="90%"))
    )
)

server = function(input, output, session) {

        my <- reactiveValues(data = iris)

        output$table1 <- DT::renderDataTable(isolate(my$data))

        output$table2 <- DT::renderDataTable({
            DT::datatable(isolate(my$data),
                          options = list(lengthChange=FALSE, ordering=FALSE, searching=FALSE,
                                       columnDefs=list(list(className='dt-center', targets="_all")),
                                       stateSave=TRUE, info=FALSE),
                          class = "nowrap cell-border hover stripe",
                          rownames = FALSE,
                          editable …
Run Code Online (Sandbox Code Playgroud)

r shiny dt

5
推荐指数
1
解决办法
4439
查看次数

使用dplyr :: mutate计算到数据点的地理距离

我试图使用R与tidyverse包,并在将函数应用于我的数据时遇到问题.我的数据包括纬度/经度坐标,我想计算从每个位置(我的数据帧的行)到参考位置的距离.我正在尝试使用geosphere :: distm函数.

library(tidyverse)
library(geosphere)

my_long <- 172
my_lat <- -43

data <- data %>%  rowwise() %>% mutate(
  dist = distm(c(myLong, myLat), c(long, lat), fun=distHaversine) # this works
)
Run Code Online (Sandbox Code Playgroud)

我使用该rowwise()函数,如上所述,但这已被弃用,所以我想知道如何使用现代tidyverse,即,dplyr或者purrr,我认为,例如我最接近的是使用map2:

my_distm <- function(long1, lat1, long2, lat2)
  distm(c(long1, lat1), c(long2, lat2), fun=distHaversine)

data <- data %>%  mutate(
  dist = map2(long, lat, my_distm, my_long, my_lat) # this doesn't
)
Run Code Online (Sandbox Code Playgroud)

到目前为止,我失败了.

r dplyr tidyverse geosphere

3
推荐指数
2
解决办法
951
查看次数

无法让 Leaflet.Spin 插件在 R Shiny 中工作

我有一个闪亮的应用程序,它涉及在地图上绘制大量线条。我想使用微调器向用户显示渲染正在进行中。大多数闪亮的方法都不起作用,因为它们只在数据发送到传单时显示微调器,而不是在传单渲染时显示。Leaflet.Spin 插件看起来很有前途,但我一直在努力让它工作。我一直在遵循的例子是

https://gist.github.com/jcheng5/c084a59717f18e947a17955007dc5f92

以 polylineDecorator 为例的传单插件和传单代理

当线条(本例中的圆圈)渲染时,如何让 js 事件正确触发并显示 Leaflet.Spin?谢谢!

更新:微调器现在可以工作,但事件会为添加的每个单独的圈子触发,因此如果圈子数量减少,微调器不会正确关闭。

library(shiny)
library(leaflet)
library(htmltools) # for htmlDependency
library(htmlwidgets) # for onRender

# https://gist.github.com/jcheng5/c084a59717f18e947a17955007dc5f92
# https://stackoverflow.com/questions/52846472/leaflet-plugin-and-leafletproxy-with-polylinedecorator-as-example
spinPlugin <- htmlDependency(
  "spin.js", 
  "2.3.2",
  src = c(href = "https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.3.2"),
  script = "spin.min.js") # there's no spin.css

leafletspinPlugin <- htmlDependency(
  "Leaflet.Spin", 
  "1.1.2",
  src = c(href = "https://cdnjs.cloudflare.com/ajax/libs/Leaflet.Spin/1.1.2"),
  script = "leaflet.spin.min.js")

registerPlugin <- function(map, plugin) {
  map$dependencies <- c(map$dependencies, list(plugin))
  map
}

# Note: Ctrl-Shift-J opens the javascript console in the browser
spin_event <- "function(el, x) …
Run Code Online (Sandbox Code Playgroud)

javascript r leaflet shiny

3
推荐指数
1
解决办法
444
查看次数